Why should we consider switching to NodaTime? So basically the main problem is not the construct itself but how developers are using it and that it is not powerful and meaningful enough. which in turn makes it hard to treat the data consistently. Both platforms provide far too few types to represent date and time values in a way which encourages the developer to really consider what kind of data they're dealing with. ![]() NET for the same reason that Joda Time exists for Java: the built-in libraries for handling dates and times are inadequate. ![]() If you are interested in more details, you should read the following blog post by Jon Skeet ( ). If you want to show the correct timestamp to the user according to his time zone setting, the C# DateTime construct would be enough for most of the scenarios, but it does not supply you with a nice and understandable interface. Also you might not know from where the client will access your application and you want to present the date and time in his local time zone. In my opinion it is one of the hardest tasks to get right, also due to the cloud, you do not know where your servers will be hosted and their local time settings. In this blog post I wanted to share some insights of how to make sure you get your time zones right in a globally distributed system. Let me give you a hint: Service Locator Pattern.As a developer you have probably had many discussions in your teams all around date and times. Noda Time: A better date and time API for.However, here are a few further readings for you on this subject: The reasons are variable depending on the context. I’m intentionally skipping why you shouldn’t use DateTime.Now directly inside your library (even, the whole DateTime API). F12 and try to figure out what it needs by looking at the implementation and good luck with mocking the static read-only members and DateTime.Now :) When you try to write a test against this class now, here is what it looks like if you are inside Visual Studio: For me, it’s horrible to see a C# code written like that. It relies on static instances which are hard and dirty to mock. It doesn’t have any constructor parameters for you to easily see what it needs. With the above static instance approach, however, you are on your own with this issue in my opinion. If you are using another editor which doesn’t have a tooling support to show you constructor parameters, you are still safe as you would get compile errors if you don’t supply any required parameters. When I try to construct the class inside a test method in Visual Studio, I can super easily see what I need to supply to make this class function the way I want. I Don’t Need to Look at the Implementation to See What It is Using Please note that this issue is not that big of a problem for a class which is this simple but I imagine that your component will have a few other methods, possible other interface implementations, private fields, etc. I have also no idea what type of environmental context it relies on. I have no idea what this class needs to function properly. Here how it looks like when I do that for this class: What is wrong with this approach? I’m not sure what’s the general idea for this but here are my reasons.įirst thing for me to do when I open up a C# code file inside visual studio is to press CTRL+M, O to get an idea about the component. ![]() With this approach, we are relying on static instances of the components (and they are possibly thread-safe and singleton). Instead of the above implementation, imagine that I have the following one: When I look at this class inside any editor, I can say that this class cannot function properly without IClock, ILogger and IEnvironmentInfo implementation instances as there is no other constructor (preferably, I would also do null checks for constructor parameters but I skipped that to keep the example clean). The Requirements of the Component is Exposed Clearly There are two things fundamental things why I would like this approach. Inside Visual Studio when I am writing tests: The way I see this class inside Visual Studio is as below: This is a simple class whose job is not important in our context. Public FooManager(IClock clock, ILogger logger, IEnvironmentInfo environmentInfo) Private readonly IEnvironmentInfo _environmentInfo
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |