Tag Info

New answers tagged

0

Are there any drawbacks to modifying the Subject Observer design pattern this way? Well, yeah. What I see is backwards, inside-out; an incorrect implementation The observer (ObserverClass) should not be the notifier - the notifier is your ProviderClass. The notifier notifies, the observer observes. Having the ObserverClass calling the providers ...


1

Try a readonly variable. MSDN says this: The readonly keyword differs from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, although a ...


0

However, now I am faced with the dilemma of which methods I should put on the interface and which methods I should put on the concrete implementation" directly. Is there a rule of thumb for this? Split the difference and create an abstract class.


1

If one thread calls Add, and another thread calls Contains, at exactly the same time, and everything is implemented correctly, then Contains will return either the value that was correct before calling Add, or the value that was correct after calling Add. That's the best that we can expect. If you can implement both the Add and the Contains method in a way ...


5

You should choose between init in declaration or constructor, not both. In a language that has a default constructor where you don't have to explicitly provide a constructor if the default is all you need, a declaration init may be fine. As soon as you need an explicit constructor with parameter, I tend to move everything to constructor(s), to ensure that ...


2

The interface should be as generic as possible. So, while this is an interface of Cars, if we assume that any car will be capable of having an owner, then the method for getting the owner should be inside the interface. However, any non-generic method, e.g. the method for taking the model of the Car's TV should not be inside the interface, since not all ...


0

If you are concerned about performance, and have more than a few shape-like objects on the screen, you cannot use controls. You need a library that provides mouse events and paints the objects for you, all in a single control. You can create your own, or use some pre-built ones. The flow-chart itself will need specialized layout routines.


1

if space is not of concern and your application can afford to use as many space as you want, I'm thinking of 3-dimensional array (or to be more precise, 2-dimensional array of list) structure 2-dimensional array will represent the screen. Each pixel is an element in the array. Each location in array contains a pointer to a list of shape identifier e.g. ...


0

I would say no. A singleton is rarely the answer. If you dont believe do a quick search on singleton anti pattern. The main problem being that it is hard to unit test code which uses a singleton. A much more flexible approach is the inversion of control pattern. Basically you would would just make the class that would use your cache depend a IDataPoint ...


1

