Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Hot answers tagged

8

A long running algorithm certainly could use some method of indicating it's progress. But it would be inappropriate for the algorithm to know that it's talking to a progress bar. The algorithm should be able to talk to adapters that can take what the algorithm says about it's progress and turn it into a progress bar, a log file, or noise over an audio ...


7

It depends on your intentions with the CheckWhiteSpace method: If CheckWhiteSpace is intended to become a reusable library function, placed in a class different from the one where AddPersonToPeopleList belongs to, then a unit test for it makes sense. The fact CheckWhiteSpace is public may be an indicator for this. If CheckWhiteSpace is currently not reused ...


7

Constructors should not do work. The initialization of a new object should happen very quickly, and making database calls, or interacting with any resource outside of the current process, can take considerably longer. Instead, your constructor should either require the data from the database as separate arguments, so that the database calls are made before ...


7

C#'s Task is somewhere halfway between Java's Future and CompletableFuture. The Result property is equivalent to calling get(), ContinueWith() does the things the massive array of continuation functions on CompletableFuture does (add some Task.WhenAny and Task.WhenAll in there). But complete(T) has no equivalent (use a TaskCompletionSource), nor does cancel()...


5

One of the most powerful design techniques is to pay attention to what you know when. Following this can help keep your design simple. You might be setting your dependencies in the wrong place. ArrayExporter.Export() depends on both string[] arrayData and string outputPath. But which do you know when? If you don't know outputPath until you know the rest: ...


4

why isn't it automatic and behind the scenes. This would work as long as there is only one action for one property of one component is involved, but as soon as there are more actions, maybe actions which are dependent on each other, one would have a high risk of running into race conditions. The explicit "Invoke" mechanics works like a transactional bracket ...


4

