Tag Info

New answers tagged

1

I do not think making decisions about if trial time was exhausted on client is a good idea. This can be easily fooled and can't be calculated with some precision. Since you have a web application, I guess, it would be much better to limit a number of API calls a trial user can make without payment. You can make some tests and map an average number of API ...


1

Beyond mocking the network interface, you don't need two physical machines for a system test: Disable/reenable the network card using scripts that do ifconfig/ipconfig. use a virtual machine for one end of the system and script it up/down or enable/disable the networking. Hooking into WinSock is way more work than you need to do here.


1

You test these by mocking the client and server and using IP Address 127.0.0.1 Depending on the test you are performing, it could be a simple matter of having the mock-server simply closing its end of the connection after some predefined time and then verifying that the client reconnected within the specified time-frame. It may be difficult to test some of ...


2

First, I think this is a perfectly good question. It's an interesting area and i don't get why was this downvoted. You don't need winsock you can disable the network a adapter and change it's settings programmatically... but network issues could be very tricky to simulate this way... ...


0

MVC is a separation of concerns, hence, your controllers should never be in your Views folder (and vice versa). If I were you, I'd simply create a folder "Library" and "Hostel" in your project root. Then add a "Controllers" and "Views" folder for each. I suppose both share the same Models, so I'd put these in a separate folder in the root. If you really ...


1

Generic answer: My gut feeling says if you have 30 different but very similar things, you should go one metalevel up, have one class for all of them and push the difference completely down to the data. Even if you end up with a nano VM / nano interpreter. Maybe during implementing it, you find out you really only have two or three kinds of them, ...


3

The other answers are quite good and clearly address the correctness concerns. Let me address your more general question: How much work should I place inside a lock statement? Let's start with the standard advice, that you allude to and delnan alludes to in the final paragraph of the accepted answer: Do as little work as possible while locking a ...


0

I would recommend putting the serialization and deserialization code in separate classes rather than the data model classes like it sounds like you are thinking. I'm solving a similar problem now and I have three distinct groups of classes. The first is the model for the messages that contain the various message fields. There's no validation or logic in ...


4

Sounds like you can cover this using straight-forward inheritance. Create a base class with the common implementation, and create subclasses that override that implementation with their own, unique behaviour. You can read up on MSDN here. EDIT: I figured I should elaborate with an example. Define a base class that implements the default behavior of your ...


4

In my purview, Object is undefinitive, and belongs in a binary format, not in a human-readable text. I don't see the relationship between these two things; there are plenty of good reasons to choose text versus binary: this is not one of them. How it appears is an implementation detail only. any derived type of Object The biggest challenge, then, ...


0

I pretty much agree with what @radarbob answer states. For some time I wondered as well, about how one would implement such a design pattern in "real life". It's fine when reading about it to understand the design pattern theoretically, but actually applying it is different. If you have access to Pluralsight I would highly recommend the following video ...


1

In most examples online however, this is exactly what's done Not sure how; neither WebRequest nor its concrete descendant HttpWebRequest do in fact implement IDisposable, so this using (System.Net.WebRequest wr = new System.Net.HttpWebRequest()) { //What? } does not compile. In fact what I find when I google around this topic is mostly ...


3

My question is then, how do I know specifically which classes need to be disposed? Does it implement IDisposable? Yes? Then it needs to be disposed. If not, not. You can maybe get away without it in some apps (C# and modern operating systems in general are better about cleaning up after you), but it is still wrong®. There are a very few framework ...


4

If you do not apply DI as long as you do not really need it (not even for unit testing), nothing bad will happen. The code does not become error prone, "overly complicated", or hard to maintain. And if you decide to refactor the dependency out later, it will most probably not be much more effort than doing it now. That's a case where the YAGNI principle ...


7

There's a development principle along the lines of DRY and SOLID called YAGNI that is designed to help streamline your development efforts in getting things done and not getting paralysed with indecision over what to do. If you later find that you need to enhance your class, then you will. YAGNI says not to worry so much over it now 'cos you probably won't ...


0

The answer like most things is "it depends". If you want to unit-test the functionality in doSomethingImportantUsingDependency, then you will need to be able to inject the dependency. However, if all that doSomethingImportantUsingDependency does is some property mapping from the result of your database call, then it would be pragmatic to not bother. If ...


9

In C# it is trivial to provide optional dependency injection without coupling yourself to your dependency too tightly: public class SomeOtherClass { private readonly ISomeClass _someClass; public SomeOtherClass(ISomeClass dependency = null) { _someClass = dependency ?? new SomeClass(); } } (or you can make the constructors explicit if ...


2

The general case here is identifying the external interface. It should be possible to write an interface for two systems or modules to interact, define what the method signatures are, data formats, preconditions/postconditions, etc. with zero implementation behind it. Next, separate teams work on separate code that use that interface. Perhaps one team ...


32

This is not a question of performance. It is first and foremost a question of correctness. If you have two lock statements, you can not guarantee atomicity for operations that are spread between them, or partially outside the lock statement. Tailored for the old version of your code, this means: Between the end of the while (_runningWorkers >= ...


11

IMHO you are asking the wrong question - you should not care so much about efficiency trade-offs, but more about correctness. The first variant makes sure _runningWorkers is only accessed during a lock, but it misses the case where _runningWorkers might be changed by another thread in the gap between the first lock and the second. Honestly, the code looks ...


1

I keep view models in the web project, for reason you stated, it's usually only useful to the relevant view. I'm not sure why your data access layer would reference the web project though?


1

This is a normal restriction in the Windows security model since NT3.x services that only the local service account is allowed access to the desktop when run as a service and it has other restrictions when you select that option. On Windows Server since about 2003, there is also a service interaction policy you may have to adjust. You may have to reboot the ...


1

The best way to manage threads is to not do it. In C# we achieve concurrency without direct thread management by virtue of async/await, or sometimes (as mentioned by others) lambda expressions, most commonly via PLINQ. It is expensive, slow and subject to availability to start and stop threads. Instead, the above approaches use a pool of preallocated ...


0

npinti provides a good example of some code, but in general what you are doing is a very common scenario. The server you are looking to make is a basic web service, often these days using a RESTful API model over HTTP. The client packs up some data in a predefined format of your choosing, and transmits its to a URL of your choosing on the server. The server ...


0

Don't. It's not a static class, and they aren't static methods, so there's no reason why it shouldn't be up to calling code to avoid concurrent calls. With static methods, then you would have to do some of the work to make it handle concurrent calls because calling code can't guarantee that it is the only calling code. And of course with instance methods ...


24

I am making a little program, which have few Threads, constantly running. At some point, I may want to stop one of them, and then, after random period of time, to start it again. What is the best way to create a thread? The best way is to not do it at all. If I have work that needs to be done in parallel to another program, the best practice is to start ...


3

With regards to the two ways of starting the thread, they are roughly equivalent, so it is a matter of whether you prefer a lambda. For stopping the thread, the most error proof way is to check a field value for a change. This can be done using a memory barrier, for example, using a lock statement or a Monitor will automatically provide a memory barrier. ...


0

You need to close the connection stream from your client. The server's while loop will not exit until the client closes the connection. As for your question regarding how will you know what is your client sending, you could use an initial series of bytes to denote what you are sending. So if you send 0x00, you are sending a file, if you send 0x01, you are ...


3

The web server is saving the data, not the user, so it doesn't matter what the user's time is. The object should be saved with a timestamp in GMT (universal) time format. Save the user's GMT timezone offset, Then add that offset to the timestamp (which is in UTC) to present to the user, his own local time. For example, since I'm in CA, my ...


2

Not so much. Maybe. The problem is that if Al from LA marks something with 1/1/2015 10AM and 7/1/2015 10AM, those will be different offsets from GMT. "What the user expects it to be" can get really dicey given our overly complex way of measuring times. Where at all possible, work using UTC (which may or may not be different from GMT depending on your ...


1

It looks like you are using your observer as both a publisher and a subscriber, while the Provider is only a publisher. Or is it both too? This seems a bit confused. Maybe you should make some kind of Bus or Queue that you can both Publish and Subscribe to. There are so many complexities in messaging that you really should make a distinct component be ...


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 ...


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 ...


1

A relatively simple and generic method to pass .NET objects over process boundaries is to use serialization. Make sure your DomainObject is serializable, then you can implement your Clone method by serializing your object to a memory stream, and deserialize it from there within you main component out-of-process. We used that in conjunction with memory ...


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

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 ...


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.



Top 50 recent answers are included