Tag Info

New answers tagged

2

If you were talking about manual refactoring to bring your code up to "C# 6.0 standard", I would heavily vote against refactoring parts of your code in areas you are not currently working with. That would bring only a minimal benefit for a certain risk of breaking things unintentionally. However, you are talking about an automated migration. In such a ...


2

If I could tell everyone who tackles the problem of injecting values from configs into a DI paradigm one thing, it would be this: You don't have to put all config access through one monolithic dependency. Just because your settings all come from one place under the hood doesn't mean they all belong together, and that you have to persist the config as one ...


0

Maybe. There are exceptions to every rule, and no rule should be followed uncritically. You example might conceivably be one of the instances where it makes sense to swallow all exceptions. For example if you want to add tracing to a critical production system, and you want to make absolutely sure that you changes don't disrupt the main task of the app. ...


3

No. And even if the performance would be different (which it is not, see the link in the comment given by Sami), what do you think is more important, speed or correctness? A program which does not work correctly is pretty useless, how fast it may ever be. The only reason I can think of is that you are 100% sure the "logic" part does not throw any ...


25

They are named differently because semantically they are quite different: A collection's Count is the number of items currently stored in it and can potentially change over time. An array's Length is the maximum number of items it can hold (it will have a length of 10 even if you haven't stored that many items in it) and is immutable. Example: If I have ...


3

Functional programming languages consist only of expressions, so most have had a feature like this from essentially the beginning. You use it when you have a relatively short, often frequently repeated, expression that can be replaced by an even shorter, much more meaningful name. A common example would be predicates that are used elsewhere, like: public ...


1

As of c# 6.0, which is now released, you should just do this, public class Room { public Room(Client client) { this.Client = client; } public Client Client { get; } } public class Client { public Client(long id) { this.Id = id; } public long Id { get; } } Raising a property of Client to Room is an obvious ...


1

It depends. Using a reader/writer lock rather than a critical section can be beneficial if read operations are noticeably more common than write operations. But I think you could probably achieve more by changing your application architecture. If writes are rare, you can probably get much better performance by treating the collection as immutable and ...


2

If it was me, I'd do something similar to the following, utilising split() and switch: public void ProcessInput(string lineOfInput) { string[] parts = lineOfInput.Split(' '); switch (parts[0]) { case "/j": case "/join": JoinChannel(parts[1]); break; case "/w": case "/whisper": ...


2

So you have a 5 neatly separated components, which I suppose you can reuse later with different code. Yet you thinking of having a single interface shared between those components, which will provide a bunch of irrelevant config properties, e.g. we can assume that 80% of properties will not be relevant for each of the component. Doesn't seem logical to ...


2

If a null Client property is a supported state, consider using a NullObject. But most likely this is an exceptional state, so you should make it impossible (or just not very convenient) to end up with a null Client: public Client Client { get; private set; } public Room(Client client) { Client = client; } public void SetClient(Client client) { ...


1

I would vote in favor #1, because I find it slightly easier to understand what's going on and control the behavior. We used this approach to build a fairly large enterprisey apps without too much fuss. It also is easily customizable, if needed (e.g. would 1 token per user suffice? Would you need multiple token if user simultaneously logs in from multiple ...


6

As long as all your configuration options come from a file, using interfaces does not bring you any benefit, since your configuration file can only provide fixed values. Even if you have to deal with more than one option per service, I do not see any convincing reason for using interfaces. You can still utilize simple "data objects" for the configuration ...


0

I think it makes perfect sense to group multiple configuration options into an interface, when they obviously belong together. But as long as you only have one option per domain, it seems indeed overengineered. However as soon as you get multiple options concerning the same domain (e.g. three options about logging) then you should move them to their own ...


6

From C#/.NET Little Wonders: Expression-Bodied Members in C# 6: So, should you use this? It all comes down to style. My gut would be to limit this to simple expressions and statements that can clearly be understood at first glance. (emphasis, mine; see Update 1 below) More from the Summary of the article above: So C# 6 now gives us the ability ...


17

I disagree with all three of the opinions. If Client can never be null, then don't even make it possible for it to be null! Set the value of Client in the constructor Throw an ArgumentNullException in the constructor. So your code would be something like: public class Room { private Client theClient; public Room(Client client) { ...


5

The second and third options should be avoided - the getter should not smack the caller with an exception they have no control over. You should decide whether Client can ever be null. If so, you should provide a way for a caller to check whether it is null before accessing it (e.g., bool ClientIsNull property). If you decide that Client can never be null, ...


21

Just a few considerations: a) Why is there a getter specifically for the ClientId when there's already a public getter for the Client instance itself? I don't see why the information that the ClientId is a long has to be carved in stone in the signature of Room. b) Regarding the second opinion you could introduce a constant Invalid_Client_Id. c) Regarding ...


