You May Have Never Learned This Lesson Right

preview_player
Показать описание
Ever since extension methods were added to C#, programmers have struggled to figure out the proper use for them and their purpose in software design.
There are three principal roles where extension methods come in handy:
1. In metaprogramming - we extend a type without modifying it. That lets us add orthogonal concerns to classes we develop and even to foreign types we have no control over. How wonderful! It is a pity that this idea brings a whole pile of novel problems we didn't have before.
2. In functional extensions - we add pure functions that operate on models, typically generically, so that they apply to any model. We can seamlessly extend the domain model in ways never conceived during their design. How wonderful! But beware: this approach requires implementation purity and interface generality. That is a formidable combination of goals if you plan to achieve them.
3. When attempting to combine models, especially object-oriented and functional ones - that is the programming model that modern C# advocates. You can implement features that apply to specific models or even to a larger class of models if the method is generic. By systematically using extension methods, you can achieve great interoperability of models in a complex application.
Watch this video to learn how to utilize extension methods in domain modeling effectively. That skill will pay off manifold once you encounter a difficult situation with many responsibilities pertaining to a set of models. Knowing which responsibilities fit extension methods and which should remain as instance-level methods is sometimes the decisive factor in either developing a successful or failing to do so.

⭐ Learn more from video courses:
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⚡️Chapters:

⌚ 00:00 Intro
⌚ 02:11 Metaprogramming extension methods
⌚ 04:37 Functional extension methods
⌚ 08:54 Functional extensions with OO model
⌚ 12:01 Conclusion
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⭐ 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 authorized 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 #functionalprogramming
Рекомендации по теме
Комментарии
Автор

Just a detail, not related to the main point of the video:

If `CompareTo` returns `int.MinValue`, negating it results in `int.MinValue` again.

That's why I prefer inverting the arguments instead of negating the result when reversing the sort direction:

But anyway, most `CompareTo` implementations don't return `int.MinValue`, so this is not a big deal.

vyrp
Автор

12:20 is key bit. I probably over use them but love them. I get a bit frustrated at times having to add packages to give me more methods and namespace conflicts arise. The main ‘must have packages’ handle this differently.

coderider
Автор

I used to hate extension methods when they came out. Thought of them as just a glorified static methods, and static was the enemy. But with time learned to enjoy the way they make the code more readable. I think i am over using them now.

yanivrubin
Автор

Build Your Own Option Type in C# and Use It Like a

zoran-horvat
Автор

I just don't catch where the functional code in the Smallest function is. IComparer<T> is an interface and thus "comparer" appears as an instance object and that is not functional at all?! Also, the sorting is a pretty weak example, as it can be replaced with built-in functionality....

var list = new int[] {7, 9, 2, 6, 8, 1, 4, 5, 0, 3}; //etc.


var top3 = list.Pick ( 3, (a, b) => (a < b) ? -1 :(a == b) ? 0 :1 );

public static class SortingExtensions
{
public static IEnumerable<T> Pick <T> (this T[] list, int count, Comparison<T> comparison)
{
var sorted = list.ToList();
sorted.Sort (comparison);

return (count <= sorted.Count)
? sorted.Take(count)
: sorted;
}
}

thomasschroter
Автор

this is something i'm experiencing now in my third year as a programmer, before it was learning different technologies and actually complete projects, regardless of how much were they put togheter with tape. Now it's expecially the code i write, sometimes things being null when they shouldn't, checking for null and throwing exception because a method might return null in other cases but in this one it shouldn't! Then those extensions methods "ToDto" or putting them directly in the model class. Well let's try owned entities in EF Core! lol gotta be carefull to update them with reference when setting current values. What about complicated entities that are used in many places, should the sorting be done by the viewmodel on the Ui or always be the same in the model to ensure invariance?

In other words your videos are the only ones online helping me to clear this confusion.

sandromagalli
Автор

Where do extension methods belong when working in a clean architecture/vertical slice architecture?
Great video!

silop
Автор

My colleagues hate using extension methods because it is within static class, hence you cannot mock it... However, when the extension is put on the object that is mockable, that shouldn't be a problem, right? Professor Zoran I love functional programming, but my colleagues and company culture thinks we need to keep things as simple as possible, so when junior programmers come in, they wont have to learn all the functional secrets that we left behind. What us your thought on this??

jonasng
Автор

why the properties FirstName and LastName of the model class Person are private?

nickw
Автор

I can't agree that complicating code for the sake of some imaginary biblical rules is justified.

Especially in the second example where for just avoiding one if statement you clime the mount Everest and return back. I don't think that this is lighter on resource expenses and speed either.

How in the world if statement breaks the whatever rule ? Except that you invented that rule and then you say it breaks it.

mocococo
Автор

LINQ is quite remarkable, but I avoid it like crime because of the horrible performance it has.

anm