You can use Memory Cache. I use memory cache to cache data rarely changes or at the specific time. Here is example: public static T GetCache<T>(string key, Func<T> initializer) where T : new() { if (!MemoryCache.Default.Contains(key)) { try { T data = initializer(); AddData(key, data); return ...


4

Addition of many elements to the end is O(m + logn), where m is the number of elements to be added. These operations would be O(n + m), and if m = n then they are almost as fast as addition to the end. In practice, they would be many orders of magnitude faster than if the user implemented them naively. However, for small numbers of elements, they would ...


0

I would use code generation from single source. E.g. define error codes in a text file and use T4 to generate C# enum and something similar to generate python code. Or write C# enums directly and use code generation to generate python only. You dont have to parse C# code necessarily, you can use some .net tool that dynamically loads your .net assembly and ...


2

It's a bit of a code smell to me that you have a constant that you want to share polymorphically between two classes so that they can define it on their own. Constants by design have been made to be unaffected by polymorphism so I would suggest you turn that into an abstract property in your abstract base class. You could either do that or have a concrete ...


5

it takes away from the beauty of their .NET library That author needs a wake-up slap to the face. For any professional project, the goal is to make working software that people want to use. Any kind of misguided ideals about beauty come long after that. If that's their only reasoning, the author has lost all credibility. Being able to mark your classes ...


0

Use null. It doesn't clutter your stuff up with a null instance, it doesn't fool people who think they have an instance into realizing they don't have an instance well after the bug occurred, and it's simply much easier codewise to be able to use the same rules for all nullable things.


0

In general, you should avoid null where possible. If you really have an optional parameter/field, then it is ideomatic to make the instance nullable. If you have a struct where null means something special - something that is not "this instance does not exist" then it depends. If other parts of your code/api use null to mean this special thing, then just ...


1

Obviously WinForms does not natively support one design pattern over another - the one that might not work is MVVM because you cannot "bind" data to the view model and have it update the data directly. Otherwise - I would attempt WinForms with MVP - I've seen that done before - here's a link to look at https://winformsmvp.codeplex.com/


2

As always, it's useful to think about the pros and cons of each approach: Pro passing in SQL It's flexible. If you have a lot of different queries that are difficult to parameterize you won't end up with a ton of methods, or having to design around it. Anti passing in SQL It's generally a bad idea to present a larger interface than is necessary. The ...


0

I think the question needs to be split into two parts. Firstly, what is the role of the POCOs representing your database objects, and secondly, how best to structure repositories to deal with them. In answer to the first question, you seem to have chosen to map your POCOs directly to database objects- one class exactly represents on table, or one view. ...


3

Passing SQL into the repository is definitely not a good way to go about things. It violates the single responsibility principle because the client uses the repository and also tells it how to work instead of it using the repository and the repository knowing how to work itself.


0

You will probably want to expose it in an easily consumable format like a timestamp and let the consumer choose how to interpret it. Something else that might help is to assume that this is what your API returns and attempt to code to it in a couple different use cases. If it feels messy, try returning the data in a different format and see if it becomes ...


0

As others have said, you should return only a single representation of each value. It's just simpler and easier for everyone. How to format those values for display is up to the presentation logic, not the service logic. If formatting a dateTime value or padding an integer with zeros is too hard for the presentation logic to do on its own, something is ...


1

Is your library a weather api that uses existing web services to get data? In that case you should probably hide the details of the actual ws, and define your own set of attributes for weather data. That way you encapsulate the external dependency from the users of the api. You may also want to watch how you get data from the xml. Try to only bind your ...


2

To add more classes, just create additional files. You may have noticed that you have not one, but two classes: Form1 and Program: the last one corresponds to the Program.cs at the bottom on your screenshot. In order to use those additional classes from your form: Either initialize an instance of the class within the form. If it shares the same namespace ...


0

I have a few pointers but overall I agree with your design and I will elaborate more on why. The very first thing I would say is - don't pass your queries to your repository. You shouldn't tell the repository how to do its job. I know that you can reduce the amount of repositories if the client calling the generic repository just passes in the correct ...


1

If the purpose of your API is just to provide data (which is true in my opinion), then do not return redundant or formatted values. In your example, returning single date/time is enough. This makes your response more concise and less ambitious. Making assumptions about consumer GUI and interpretation of provided data is usually wrong approach, avoid.


2

I would follow the YAGNI principle and carefully think through what the client needs on his/her side. Including things that you think the client is going to need is a symptom of a core problem - that being lack of research and/or knowledge what the client actually needs.


1

How about something like this? private static readonly object _syncRoot = new object(); [HttpPost] public void Post(InstagramUpdate instagramUpdate) { var subscriptionId = instagramUpdate.SubscriptionId; var lastUpdate = GetLastUpdate(subscriptionId); // To avoid breaking my Instagram request limit, do not fetch new images too often. if ...


0

This answer is based on the additional information about the use case mentioned in the comments. We have a web service the provides graph nodes. We hold a local copy of part of the graph and expose and async API to query the graph. If the area that you query is in the cache then we simply calculate and return the result, otherwise we start ...


1

Two things could help you there: Instead of Use Cases, User Stories. Why? Well User Stories are written for/by the user. They are not an accurate description of functionality, but do specify what the user wants to do. This can be your initial road map into writing code that actually allows the user to do something, rather than comply with system ...


0

A test fundamentally tells you, "If I supply input X to program P, I get output Y." Test driven development works by writing X and Y first then filling in the gap. Work backwards. The program will produce some outputs. Whether these are control signals, images, web pages, text, lamps, toast, whatever: outputs. Produce some representative outputs by hand. ...


1

First try to manipulate your data source to expose data in a way that matches your GUI better. Then you won't need as many converters in the first place. In the MVVM pattern the ViewModel is a representation of the Model which is better suited for binding by the view. Then I would say try to create data-centric converters, where the converters are designed ...


0

I solved this problem by writing a general purpose multiconverter that can take expressions and evaluate them, since it's a multibinding, it will update the value whenever one of the sources is updated, it also means I rarely have to write new converters at all. The only expense is the xaml verbosity. One example of an expression: <CheckBox ...


1

I have another suggestion for the mechanism: when an update arrives, mark a subscription as having to update, and wait for your delay before making the update. That is, there is no need for a cache, you only need to store some state for each subscription id as "having to update" or not. I will use async because it makes the code pretty straightforward. For ...


3

You cannot jump directly from "idea" to "implementation". Good example is the "V model" : You start at high level go lower and you write tests on each level. And each level gets more specific in both implementation and testing. For example you write an acceptance test that says that you should be able to add a customer. This results in you writing an ...


-1

Create virtual methods and properties in your default implementation objects and inherit and override as needed, interfaces could play a role here incase you need to completely overhaul the default object to a customers need. Dependency injection is the way to go configuration wise. I have used TinyIoC and other great projects such as NancyFX use it as well. ...


5

The size of a struct refers to the struct's fields. Automatic Properties include a field as part of their compiled structure; this field counts towards the size of the struct. Otherwise, properties are no different from functions - they are not part of the struct's instance at all, since there's nothing particular to that instance in the property.


0

Writing use cases with (say a Product owner) could perhaps get you started. The problem for me is that "conception" in your graph and unit tests are on different conceptual levels. Unit tests are on a very low level (and are best viewed as a code design tool), they do not dictate or guarantee system level features. I would start by writing automated high ...


0

What would be best is to have a mixin, but unfortunately C# doesn't include this concept. In the absence of mixins, I'd pick an interface over an abstract class almost every time, and definitely in this case. Whatever functionality has to be offered to derived classes, it can be offered in form of helper objects and utility functions. Favoring composition ...


1

Nonce is used for just that it's a number, possibly passed in the message header and it can be only used once and the subsequent requests with the same number are rejected by the serve. For details on how the value is determined please have a look at the article.


0

Presuming the "hash phrase" is client-unique, your integration already entails a level of statefulness. It's not much more of a leap to provide the client with unique request tokens. The server can issue replacement token(s) as part of the response for each request. The unique request tokens can either be/replace the "hash phrase" itself, or simply operate ...


3

Yes, Solr supports out-of-the box (well, after a bit of configuration, see the examples from version 4.9 onwards) PDF and Word documents. The thing to note is that Solr != Lucene. Solr is a higher level abstraction over Lucene, and as such it has a different API, features and behaviour. IMHO, the difference between Solr and Lucene utilisation can briefly ...


3

Why is an anemic domain model considered bad in C#/OOP, but very important in F#/FP? Your question has a big problem that will limit the utility of the answers you get: you are implying/assuming that F# and FP are similar. FP is a huge family of languages including symbolic term rewriting, dynamic and static. Even among statically-typed FP languages ...


0

You should have both the interface and a class with a default implementation. For example, if you have an IDog interface, it could have a Bark() method defined. A separate Voicebox class could provide the default implementation. This could look like this: public interface IDog { public void Bark(); } public class VoiceBox { public void Bark() { ...


1

A common use for events is in form controls. A form control can have a lot of events, a ListView for example has 79 different events. Having to create a delegate list for every event would mean that there would be a lot of those. With just a dozen controls there would be around a thousand delegate lists to create, but most of those would end up unused and ...


1

All delegates are effectively lists. The empty list is null, but you can still append to it with +=. The need to check for null before calling is very inconvenient, yes. But does not occur because they are not lists! It occurs because of how the empty list is represented. However you can avoid this by initialising the event: public event EventType MyEvent ...


2

As usual: If you need to implement some methods, the interface is not a solution. You have to use an abstract class. On the other hand, if you need to describe the behavior, especially in a context where classes which have this behavior might already have a parent class, you have to use an interface, since in C#, a class cannot have multiple parents. If ...


2

This is not an answer, but a long comment. I voted for closing as "too broad". This comment explains my rationale. At the end of this comment I have some suggestion, but it is out-of-topic. Unfortunately the question is too broad to be answered here. There are decades of research on algorithm human facial expression recognition. The "public ...


2

In this answer I will focus on the thinking part and less on the coding part. In other words, if the thinking is not correct, the code will probably not give the result you expected, even if the code implements your thinking faithfully. I will just point out the flaw in the code part, without going into detail: listOfPossibleTeams contained all possible ...


4

You can trivially define an extension method for ToHashSet if that's what you're looking for. public static class Extensions { public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source) { return new HashSet<T>(source); } } You should now be able to use it in your original example: var result = ...



Top 50 recent answers are included