23

It smells like you should limit the number of states your Room class can be in. The very fact that you're asking about what to do when Client is null is a hint that Room's state space is too large. To keep things simple I wouldn't allow the Client property of any Room instance to ever be null. That means the code within Room can safely assume the Client ...


0

Your problem sounds like it is caused by a failure to follow the Interface Segregation Principle. Fortunately, there's a fairly simple solution: take your entity objects and create one interface in each for each type of client, containing only the methods that make sense for that client. Then, for each client, create a wrapper for your ORM that works using ...


2

The benefit of immutability is that it doesn't matter as much what you do with your references to it. Globals, static classes, and singletons are much less problematic than their mutable counterparts, because you don't have to synchronize mutations and track the order dependencies that creates. However, they still have the issues with mocking out for unit ...


-2

(This entry is a wiki. Please feel free to improve this answer) Here is argumented how using static for immutable might be preferable, and related concepts. Constants themselves are defined as global. Objects that wont change not only during runtime, but wont have alternatives values (like a filesystem path might change depending configuration) a priori ...


4

Fundamentally, there's no problem described in the question that actually needs a solution. You've given no reason for a Foo holder to exist, or what it should be able to do. So really, FooList is completely useless. If you need a Foo, there's a language feature for that- new Foo(1). There's no reason for any additional holding to be baked in to the Foo ...


7

The problem is that you've effectively created a global variable for accessing these objects, the Foolist class. This has drawbacks for things like testability and is in general just poor design. Global state doesn't help make code easy to read and the consuming classes can never be isolated from the Foos (look up mocking) d when testing. Avoid the static ...


0

I think a nonpublic set for "user editable" properties is just making work for yourself. I'd leave them public (for changeable properties, i.e. not Id) and then either use the DAO-style .Edit and .Update state change methods (where attempting to change before calling .Edit throws a runtime error), or simply always allow changes and supply .CommitChanges to ...


7

The underlying issue is not Session or Asp.Net specific, it is a fundamental issue in how collections in statically typed languages work. The Session object is designed to let you store objects of any type. This means the static type of the stored items must be Object. So Session is equivalent to a Dictionary<string, Object>. Since the compiler can ...


4

What you're suggesting is not possible, because the type of the assigned variables cannot be known until runtime. Remember that session is effectively shared between all modules used in creating the page. Some of these may not be known at compile time. Therefore you either need to specify the type, or move the check to runtime by using object or dynamic. ...


2

Session is effectively a map/dictionary with a key that is a string, and a value that is an object. It predates generic types in C#/ASP.Net, and would more likely be an IDictionary<string,dynamic> today, which would support what you are trying to do. //you cannot do this either... //default from a session is an object/null, not even an integer object ...


0

