You Will Add Smart Constructor to Your Rich Domain Models When You See This!

preview_player
Показать описание
Have you noticed the limitations of regular class constructors? They are indeed limiting class design in more than one way! This tutorial delves into Smart Constructor - an advanced pattern that addresses the constraints of traditional class constructors, thereby enabling more flexible domain modeling.
The tutorial builds on nullable reference types in C#, a critical element in enhancing constructor robustness. By combining nullable references with the Factory Method and Null Object patterns, we will present a refined approach to deep domain modeling, which helps handle special cases elegantly, while enhancing overall class implementation.
In essence, this tutorial aims to empower you to create classes that are robust, adaptable, and maintainable.
By leveraging Smart Constructors and related concepts, you'll add more efficiency and reliability to your object-oriented programming and domain modeling practices in C#.

Thank you so much for watching! Please like, comment & share this video as it helps me a ton!! Don't forget to subscribe to my channel for more amazing videos and make sure to hit the bell icon to never miss any updates.🔥❤️

⭐ Learn more from video courses:

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⭐ CONNECT WITH ME 📱👨

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
👨 About Me 👨
Hi, I’m Zoran, I have more than 20 years of experience as a software developer, architect, team lead, and more. I have been programming in C# since its inception in the early 2000s. Since 2017 I have started publishing professional video courses at Pluralsight and Udemy and by this point, there are over 100 hours of the highest-quality videos you can watch on those platforms. On my YouTube channel, you can find shorter video forms focused on clarifying practical issues in coding, design, and architecture of .NET applications.❤️
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⚡️RIGHT NOTICE:
The Copyright Laws of the United States recognize a “fair use” of copyrighted content. Section 107 of the U.S. Copyright Act states: “Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrighted work, including such use by reproduction in copies or phono records or by any other means specified by that section, for purposes such as criticism, comment, news reporting, teaching (including multiple copies for classroom use), scholarship, or research, is not an infringement of copyright." This video and our youtube channel, in general, may contain certain copyrighted works that were not specifically authorised to be used by the copyright holder(s), but which we believe in good faith are protected by federal law and the Fair use doctrine for one or more of the reasons noted above.

#csharp #dotnet #vscode
Рекомендации по теме
Комментарии
Автор

It's hard to explain how much I love your videos 😁😁

MilanJovanovicTech
Автор

My “smart” constructors return Result<> ti encapsulate business rules violations, and throw exceptions when the invariant is violated by a corruption (db for example). This approach, as you can guess, enable the beauty of functional composition and the clean propagation of the logic errors from the very deep of the domain.
I’m very interested on your opinion.
I really enjoy your contents.
Bravo

GiovanniCostagliola
Автор

The whole time I was thinking about how I'd use an Option type instead of null

Glad to see you mention it at the end, gives me more confidence in my decision making

asdasddas
Автор

The only thing that I'd like to add is to consider moving this smart constructor into a standalone factory class. Money object with the smart constructor feels like it's having too many responsibilities and one red flag is that it knows about other implementations of IMoney. If you extract the smart constructor logic in the separate class, it will know about concrete implementations, which is fine, it will only ever do one thing (create instances IMoney). So it will be small, concise, very easy to unit test and will respect single responsibility principle. Last, but not least, if we ever expand IMoney with new implementation (let's say Debt, where amount does go in negative value), to me it makes more sense to change the MoneyFactory, rather than Money.

ms
Автор

Awesome video as always

7:52 As a note, using the null-forgiving operator ('!') can lead to confusing exceptions such as NullReferenceException in seemingly unrelated code.
I prefer to adopt the "fail fast" principle. If something that shouldn't be null is null, the developer should be warned immediately.

Which is why I always implement a simple extension method that asserts that an object isn't null and returns it:

public static T NotNull<T>([NotNull] this T? t, string? message = null)
{
Debug.Assert(t is not null, message);
return t;
}

Throwing a generic Exception() feels like a bad idea, an assertion makes more sense here since if t is null then it means there's a bug in the code.

cover
Автор

At 6:02 there is a bug in the Money constructor for lines 10 and 11. It is possible that Math.Round(amount, 2) causes Money to get set to 0 (for instance, amount == 0.001). In this instance, the check from line 10 will already be passed, but you get an invalid Money object.

jaredlash
Автор

Thank you so much Zoran. WDYT, if the TryCreate method will have the output parameter? In that case, we get the result (true or false) and the instance (out) to conform the Try... pattern.

AndriyBezkorovayny
Автор

4:12 how do you hide the default constructor, and how wouly you recommend proceeding on a record struct?

thygrrr
Автор

Thank you so much Zoran for sharing you knowledge and expertise. I definitely can recommend your courses to anyone who wants to improve their skills. I really enjoyed watching your course: Making Your C# 7 Code More Functional. I was wondering if we will see any F# courses from you?

seriyezh
Автор

5:51 Your three levels deep nested tertiary statement is terse but isn’t all that readable imho…

larsp
Автор

In what order should I take your UDEMY courses from start to finish?

codiore
Автор

Smart constructor can also be made async if that is what it takes to initialize an object

happy_burger
Автор

Maybe it's stupid question but why You do not return NoMoney instead of null in TryCreate example. Returning null force responsibility of handling this null from object to client. Even using Optional will move this responsiblity to client, I wondering if it a "good" approach/idea. Maybe I don't get the difference between Optional/Maybe vs. Null object pattern or do not understand something :)

Rafa-fpih
Автор

I never understood what makes constructors so much better than a factory method like `new()`, especially when you consider initializers. I don't even know when the last time was I wrote a constructor aside from making the default one private.

Ruhrpottpatriot
Автор

You can retrun Maybe<Money> or even Result<Money> and so avoid null reference.

dxs
Автор

Great video, thank you:-) One thing I've noticed in general is that exceptions often lose details which makes it harder to fix. So in this example, if somewhere on the call stack we catch an ArgumentException from Create, we don't know if it was the amount or the currency that is invalid.
If we wanted to keep that detail, you might say: if amount is invalid then throw new ArgumentException("Invalid amount") else if currency is invalid then throw new ArgumentException("Invalid currency") else TryCreate(amount, currency).
The drawback of this is that there is now duplicate code in Create and TryCreate. Is there a cleaner way to do this?

seandonohue
Автор

Are you dropping the usage of the Optional (Maybe) type altoghether in favor of nullable reference types? What do you think? Is is still worth using it?

SuperJMN
Автор

where can we get the source code of your awesome videos?

DavidSilwal
Автор

Great video!

Maybe it's just me but I find a bit hard to follow the nested ternary operators and I'm guessing you're doing it that way because you need it to use arrow sintax declaration everywhere is that right? So I have another question, is there any advantage on only using arrow sintax to declare methods/constructors?

ruekkart
Автор

Thanks, it's great video.

There is optional in DotnetNEXT Project, could you please explain how to use it and what is benefits of that wired implementation of optional

yousef.a.k