New answers tagged

1

I don't think so because there are a fixed number. You may have different boards. If you write different code for each board (i.e. in the style of board.RelayTen.), you'll more code to maintain. One way to contain this expansion of configurations is to use a common program with external configuration or maybe an internal configuration that is ...


0

In my experience, when someone has complained about 'too much boilerplate' and wants to use AutoMapper, it's been one of the following: They find it irritating to write the same code over and over. They are lazy and don't want to write code that does A.x = B.x. They are under too much time pressure to actually think about whether writing A.x = B.x over ...


1

Refactor the big function. It probably does several things repeatedly or nearly so, if it loads several "prefabs". Then, the logic needed for repetition can be put into other functions, which could open more possibilities for refactoring (one or more new classes for loading "prefab" types, maybe?). Moving the code to a partial class will only obscure the ...


2

Readonly fields This goes without saying. And getter-only properties Yes, as long as by "getter-only properties" we strictly mean type prop { get; } That are immutable structs Yes, in the sense that in C# primitives are also implemented as immutable structs. And aren't static This goes without saying. And aren't interfaces This ...


2

Don't you want to abstract out this functionality ? e.g. you may want to log to a file, to a network socket, to stdout etc. I certainly don't think that your classes should know about files etc. but about an abstract logger, and you can decide later how this is going to work. This is a very common requirement, and you'll find libraries already exist to do ...


2

I would suggest you rather use a Win Form app for game. If you use Console you will need to refresh (clear and redraw) the console each time you want to change display - that is with change in each second. On a win form you can directly put a timer control and start the timer on game start (button click, form load - as per your need). When timer tick event ...


6

If your class is too big and unwieldy when it's all in one place and you can easily see it all, what makes you think scattering it across multiple files and making it harder to correlate will be an improvement? Partial classes are essentially a hack to make it easier for generated code, such as that produced by the form designer, to work together with ...


1

You should read up on the differences between object and dynamic. Here's some info http://stackoverflow.com/questions/5523031/dynamic-keyword-vs-object-data-type and https://blogs.msdn.microsoft.com/csharpfaq/2010/01/25/what-is-the-difference-between-dynamic-and-object-keywords/ The biggest difference is that the compiler leaves dyanmic checking to the ...


2