Similar to this question/answer and I've used the c# algorithim posted there in the past and it works like a charm here's the algorithim from the linked answer: public class SimilarityTool { public double CompareStrings(string str1, string str2) { List<string> pairs1 = WordLetterPairs(str1.ToUpper()); List<string> pairs2 ...


1

Regardless of what you use, you need to make sure you lock the single record in your settings table for the shortest time possible. It is the bottle-neck in all of your receipt creation. Because of this, I would do this in 3 steps with the first 2 wrapped in a transaction (I believe Nhibernate can do that.). Create Receipt Record Start Transaction: Update ...


1

Your application code and your EDMX should be portable, i.e. you should not need to change either of them when moving between Environments. That means no database (or schema) names "hard-coded" in there or, if there are, you should be able to replace them using nothing more complex than a global Search-and-Replace. Anything "cleverer" and all bets are off. ...


6

Here's a question: does this look like DI? public class MyCar { private IEngine _engine; public MyCar(IEngine engine) { _engine = engine; } public MyCar() : this(new MyV8Engine()) { } } It should, because it IS DI. Dependency Injection is about injecting a dependency into an object, instead of placing the ...


1

If there are objects being instantiated on the action due to lack of contructor injection how can you mock them to test? You can refactor your code to encapsulate all code that should be mocked away into a seperate protected methods and then use partial-mocks to replace that logic. Example (in java) Original protected void moveFiles(File[] ...


2

Your post gives me the (maybe false?) impression that you are questioning the use of NHibernate in full, because of a minor problem which cannot be perfectly solved by an ORM directly. Actually, in such a situation I would try to create a workaround for the minor problem (maybe I got you wrong, and that is what you already have in mind). For the case you ...


1

Compilers do a lot of optimizations. Inlining is one of them, whether the programmer wanted or not. For example, MethodImplOptions does not have an "inline" option. Because inlining is automatically done by the compiler if needed. Many other optimizations are especially done if enabled from the build options, or "release" mode will do this. But these ...


3

I would suggest using a facade object to hide the detail of the requirement for the decorator. The facade would simply create the service and its decorator behind the scenes, and then forward client requests to them. By presenting a simpler interface you make it easier for clients to use your service, avoid the need for a separate factory api and make it ...


0

SOAP/WS-* and RESTful APIs are not the same. If you want to build SOAP/WS-* WSDL supporting APIs the tool of choice in the Microsoft stack is WCF, mounted with an HTTP binding option (there are XML and JSON binding options, XML being the WSDL supporting option). In practice, consuming a WSDL from a different implementation language or platform has been ...


2

If you explicitly save data, the best place to perform that is in a call at the end of the highest level method, the entry point to your service. That means you must divide your service methods into two categories, those that are public, entry level methods, and those that are internal methods, that should not save data.


0

Should I provide these methods or should I let people implement their own? I'm trying to find the post (I think it was Stephen Cleary on SO), but one of the answers had made the point that he preferred to make his operations synchronous and let the client decide if they want to call them asynchronously or not. It stuck with me because it was Stephen ...


0

Unless you have a strong reason, you should use an ORM (Entity Framework, NHibernate). When you work with an ORM, the objects that were returned from the database will be "observed" and you just run the "SaveChanges();" leaving the dirty work to the ORM; The ORM will automatically check which entities need to be persisted and if there is no change to be ...


1

The more modern approach is to utilize the HTML Agility Pack. The syntax is closer to the dom selections that would be in a JS Library like JQuery. Here's some examples: http://stackoverflow.com/questions/846994/how-to-use-html-agility-pack http://htmlagilitypack.codeplex.com/wikipage?title=Examples This can also be loaded as a Nuget package.


6

It's not a very good design, because it doesn't adhere to Single Responsibility Principle . ICsvReaderWriter has two responsibilities, as the name itself indicates. I wouldn't lump them together into one class. Besides, the Reader / Writer distinction is sort of idiomatic in the .NET world (think StreamReader / StreamWriter - you won't find a ...


1

I think you might like the query object pattern I wrote for NHibernate:https://github.com/shaynevanasperen/NHibernate.Sessions.Operations It makes it easy to build and consume your own query objects, with support for "3rd level" caching and query-result transformation. It provides an IDatabases interface, which you use to invoke queries and commands. If you ...


0

After a bit more research and gaining a greater understanding of GPU architecture (it really helps if you have an electronic/microprocessor studies background here). Simple answer no: you cannot parse between kernals without CPU managment , which causes bottlenecking...and defeats the purpose of GPU calculations. However you can run functions external to ...


0

There's some datatype that's not supported by EF. geography geometry hierarchyid sql_variant http://thedatafarm.com/data-access/sql-server-2008-data-types-and-entity-framework-4/ I never use any of that type, but when using dapper I also come to this similar problem(processing image datatype), the easiest way to deal with this limitation is just to go ...


0

EF isolates your code from concrete database. Each database has it's own features but EF's API is not so powerfull to express them all. Actually, from the client's side EF is a simple object storage. So, if you want to manage such database features like specific datatypes, security checks, triggers, views... EF can't help with it. I haven't seen anything ...


0

In C#, the callbacks are mere delegates. Delegates wrap your callback method inside a object and that object is passed as a parameter. So, whenever you are passing a callback function, lets say an event handler, that event handler is wrapped in a delegate object. Now when the proper time of invocation comes, the corresponding element, in your example, the ...


0

Keeping a separate xml file is advisable because in future you may need to change these settings written in it and if it is in config then might be complex to change it... But make sure while going live that make that file read only for security reasons..


4

What do you call a callback function that accepts some parameters, but they are not explicitly declared when calling the function? I'm not sure. In certain cases an object's method may be considered to have the object itself as an implicit parameter. But there are no function calls with implicit parameters your examples. With a callback, you don't pass ...


2

What do you call a callback function that accepts some parameters, but they are not explicitly declared when calling the function? I'm not sure what you're talking about. function(data) explicitly declares a parameter named data for the anonymous function. private void MyCallBack(IAsyncResult ar) explicitly declares a parameter named ar with the type ...



Top 50 recent answers are included