The RIGHT way to deal with Date and Time in C#

Поділитися
Вставка
  • Опубліковано 15 жов 2024
  • Check out my courses: dometrain.com
    Become a Patreon and get source code access: / nickchapsas
    Hello everybody I'm Nick and in this video I will show you how you can use DateTime and DateTimeOffset correctly in your code, to make sure that it is testable. This is a technique that even Microsoft themselves are using and it is considered a best practice when dealing with time and date sensitive code.
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: bit.ly/ChapsasG...
    Follow me on Twitter: bit.ly/ChapsasT...
    Connect on LinkedIn: bit.ly/ChapsasL...
    Keep coding merch: keepcoding.shop
    #csharp #dotnet

КОМЕНТАРІ • 240

  • @birdegop
    @birdegop 2 роки тому +555

    I need that video about DateTime vs DateTimeOffset.

    • @a13w1
      @a13w1 2 роки тому +14

      Yeah so far not really seen DateTImeOffset used even in production code at various companies

    • @codex4048
      @codex4048 2 роки тому

      I need it for my colleagues, I keep coming across code that's based in Datetime and its really frustrating

    • @badwolf01
      @badwolf01 2 роки тому +6

      I'd like to see a video on DateTimeOffSet vs. DateTime as well. I get the basic definition, but don't understand why, for example, you are using it in this case instead of DateTime where only the local aspect is relevant.

    • @milos5247
      @milos5247 2 роки тому +4

      Well, DateTimeOffset is your only option if time zones are important, and most often they are.

    • @OhhCrapGuy
      @OhhCrapGuy 2 роки тому +13

      @@badwolf01 Any DateTime object that's not Kind == DateTimeKind.Utc should always be thought of simultaneously representing 38 different times, because you have no idea which time zone it's referring to, especially when you have a central database. Maybe you have a server app that's geo replicated in parts of Australia, India, Europe, US Eastern, and US Pacific time zones, with a central DB. Imagine one server updated a record with a local ModifiedDate of 2022-10-13 13:27:18.268, and another server updated a record with a local ModifiedDate of 2022-10-14 01:51:29.491. Which one happened first? You literally have no idea. Same time zone? First one happened 12.5 hours before the second one. First one happened in Central Australia and second one happened in Seattle? Then the "first" one happened 4 hours after the "second" one.
      Without an offset, every time you see a local DateTime, what you have is a range of 52 different possible times it might be referring to.
      Even if you aren't running in multiple time zones now, writing an application to not use offsets just means you're adding bugs that aren't affecting you *yet*.

  • @taipanprasithpongchai1465
    @taipanprasithpongchai1465 2 роки тому +222

    This is exactly what I have in my projects but instead of DateTimeOffset, I'm using DateTime. Looking forward to see the comparison between them.

    • @Max1weber
      @Max1weber 2 роки тому +1

      same here...

    • @DanielLiuzzi
      @DanielLiuzzi 2 роки тому +8

      Unless you were 200% sure your app will never, _ever_ deal with time zones, forget about DateTime and always use DateTimeOffset. This is one of those things that cost next to nothing to implement from the get go, but are a monumental PITA to do later.

    • @taipanprasithpongchai1465
      @taipanprasithpongchai1465 2 роки тому +2

      @@DanielLiuzzi even though i'm using datetime but i always send utc time to db. once it's retrieved the frontend (react) handles the local time convertion.

    • @jongeduard
      @jongeduard 2 роки тому +4

      I would like to learn more about that too! I generally use DateTime because when working with values of this kind, it always just souded much more natural than using DateTimeOffset everywhere, since the word "offset" suggests a difference between things (like TimeSpan or so), not something you would use by default.
      But note that when I am calculating time differences and such, I use DateTime.UtcNow and such. I would like to hear how DateTimeOffset gives me more certaincy of correctness with things that this.

    • @xxgn
      @xxgn 2 роки тому +4

      DateTime encoded with "local" or "unspecified" time zones not reliable, since they don't actually specify the timezone, only that it was local to whoever generated the DateTime object. Always using UtcNow can mitigate this, but it requires discipline. I guess you could throw an exception any time Kind is not set to UTC, but that's silly compared to just using a DateTimeOffset in the first place.
      DateTimeOffset always encodes both the time AND that time's offset from UTC (hence "Offset" in the name). When you are working with DateTimeOffsets passed around from arbitrary functions, you can be confident that you're not inadvertently interpreting the times in the wrong time zone offset.
      DateTime can make sense if you don't care about time zone (e.g., a 7AM alarm should go off at 7AM local time, even if that local time zone changes).

  • @vertigosoft
    @vertigosoft 2 роки тому +129

    Yes a video explaining the difference between DateTime and DateTimeOffset and Time and Timespan how Time zone is handled will be fine.

  • @johnny_rain3226
    @johnny_rain3226 2 роки тому +30

    As a senior C# developer, I must say, your videos are just awsome and professional.

    • @Velociapcior
      @Velociapcior 2 роки тому +1

      Yeah I love the performance ones. This one is for example more of a basic feature, but I saw too many times mids and senior fall in similar pitfalls

    • @rubenalvarez9094
      @rubenalvarez9094 2 роки тому +1

      @@Velociapcior I can testify of that as a mid developer..

    • @Velociapcior
      @Velociapcior 2 роки тому

      @@rubenalvarez9094 let whoever is blameless, cast the first stone

  • @xcastor3
    @xcastor3 2 роки тому +13

    In a company I was working for a few years ago we used NodaTime and it worked just fine. It's like the implementation you showed but much more complete. It's a nuget package created by the one and only John Skeet :D

  • @andrewborges4276
    @andrewborges4276 2 роки тому +10

    For sure want a video about DateTime vs DateTimeOffset. Good video as always!

  • @Zashxq
    @Zashxq 2 роки тому +5

    love the additional context of different teams working on .NET solving the same problem over and over. it really demonstrates how common this problem is, and how practical it is to be using the same solution in our own code.

  • @jcx-200
    @jcx-200 2 роки тому +1

    Can't express how handy this has been. Thanks for making the video.

  • @steveanon5284
    @steveanon5284 2 роки тому +3

    I would love a video on DateTime and DateTimeOffset and all the pitfalls developers commonly run into.

  • @davemasters
    @davemasters 2 роки тому +4

    Nice vid, I do the same & is what Mark Seeman advocated for years ago on his old MS blog. One thing I like to do is make it an optional param and new up the default if not provided. This way I don't have to bother adding anything to DI and only pass in an non-default when testing.

    • @martinpolansky9154
      @martinpolansky9154 2 роки тому

      Right. Ahother option would be just passing value to the function GenerateGreetText(DateTime).

  • @bbohac
    @bbohac 2 роки тому

    Such a small change, such a gain of testability.
    Great as always, thank you so much.
    And yes, please, please, please, show us, why it ist DateTimeOffset instead of just DateTime.

  • @tommaple
    @tommaple 2 роки тому

    I’m using this for years, although recently I started using the implementation with methods instead of properties-IMHO, it makes more sense, as it shows that we get (meaning calculate) this value each time instead of just reading the property. The property suggest that each time the returned value should be the same, unless the state of the object has changed.

  • @kelvin6444
    @kelvin6444 2 роки тому

    It's like you reading my mind, was having trouble with dates and time. Thanks for the video

  • @TheMathias95
    @TheMathias95 2 роки тому +7

    I've for a while struggled to wrap my mind around the differences between fake, mocks and stubs and when to use which one. Do you have any videos covering this in detail with examples, or do you plan on making one in the future?

    • @nickchapsas
      @nickchapsas  2 роки тому +14

      I do not but this is actually a great topic for a video. I’ll add it in the list

    • @TheMathias95
      @TheMathias95 2 роки тому

      @@nickchapsas
      Great to hear, looking forward to it then!

  • @Ubhaya1
    @Ubhaya1 2 роки тому +5

    @nick chapsas please make the video on the difference between datetime and datetimeoffset

  • @brunocandia703
    @brunocandia703 2 роки тому +4

    Nice video, It would be interesting to see an example of how DateTimeOffset should be used in a database table to track events and if that is not enough in some scenarios (future events) then how to use NodaTime.

  • @Sergio_Loureiro
    @Sergio_Loureiro 2 роки тому

    A very good example to show the advantages of D, Dependency inversion, from the SOLID principles.

  • @maurieezzz
    @maurieezzz 2 роки тому

    Yep we have had this discussion about the datetime va datetimeoffset in our company too we couldn’t see why in our code. So bring it on!

  • @rorysanchez
    @rorysanchez 2 роки тому +3

    Thanks for your videos Nick. I would very much like to know about DateTimeOffset. Also, is there any reason for you to use NSubstitute instead of Moq? Would you recommend one over the other for any particular use case or it is a matter of taste? Best regards!

  • @david3552
    @david3552 2 роки тому

    Thanks for your videos, watching you program reminds me of how joyous and orderly programming can be. I'd like a video on why use datetimeoffset vs Datetime

  • @GiovanniOrlandoi7
    @GiovanniOrlandoi7 2 роки тому

    Thanks for the video! Would definitely watch a DateTimeOffset vs DateTime comparison if you decided to make one

  • @jackkendall6420
    @jackkendall6420 2 роки тому +1

    I'm always interested in hearing knowledgeable people talk about dates and times in programming languages.

  • @snapching
    @snapching 2 роки тому +1

    I prefer a optional parameter using nullable datetimeoffset. If null, I default to system time. If used within the class, and scope of use is ok with a captured time at instanciation, then I can see this pattern as well

  • @stevehoff
    @stevehoff 2 роки тому +3

    Interesting. I find it simpler ( more simple?) to create an extension method on datetime/datetimepart. It's less code, just as testable and lends itself well to domain specific language.

  • @OddSimenMossik
    @OddSimenMossik 2 роки тому +1

    Hey Nick, thanks for speaking at NDC Oslo this year!

    • @nickchapsas
      @nickchapsas  2 роки тому +1

      Thanks for being there! 🙏

  • @victorchisomnwisu9776
    @victorchisomnwisu9776 2 роки тому +7

    How about passing the datetimeoffset as a parameter to the method but give it a default value of 'Now'?

    • @paulkoopmans4620
      @paulkoopmans4620 2 роки тому

      Your suggestion ONLY solves that you can unit test the method. The issue is that now on the, say 10 places, in your code you put 'now' there!. Imagine that the service is something else like a connection to an api or a database or something. The callers should not be worried about putting the connection string in as an argument.

    • @Denominus
      @Denominus 2 роки тому +3

      Underrated approach. You can do this with many things to turn an almost pure function (impure) into a pure function which is inherently unit testable.

    • @vasulko89
      @vasulko89 2 роки тому +1

      Good point, my first idea was also to use method parameter with default value. Just because we can introduce millions of interfaces doesn't mean we should do that.
      With parameter it takes seconds to understand what is going on inside the method.

    • @Ogix7
      @Ogix7 2 роки тому

      I also pass DateTime as a parameter to the Domain layer. It's much easier to write unit tests for business logic when there are no dependencies.

  • @Axemasta
    @Axemasta 2 роки тому

    I have a rule which I live by, I am incapable of writing time code. I will always get time code wrong when I eyeball it. I now make sure I TDD the time math first by writing all of my scenarios, draw up the method contract and implement the tests. Only then can I go in and write the code. It really helps to cut down on bugs that are just inevitably caused by datetimes!

  • @IAMAliJan
    @IAMAliJan 2 роки тому

    We faced this problem a few months ago, where our mssql stored procedures were using Date() function directly everywhere.

  • @Velociapcior
    @Velociapcior 2 роки тому

    Nick I would love DateTime vs DateTimeOffset video, but also I would love to see Nunit vs XUnit comparison. Personally I used both and I'm more keen towards nUnit, but I would love to see your opinion

  • @danielschmid8530
    @danielschmid8530 2 роки тому +1

    This is less about how to use Date and Time but how to do IOC/DI for unit testing. I was really looking forward to some tricks about timezone handling. :(

  • @tonyschoborg
    @tonyschoborg 2 роки тому

    I am also curious about the DateTime vs DateTimeOffset. Great video as always.

  • @carlcruz2803
    @carlcruz2803 2 роки тому

    Awesome video as always! But yeah, it’ll be great to have that DateTimeOffset video as well. Cheers

  • @bsod001
    @bsod001 2 роки тому

    As an alternative, I usually inject the IScheduler interface from Reactive. Which is actually pretty convenient, because there are lots of test helpers available for it.

  • @alexclark6777
    @alexclark6777 2 роки тому +2

    Nick, shouldn't that be 'public *sealed* class GreeterService' 😉
    Great vid, looking forward to the DateTimeOffset video!

  • @adriano.digiere
    @adriano.digiere 2 роки тому +2

    It would be great a video about DateTime vs DateTimeOffset

  • @stephenyork7318
    @stephenyork7318 2 роки тому

    I've been doing this for a few years now. Ultimately I decouple dependency to most things that are concrete so that I can make all my code testable. Didn't realised internally .NET had so many implementations of the same thing though. I'm all for standardising it, but then it's such a simple interface I think leaving it to people to do their own specific way isn't so bad.
    I'd love to see your take on DateTimeOffset vs DateTime

  • @alexpaskal8345
    @alexpaskal8345 2 роки тому

    that's a very good queestion, why you and MS team use DateTimeOffset, pls, make a video about it

  • @JuanManuelSaad
    @JuanManuelSaad 2 роки тому

    I would love to travel back in time and redo all my projects to use this simplistic approach! Please explain DateTime vs DateTimeOffset, I would love it too

  • @ThePeebl
    @ThePeebl 2 роки тому

    Great video as always Nick!
    Was also a pleasure meeting you at NDC (after the cruise) :)

  • @hodor2704
    @hodor2704 2 роки тому +1

    What confuses me about that Dates is timezones, especially when submitted on the browser and you handle them at the server. I don’t really know how they work or what strategy to place especially on a SaaS product that can be used in multiple timezones within the same organisation.

  • @coolmn786
    @coolmn786 2 роки тому +2

    Nice video Nick as usual!
    Quick question, you mentioned on the video you have unit and integration testing on your website. Have you also covered acceptance testing? I see you have a bundle for both but wondering if I should wait for the acceptance one so I can buy three at once

    • @nickchapsas
      @nickchapsas  2 роки тому +1

      I don’t have one on acceptance testing yet but it might come late last year

    • @sodreigor
      @sodreigor 2 роки тому +2

      @@nickchapsas did you mean, late "this" year? or "next" year perhaps?

  • @musacj
    @musacj 2 роки тому

    I really asked myself why not use DateTime, and I hope the future video will be recommended to me.

  • @bartekk689
    @bartekk689 2 роки тому

    Same as below! Please make a video on DateTime vs DateTimeOffset!

  • @sssidhu753
    @sssidhu753 2 роки тому +1

    What are your thoughts on using NodaTime package instead? I think using Date time has more problems. As it is ambiguous what the user wants to do.

  • @jamesbarrow
    @jamesbarrow 2 роки тому

    Do you have a video that deals with properly serializing & deserializing DateTime / DateTimeOffset, catering for timezones, daylight savings, etc.? This to me seems like a common issue that's not always clear too, unless you for example make use of NodaTime or similar. Would be nice to be able to just point people to a video for that.
    I remember having a test that was flaky because it was testing using dates, even using a date provider kind of setup, but then it still would fail because of timezones / daylight savings, because the developer machines were not in the same timezone as the build server machines for some reason, and so some assumptions were incorrect. In the unit test, I tried to have a setup step, or make use of a using statement so within the actual execution, it would make use of a specific UI culture to use specific date formatting options.

  • @aweklin
    @aweklin 2 роки тому

    Thanks Nick. I need the video on DateTime vs DateTimeOffset, please.

  • @justinian.erdmier
    @justinian.erdmier 2 роки тому

    I kind of understand why everyone uses DateTimeOffset, but I'm interested in how well it works with EF Core. I know SQL Server added a new DateTimeOffset type, but what about other databases?

  • @KibbleWhite
    @KibbleWhite 2 роки тому

    Totally would like to know why DateTimeOffset and not DateTime instead. I'm also using DateTime, however, I'm only ever dealing with UTC datetimes in the backend, and they get converted at the UI layer to whatever timezone that is requested by the present user.

  • @lucasteles42
    @lucasteles42 2 роки тому

    Did is a solid solution.
    In general in most cases we need just the DateTime.[Utc]Now, so I start to just register the DateTime on the DI and injecting it in the route/service

  • @antoniocogo1
    @antoniocogo1 2 роки тому

    You provided a full DateTimeOffset instance knowing very well that your SUT only needs the hour component.
    To me, that makes it look as if the whole instance is needed, as if the year 2022 that you set is actually relevant for the test, when it is not.
    What are your thoughts on only mocking the specific part you need, something along the lines of '_dateTimeProvider.Now.Hour.Returns(18)' (or something similar)?

  • @IanHorwill
    @IanHorwill 2 роки тому

    Would definitely be interested in DateTime vs DateTimeOffset. Also, where they fall down compared to NodaTime.

  • @Lashas83
    @Lashas83 2 роки тому +1

    I would suggest to use name IClock instead of IDateTimeProvider.

  • @LuigiTrabacchin
    @LuigiTrabacchin 2 роки тому +1

    I always do that, and i really feel there should be a standardized way in the framework. To overcome the fact there isn't I've made the smallest nugget package ever with only that interface ... But it's in our private nugget server...

  • @RikinPatel13
    @RikinPatel13 Рік тому

    Please make video for DateTime vs DateTimeOffset. In which usecase we must to use DateTimeOffset. I am too much confused, still I did not find any proper solution.

  • @sheglova
    @sheglova 2 роки тому

    Used to create an interface to do this, but simpler just to pass a func which returns a DateTimeOffset in most cases, unless you really need it for dependency injection.

  • @minnesotasteve
    @minnesotasteve 2 роки тому

    More people should be using DateTimeOffset, it’s even a data type in sql server. Makes life so much easier.

    • @Kirides
      @Kirides 2 роки тому

      Atleast as long as they only work with past time (now is basically past)
      For calendar like applications that won’t help much, and one would still have to grab timezone info and not just some random offset (timezones change, offsets are not bound to a specific timezone)

  • @davidjsutherland
    @davidjsutherland 2 роки тому

    I really want to know about the DateTimeOffset thing. I don't think I've ever used it so I'm super curious.

  • @ronaldschutte7948
    @ronaldschutte7948 2 роки тому

    Would like to see that video about DateTimeOffset as well. Especially in combination with Timespan, because I use it a lot for databinding with TimePicker controls.
    So what would be the best implementation to set up a time-bind TimePicker for instance with Mudblazor TimePicker with DataTime.Kind = Local on the frontend and a DateTime.Kind = UTC => DateTimeOffset for the backend?

  • @joseluiseiguren7827
    @joseluiseiguren7827 2 роки тому

    Can you make a vídeo explaining the difference between Datetime and DatetimeOffset?
    Thanks man!

  • @gavinreid9184
    @gavinreid9184 2 роки тому +1

    I prefer to lift the dependency to the calling code - genenerategreeting(datetime). I have used the interface strategy before but prefer a pure function until a pure function breaks down

    • @Pookzob
      @Pookzob 2 роки тому

      This is what I do to where possible. DateTime is a static dependency, those can't be mocked so basically you "inject" the value through an argument.
      Fancy way of saying "just pass it in".

    • @nickchapsas
      @nickchapsas  2 роки тому

      It doesn’t matter because something will have to provide that DateTime anyway, so this example who apply for that upstream method that had to set the value as a parameter

    • @Pookzob
      @Pookzob 2 роки тому

      @@nickchapsas Depends on which layer of the application you are in. I was implicitly thinking about the domain layer, where all dependencies are (should be) inverted.
      If your greeter took a datetime as input it could still be tested with ease since the dependency was inverted.
      The interface is a nice way of doing it, don't get me wrong, but it's also much more boilerplate than just taking DateTime(offset) as input. I know you do this as an example to get a point across, so good video non the less!
      Waiting for that offset video :)

  • @brechtlaitem
    @brechtlaitem 2 роки тому

    I'm always using the ISystemClock from the Microsoft.Extensions.Internal namespace. Works fine for me since this doesn't seems to be internal.

  • @Crozz22
    @Crozz22 2 роки тому +1

    Make a video on NodaTime which makes dealing with time a lot easier across the board

  • @nemanjamilosevic1594
    @nemanjamilosevic1594 2 роки тому

    What are your thoughts on NodaTime? I'm kind of leaning towards switching to NodaTime solely for testing purposes but after watching this video I'm not so sure about that anymore.

  • @qj0n
    @qj0n 2 роки тому

    I always struggled with built-in types as you have to be constantly councious if you work with local time or UTC etc.. All the issues were solved once I switched to NodaTime package, which has much more precise types + it's clock should be always injected from DI

  • @nanvlad
    @nanvlad 2 роки тому

    You can also inject IDatetimeProvider in the method if your serivice constructor is private/internal or use shims to fake the system time on test run

  • @holgermuller6701
    @holgermuller6701 2 роки тому

    Yes please make a Video about DateTime and DateTimeoffset

  • @GlassScissors
    @GlassScissors 2 роки тому

    DateTimeOffset is a very intriguing topic, please teach us :)

  • @Ristogod
    @Ristogod 2 роки тому

    Yes, make the DateTime vs DateTimeOffset video please. Thanks.

  • @ExpensivePizza
    @ExpensivePizza 2 роки тому

    Ironically, the last one you show in the video is public but it shows up in a namespace called "Microsoft.Extensions.Internal" 😂
    That's the one I normally use when I couldn't be bothered creating my own interface and I've already referenced the assembly. It's pretty lightwight.
    They even have documentation for it that says "Abstracts the system clock to facilitate testing."

  • @ArnonDanon
    @ArnonDanon 2 роки тому

    Glad to see i am not the only one mocking time for tests 😂
    Thanks for another great video Nick.

  • @andrzejwro
    @andrzejwro 2 роки тому

    DateTimeOffset vs DateTime would be also interesting topic

  • @GufNZ
    @GufNZ 2 роки тому +1

    Have you encountered the NodaTime package? It fixes *_all_* the issues around dealing with calendars, times and durations, as well as including a SystemClock like you do.

    • @nickchapsas
      @nickchapsas  2 роки тому +1

      I’m planning to make a video on NodaTime. This video is a bit of a segway

    • @JonSkeet
      @JonSkeet Рік тому

      @@nickchapsas Not sure whether this has happened yet - I can't find it if it has. Anyway, if it's still planned, I'd be happy to collaborate on it with you...

    • @nickchapsas
      @nickchapsas  Рік тому

      @@JonSkeet Hey Jon, it hasn't happened yet but I'd be happy to have you on a video to talk about it, especially given the new time stuff in the latest .NET versions

  • @hpkruger01
    @hpkruger01 2 роки тому

    Yeah. Video about datetime vs datetimeoffset might be good!

  • @brianm1864
    @brianm1864 2 роки тому

    I agree with others... would love to see a good explanation of why I should use DateTimeOffset instead of DateTime

  • @GufNZ
    @GufNZ 2 роки тому

    And while I'm mentioning other interesting packages, have you come across PowerAssert? It takes Expressions and annotates them completely if anything fails, making it obvious where the failure is coming from.

  • @cmtv357
    @cmtv357 2 роки тому

    What mocking framework are you using?

  • @PaulOwens
    @PaulOwens 2 роки тому +1

    Shouldn't we keep the GenerateGreetText functions "pure" by passing in the datetime? At the moment it can give different outputs to the same input.

    • @paulkoopmans4620
      @paulkoopmans4620 2 роки тому

      Huh? At the start of the example the method is indeed not pure as you have no control over the system clock dependency. However after making it depended on a IDateTimeProvider, whether or not that comes in through a constructor OR passed along as an argument on the method, does not matter. The GeerterService as a whole is pure now. There is NO way to have GenerateGreetText return different outputs anymore. Whether he calls in one time, a million times or adds a loop in the unit test that runs for 24 hours, the output will be the same.
      The downside of the suggestion where methods like these will get arguments is that now you tightly coupling the usage of it. You make all the callers now pass a value. You have just created a scenario where you end up with 12 DateTimeOffset.Now sprinkled through the code base.
      Imagine this would be a method that requires 4 other services it depends on. You would then put all of that 'knowledge' onto the callers. They would now have to get instances of these four dependencies, and all of the dependencies they need, etc. The whole point of dependency injection and decoupling your code .

    • @volodymyrliashenko1024
      @volodymyrliashenko1024 2 роки тому

      ​@@paulkoopmans4620 yes, but you still need to create DateTimeProvider everywhere where you want use this code. Of course DI will for you.
      I think there should be a balance between depending on DateTime as a parameter or as DateTimeProvider.

    • @paulkoopmans4620
      @paulkoopmans4620 2 роки тому

      @@volodymyrliashenko1024 but that is exactly the benefit of Nick's approach. Any sort of object that would want to have a GreetMessage would only have to get DI to give a GreetService instance. DONE! Me as a caller should not be bothered by putting an argument for a provider on my constructor.
      If it is something more substantial, say like a DBConnection, you would also not do that either! right? The idea is to take any impure functions/actions and abstract them away behind an interface. That way your method becomes pure. Whether or not you get it as an argument on the method or on a constructor doesn't really matter.

  • @nove1398
    @nove1398 2 роки тому

    Why are we using DateTimeOffset and not DateTime?

  • @goblelol
    @goblelol 2 роки тому

    I believe that was already covered in some of your's videos a bit of time ago, Nick :)

    • @nickchapsas
      @nickchapsas  2 роки тому

      It was covered as part of a different topic. I wanted to make a stand-alone video on it in case someone missed it

  • @limpalex
    @limpalex 2 роки тому

    What about Microsoft Fakes? Did you try to use it?

  • @xavier.xiques
    @xavier.xiques 2 роки тому

    I want that video about DateTime and DateTimeOffset please.

  • @kylekeenan3485
    @kylekeenan3485 Рік тому

    I just realised. Good morning, Good evening and Good afternoon can be used as greetings but Good Night is a only used as a valediction (goodbye).

  •  2 роки тому

    why do you prefer rider instead of vs ?

  • @AlexanderBaranski
    @AlexanderBaranski 2 роки тому

    first thought: when the greetertext depends on a datetime, make it an parameter in the function

    • @nickchapsas
      @nickchapsas  2 роки тому

      Ok, now unit test the thing that needs to pass down the DateTime to the function

  • @Ivolution091
    @Ivolution091 2 роки тому

    Keep teaching to help us keep coding!)

  • @watchchat
    @watchchat 2 роки тому

    +1 for the offsets video

  • @andreaskarz
    @andreaskarz Рік тому

    I don't like this over complexed constructions 😞
    I solve this with two constuctors in the 'GenerateGreetText' - one with a param DateTime and one without. And then in the constructor without parameter I get the DateTimeNow via constructor chaining to the constructor with parameter.
    You will have less code, you will see directly in the class what will happens - so it is much clearer and more maintainable.

  • @mariocamspam72
    @mariocamspam72 2 роки тому

    Interesting topics every time, how do you do it??

  • @charles_kuperus
    @charles_kuperus 2 роки тому

    I really would like to know the real difference between datetime and datetimeoffset

  • @zhebed1195
    @zhebed1195 2 роки тому

    Yes plz tell about Offset and datetime

  • @StigBrembo
    @StigBrembo 2 роки тому

    Yes, more videos on DateTime, offset and Noda. DateTime is one of the things, besides decimal and thousand separators and encoding/none latin chars, that "none US"-developers are spending too much time fixing in bad code.

  • @kiranfenrir1
    @kiranfenrir1 2 роки тому

    Need a DateTime vs. offset video

  • @mariumbegum7325
    @mariumbegum7325 2 роки тому

    Fantastic video

  • @edbutler3
    @edbutler3 2 роки тому +1

    Adding another vote for the DateTimeOffset video...

  • @viktorasmickunas2527
    @viktorasmickunas2527 2 роки тому

    For scenarios like this I just use NodaTime's IClock and IFakeClock

  • @leonardofranchinleite139
    @leonardofranchinleite139 2 роки тому

    I didn't know you could use switch return like that lol, it will be useful.

  • @sodreigor
    @sodreigor 2 роки тому

    I would like the DataTimeOffset video

  • @HelbaWavemaster
    @HelbaWavemaster 2 роки тому

    Need that offset video!

  • @orthodox-4-ever
    @orthodox-4-ever 2 роки тому

    I would like to see DateTime vs DateTimeOffset video

  • @henadzyermakavets120
    @henadzyermakavets120 2 роки тому

    Why not a function with "Date and Time" argument? :)

    • @nickchapsas
      @nickchapsas  2 роки тому +1

      Because it doesn’t matter at all. Something will need to pass the date time at some point and this technique applies there :)