This looks like a good use case for the Adapter pattern. You can change the class KeyValueAccessibleType to an interface: public interface IKeyValueAccessibleType { string GetKey(); // ... } Then, you can have one concrete implementation per your preexisting framework classes: public class EnvironmentVariableKeyValueAccessibleType : ...


3

Is this a sane approach to this problem? It depends. For many situations, this is sufficient and perfectly ok. If you don't create a new type of exporter every two weeks, or don't want to provide an extensible black-box library for dealing with exporter classes, it is probably better to keep things simple and live with a little bit of "duct tape". It ...


3

As @1201ProgramAlarm commented, you have to consider DST for precision since the offset changes along the year. And from year to year since the saving time change doesn't occur every year on the same days. Take Madrid's time zone and its DST for 2019. If the user specifies the offset UTC+2, we have to consider the possibility to allow the user to specify ...


3

Yes, you wrote the code so it needs a test. If you are arguing that it's too trivial to test then its equally too trivial to require the extra function, bundle it in with where ever its called. You are bound to have Prince or Madonna sign up to your service and then you will thank me for making your write this test! Here's How I would code it: Notice I ...


3

As @Ewan mentioned his comment, you need Repository Layer and Service Layer. I also prefer adding Facade Layer in Service Layer. By this way, you can begin a transaction and can call kind of methods even they are not related. For example, you have UserService and CompanyService. Normally, there is not required to create User and Company same time but you ...


3

Do I need to unit test a generic method with all accepted types? It depends. The types accepted by a generic method are just as much an input parameter as the values that you pass as arguments and the same considerations for testing apply. This means that if it is really important that your function works correctly for certain types, you should test with ...


3

Some things to think about: Creating the IoC container need not be the first thing you do. In fact, reading configuration should generally happen first, partially because other aspects of the IoC configuration might depend on that too. For example, maybe your configuration lets you switch between completely different data sources, that therefore require ...


3

Is there a unique android ID question gives a good overview of the options to get a unique id for android. Best practices for Unique Identifiers recommends using instanceID for most use cases. See IdentifierForVendor for iOS products. Once you have an ID, hash it and send the hash to the webapi over a secure connection. This does not protect against rooted ...


2

Your question is very broad, so expect only to get a broad answer. The general strategy for solving these kinds of problems is to make the requirements customizable. This can be done by introducing run time or compile time switches, configuration files, special code which is only used for a specific customer, "plugins", and so on. From what you wrote, in ...


2

You do not need one repository per table. A repository can encapsulate all data access required for a given entity. The record in the users table is associated with a record in the user types table by way of a foreign key. You can model this in C# as well by replacing the User.FK_UserType property with an actual UserType object. The UserRepositoryDal (which "...


2

Conflicts: without implementing another layer of repository which returns Aggregate we cannot even >partly resolve the abovementioned pitfalls by implementing an extra layer of repository we are ignoring the built-in features of EF (every DbSet is already a repo) and over-complicating the app I have used an approach where every Aggregate gets ...


2

You can refactor your method to return a Response object and use a custom deconstructor. Like this: public class Response<T>{ public T Value{ get; set;} public HttpStatusCode Status{ get; set;} public ErrorResponse Error{ get; set;} public void Deconstruct(out T value, out HttpStatusCode status){ value = Value; status = ...


2

If your program wants to update the UI from a non-UI thread, the application framework has two choices: block the non-UI thread until the UI thread is waiting for events, then lock out the UI thread until the non-UI thread has done its update. let the non-UI thread specify the update as a deferred action, i.e. some code that can be executed by the UI thread ...


2

If you've been doing TDD properly, and this is method is something that you've created during refactoring, then no - as this is already covered by tests at the appropriate level of abstraction. Refactoring is rearranging the internals without changing the functionality - and without changing tests, that act as a safety net for you. You have to make a design ...


2

So why create a nested class? I can think of couple of important reasons: 1. Enable encapsulation Many times nested classes are implementation details of the class. Users of the main class should not have to care about their existence. You should be able to change them at will without requiring the users of the main class to change their code. 2. Avoid ...


2

Breaking down the problem What you've done right is identified that all exporters work the same. As a basic example (which I will expand on), this means something along the lines of: IExporter exporter = GetWhateverExporterYouWant(); exporter.Export("C:\\output.txt"); But you've also noticed that the input value (or rather its type) changes based on the ...


1

If we use your method, this is actually very easy to test. You have an interface that exports database and arrays. Now add a test export class that has a simple mocking implementation that implements the interface so you could use it for tests. Also, could you explain a little more why this makes it hard to test? Also, maybe the export interface is not a ...


1

When you instantiate the object CreditCardApplicationEvaluator, it needs to be ready to evaluate applications. We must think of the failure case where you can't pull this data from the database. So if you put the db call for those constants in the constructor, it might never return, so you will never instantiate your object or it will fail. Also another ...


1

In your particular example: First: You need to add test cases for "FirstName" and "LastName" being null or white space " ". Second: No, don't test logic which already tested through other tests. If you are using CheckWhiteSpace somewhere else in your application, you probably are having tests which covers logic of CheckWhiteSpace as well. Test ...


1

For thread safety: the threading apartment used by WPF controls is STA, which only allows the thread which created the components to modify them. This is done to increase thread safety on objects that are not inheritly thread safe. Changing something on a background thread might indicate a threading error on behalf of the programmer, hence the exception and ...


1

YAGNI principle: It's up to looking at your own codebase to see whether you would need to use this validation elsewhere. If you do then it's reasonable to refactor out the method and then write a specific set of tests for it. It looks like you are defining the helper method in the same class as your AddPersonToPeopleList class. If you want to explicitly ...


1

This is how I have approached this problem. There are a few points I find important. Possibility to subdivide the progress into parts. It seem fairly common to either have separate phases in the work, or that each work item takes a considerable amount of time, so each work item should also report progress. I do not want to worry about the update frequency ...


1

One approach is Application requests a license key at startup, attach either a random or hardware based id and the username to the request. The server finds the appropriate key and marks it as used, with the id and other relevant data. This can be done by using a separate 'used licenses' table or by updating the licese key row. Application sends a keep ...


1

What is a useful/recommended approach to addressing this situation? That depends on how set in stone the statement, "20-20 hindsight is a wonderful thing but unfortunately doesn't provide a simple way forward" is. If you are using semantic versioning and you have a good set of automated tests covering your back when making changes, then it may well be a way ...


Only top voted, non community-wiki answers of a minimum length are eligible