Instead of "last", if you implemented "orderbyDesc" and "take" (and maybe also "skip") then your queries could always return collections, but users would understand that to get to the last item they would need to fetch it via $where[#eq(Name,Test)&&#gt(Id,12345)]$orderbyDesc[Name,-Id]$take[1] I wouldn't change the collection to a single object just ...


0

Your question can be easily implemented using a distributed cache like NCache. What you require is a Pessimistic Locking mechanism where you could Acquire a lock using an object. Then perform your tasks and operations and release the lock for other applications to consume later on. Take a look at the following code; Here you would acquire a lock on a ...


1

tl;dr; Yes, it is possible. But I doubt you will be able to support high-quality high-definition video. The performance of the decoding is not about it being "slow". It is about being able to support specific bitrates. If your decoder is slow, it can still play low bitrate video, but it will be unusable for higher bitrates. Video decoding is all about data ...


4

Video players have what I call a "threshold of computation;" there exists a minimum level of data flow (for a given video resolution), below which a video player will not work at all (or will only work intermittently). To support writing a video player successfully, a programming language would most likely need: Relatively high performance, Bit ...


1

I think the question is a symptom, not a solution. I recently read a lot about Singletons being bad and how dependency injection (which I understand as "using interfaces") is better. When I implemented part of this with callbacks/interfaces/DI and adhering to the interface segregation principle, I ended up with quite a mess. A solution looking for a ...


2

The main question over whether you should implement IDisposable is if you are using any unmanged resources. From the IDisposable Documentation The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not ...


4

If TestProcedure contains exclusively managed objects (as opposed to the resources such as database connections, GDI+ objects, etc.), there is nothing you need to do. Just create a new instance of TestProcedure at the beginning of the test, and let Garbage Collector handle the removal of the object when the test finishes. If TestProcedure uses objects which ...


1

I would use the former approach. The latter duplicates a lot of effort, especially since the pairs of interfaces will likely be the same. So following the KISS and DRY principles, I would simply use one set of interfaces and not create a second (likely duplicate) set.


0

Given Have StandardizedData, Collection, and CollectionCollection store than the standardization info. I'd like to avoid this scenario if possible as there could be a 1000 StandardizedData object which will be loaded into the small memory of mobile devices. you could investigate the flyweight pattern. If your standardisation info is replicated, ...


3

This is not a good idea because you can introduce a race condition. In one thread you change the value to one you want but before you manage to create an instance of the class another thread changes the value, and you get an instance created with the wrong default value. Your hair will grow whiter than mine, trying to figure out why things aren't working ...


4

I'm sure there are situations where this would make sense to do, particularly if you're writing a small application, but in general I think it would be a bad idea. You could apply any argument that you could make about global variables to static variables - they're still changeable anywhere in the application. If your application (and dev team) is ...


3

Are there any other objections? Are there any legitimate, real-world reasons why DI with a static/global service locator would be bad? Ugh, yes. Statics/globals are horrible. They assume that the runtime of your application is homogenous - all of your instances require all the same sort of instances all over. That is naive. They interfere with ...


1

I think you are missing the benefit of dependency injection--that anything the class depends on is given to it, rather than assumed. What you're proposing is the service locator anti-pattern. There are a few cases where I might be tempted to use it (identity), but I'd prefer a clean contract for one reason--I never know how someone is going to use my code. ...


0

Factories are an exception to the: no service locator no singletons no global variables no tightly coupled dependencies rules. In theory, an enterprise code would be divided into two main parts: object graph constructors: place where classes are newed to become objects and are wirded together based on either specific configuration, flags or general ...


2

First things first, the mantra that service-locators are an anti-pattern is tiresome and counter productive. They have their downsides, but they're pretty much the same as conventional IoC containers except IoC containers are good. That said, let's focus on your examples: Simple factories are good. They are clear. They are easy to test. They are easy to ...


0

If you load the regex's from database you get a lot more dynamic solution, which I tend to like. If you don't want to create the complexity right now though, you could start with a few hardcoded patterns and then refactor into a db-driven solution. I get a bit worried about your validation in javascript though. Client side validation is only for quick ...


4

I have experienced this problem first hand. It is difficult, and I was only integrating five applications. I can offer a few tips: Decouple the database by using RESTful services for as many things as possible. This allows you to easily create multiple interface versions if needed, and it makes database migrations much easier. If database access for some ...


0

MyModels - class lib. No dependencies IMyDataRepository - interface. Depends on MyModels MyDataRepository_EfSql - depends on MyModels and IMyDataRepository and Entity framework private EF Context private EF Models private EF Model to Domain Model mapping MyApplication - depends on all three. But the repository is loosely coupled via dependency injection ...


1

"Class heirarchy" is a bit of a red flag. Hypothetical: a web page with 5 widgets. The page is not an ancestor of any of the widgets. It may hold a reference to those widgets. But it is not an ancestor. Instead of class heirarchy, consider using composition. Each of the 5 widgets can be constructed on its own, without reference to the other widgets or ...


1

If you are into TDD, I suggest you write up a test which will fail until the functionality gets implemented. The only downside I can see is if you are a somewhat lazy function such failing tests will contribute a lot of noise to runs and may hide actual failures behind all that.


3

This looks like it will eventually bite you in the behind, as in that it's very cluttered and doesnt really provide good maintainability. I would have organized it like this: Each resource (model) gets its own class file containing its attributes, like: Model/Article.cs Id Name Price Each resource collection has a class containing methods for ...


2

If you encapsulate all IO operations in a small class with an interface and inject it to the archiving class you can mock calls to the IO system during testing. public class Archiver { public IFileSystem Filesystem { private get; set; } public void DoWork() { //business logic here } public Archiver() { Filesystem = new ...


0

You should either use a NoSQL database and convert your objects to JSON or you could use NCache and simply serialize your C# objects and store them as is. If you need to query against those individual values then you should use its SQL capabilities or tag/group your keys since NCache is more than just a simple .net key value store. Read here store = ...


2

Use the factory-pattern. A factory is an object which creates objects. So when you have a weapon which shoot projectiles, pass a ProjectileFactory to it and leave the creation of the projectile to that class. You can then have different classes which extend ProjectileFactory, like BulletProjectileFactory, ExplosiveProjectileFactory, ...


5

Excuse me while I react to everyone suggesting the builder pattern here. This is c#, not java! A main reason for Joshua Bloch's builder pattern is to hack around java's lack of named arguments. This gives java a way around the evil telescoping constructor pattern. You're in c#. You have named arguments! Another reason for Joshua Bloch's builder pattern ...


0

Is it against any design rules or considered as bad practice when you give a class many constructors and assign via them as many properties as possible? I don't think is a bad practice to do that, but I think the real question here is: Are you sure they are really needed? There is the YAGNI (You Aren’t Gonna Need It) principle of software development ...


0

Is it against any design rules or considered as bad practice when you give a class many constructors and assign via them as many properties as possible? This sounds like: you need a builder pattern. Implement a builder, and you can then call 'build()' repeatedly on a previously constructed builder, adjust parameters as required if you have that ...


0

You could use generics, but its not quite the same as your example. MyFaveClass<T> { public T OnlyOneOfThese { get; set; } } var x = new MyFaveClass<AnotherClass>(); Here is another method which allows you to set the object. The as operator returns null if it cant do the cast, so this relys on the classes not inheriting from each other ...


1

Looks like MyFavoriteClass itself is a problem and should be split. Create specializations of this class which can only contain a specific pair of referenced types, that's how you get the constraint. Then use a factory which is capable of emitting the correct specializations of MyFavoriteClass. You may either provide an overload for each combination of ...


0

Once you have a rough idea of what a class or function should do, you have enough information to write tests. These tests may fail, due to the thing you're testing being broken, but that is essentially a good thing. If you prefer writing the code before you write the tests for the code (sometimes, having an implementation may yield a better idea of what ...


1

Someone else has mentioned this, but I really think an Enum is the tool for the job here. Here is a detailed explanation of when and why they can be useful. It's a good design choice over many other constant/key options for performance, extensibility, readability, etc. If I understand what you're trying to do, an Enum is doing exactly what you intend with ...


3

How about an enumeration? (thanks to @JoelFan for pointing out the Enum ToString method) var value = GetValue(Keys.SSN.ToString()); And of course you would define an enumeration with each of your keys. enum Keys { SSN, FirstName }


3

No. You've made the contract of the SSN constant really weak- it's completely non-obvious what it's for. You could use nameof in the definition but that would still look pretty similar to your original code. const string FirstName = nameof(FirstName); const string SSN = nameof(SSN);


4

You should be creating tests for all the code that you write, regardless of whether they're libraries or your application. Why ? you can assert that the code you write works the tests assert that that code continues to work as you change or add subsequent code. In short, it doesn't matter whether it's a library or an application that you're writing code ...


0

Unless you have a good reason, I would go with the simpler approach, that is, eager loading. And, assuming you can use C# 6.0, I would try to simplify the code even more: public MvxCommand<object> LoginCommand { get; } = new MvxCommand<object>(LoginUser); public MvxCommand<object> SignUpCommand { get; } = new ...


1

You should follow the coding guidelines where you work or on the projects you work on. You will find that there are a lot of people who are totally for or totally against the use of this unless it is needed as in the example given in @KilianFoths answer. It does not have anything to do with public, protected or private.


3

No, you shouldn't use this redundantly. It adds unnecessary clutter and makes you look as if you're not sure what fields your class has. The only reason that many people do this is that they often see this introductory example: public Foo(Bar bar) { this.bar = bar; } and wrongly assume that you always have to use this, when in fact you only need it ...


2

In Ruby on Rails, which places validation logic in the model rather than in the controller or otherwise closer to the front end, the pattern used is to provide both a Boolean and an error dictionary. After an object has been validated, you can access the errors dictionary which has property names as keys, and a set of error description strings as the value. ...


1

The article you linked to is using Tasks, you should should do that too, instead of BackgroundWorkder. There are many reasons why Tasks are preferable, Stephen Cleary has a series of articles explaining why.


1

Don't use Pthread mutexes to synchronize between processes, at least on Linux. (I am not sure that Linux is implementing pthread_mutexattr_setpshared correctly and efficiently, at least not in GNU glibc 2.21). Use POSIX semaphores, see sem_overview(7). Or consider the Linux specific eventfd(2) probably with poll(2) & read(2) & write(2). Both ...


2

By exposing Services you are exposing your business. You should not expose your database. Even if Service just implement the very same methods that repository does. It's also a way to grant access to the data to certain set of components. These components are also key components for the transaction management. By exposing the interface instead of the ...


2

Brian Agnew's suggestion is very much applicable, but I'd like to point out something else: I get the feeling that maybe the actors (the plugged-in behavior you mentioend) on your model are carrying two very different responsibilities. Alternatively, you could design your framework to have a separate registration mechanism, exclusively for disposable ...



Top 50 recent answers are included