No video

Unleash Your Functional Modeling Superpowers | C# Tutorial

  Рет қаралды 15,081

Zoran Horvat

Zoran Horvat

Күн бұрын

Download the source code for this demo at Patreon: / unleash-your-84639661
How about creating a more concise and expressive rich domain model using C# and .NET? Let's face it: The C# programming language is changing. It is adopting functional programming paradigm, with many programmers facing difficulties to follow, some even saying: "This is not the C# I know!". Don't be one of them. Embrace functional design in C#!
This video will teach you some functional parts of the latest C# and how you can leverage them as part of effective domain modeling beyond the reach of the traditional object-oriented method.
Enter Functional Programming - a paradigm where applications are structured based on functions and immutable data. A concept seemingly overlooked in the past, it's been making waves in the .NET community for the last 15 years.
This tutorial demonstrates a practical shift from an Object-Oriented design to a functional model in an ASP.NET Core application. Here's a sneak peek: we introduce a new immutable model, which simplifies the process of producing and manipulating the domain objects in the application. What used to be a convoluted procedure in the object-oriented model, is now turned into a neat mapping expression thanks to powerful pattern matching constructs.
By the end of this demo, all functional elements of the latest C# specification fit into place: Record types, discriminated unions (based on C# records), type test and set expressions, C# 11 list patterns, immutable collections, and custom immutable classes. And behavior? Implementing behavior in the new, functional model, will reduce code by half, compared to the previous object-oriented version.
The future of C# is here, and it is functional!
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.🔥❤️
✅🔔 Become a patron ► / zoranhorvat
✅🔔 Subscribe ► / @zoran-horvat
⭐ Learn more from video courses:
Beginning Object-oriented Programming with C# ► codinghelmet.c...
⭐ Collections and Generics in C# ► codinghelmet.c...
⭐ Making Your C# Code More Object-oriented ► codinghelmet.c...
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⭐ CONNECT WITH ME 📱👨
🌐Become a patron ► / zoranhorvat
🌐Buy me a Coffee ► ko-fi.com/zora...
🗳 Pluralsight Courses ► codinghelmet.c...
📸 Udemy Courses ► codinghelmet.c...
📸 Join me on Twitter ► / zoranh75
🌐 Read my Articles ► codinghelmet.c...
📸 Join me on LinkedIn ► / zoran-horvat
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
👨 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 KZfaq 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.
⭐For copyright or any inquiries, please contact us at zh@codinghelmet.com

Пікірлер: 47
@jbaguetta
@jbaguetta Жыл бұрын
You are the best coding tutorial author I have ever seen and I have watched hundreds of people in my lifetime. Absolute incredible work. Microsoft should really make you a mvp.
@JeremyConnor
@JeremyConnor Жыл бұрын
Your example functional mapper using pattern matching is beautifully elegant.
@tristanstricker967
@tristanstricker967 10 ай бұрын
I think the best thing you bring to your tutorials is clarity. I always understand why you are doing something. Beautiful code always comes from clear thinking
@geesysbradbury3211
@geesysbradbury3211 Жыл бұрын
I really like your dry humor and beautiful code! You manage to capture and hold my attention by having exact the right pacing and level of detail. Most videos bore me after the first minute and I start skipping, not yours. If all teachers were like you, the world would be a smarter place. So, I forward your videos whenever I see one to all my friends (Who're also happen to be programmers)
@zoran-horvat
@zoran-horvat Жыл бұрын
I'm glad to hear you liked it! I have learned from comments that many people seek practical coding sessions, which is, lucky enough, the way I prefer doing things.
@user-tk2jy8xr8b
@user-tk2jy8xr8b Жыл бұрын
Pattern matching is a cool feature. I hope C# lang designers eventually add list pattern support for IEnumerable, even without spread bindings
@zoran-horvat
@zoran-horvat Жыл бұрын
That would be tricky to implement, but more importantly - the developer wouldn't know what will happen when that code executes. Though I see the benefits (e.g. no more MoveNext calls than necessary) I doubt they will go that far.
@shmupful
@shmupful Ай бұрын
Art of coding at its best
@GiovanniCostagliola
@GiovanniCostagliola Жыл бұрын
I really appreciate yuor contents and your promotion for a functional style in C# (even when you don't declare your goal in advance).
@iamacsel87
@iamacsel87 Жыл бұрын
Thank you for the great video. I love the immutability and lack of side effects of functional programming. While the declarative nature isn't especially important for me (except perhaps for libraries), it reminded me of method chaining and, at least in that regard, i see there are some overlaps between these concepts/patterns and implementations: - functional programming - method chaining (the same as piping?) - fluent interface pattern - builder pattern - extension methods I'm trying to see where each fits (what problem are they solving?), they're use case (something specific or more generic), smaller or bigger scope
@zoran-horvat
@zoran-horvat Жыл бұрын
FP is based on the separation between types and functions, so extension methods are a natural choice for the implementation in C#. FP is forcing each function to return an object, and then it is favoring function composition over large functions that do many things. That is the call chaining and fluent interface. The builder pattern is not common in FP. It is because every function is, in a way, a factory of some object. By chaining multiple calls, we can build a resulting object of significant complexity.
@MultiMohsinkhan
@MultiMohsinkhan Жыл бұрын
I pressed Like at 6:14, because your sharing always makes difference
@zoran-horvat
@zoran-horvat Жыл бұрын
I am glad you found it useful.
@vladislavzhuravlev6440
@vladislavzhuravlev6440 10 ай бұрын
Как Боженька пишет... Thank you, Zoran! It's very impressive!
@RichardONeil
@RichardONeil Жыл бұрын
Whoa...that was super!!!
@codingbloke
@codingbloke Жыл бұрын
Thanks again. Very clear. What would you say though to anyone (I'm not saying that's me...but it is me) who might be concerned about performance. I certainly prefer this declarative style but if, like me, developers have been trained to spot the memory allocations going in the code they are writing, it can be a bit unnerving. This is irrational I know, and I write code under the true-ism "It is easier to get working program to go faster, than it is to get a fast program working", but I'd like to know your thoughts on this?
@ivankorhner3403
@ivankorhner3403 Жыл бұрын
If you are concerned about performance, the first step is to measure it. Optimization should never be done without monitoring in place. So lets, say you measure performance of a REST api of a web application. You will soon discover that network, database and other I/O stuff take up >99% of the time. No matter what you do with plain code in this situation, be it for better or worse, impact will be minimal. So, performance price for readability is a great tradeoff there. If, on the other hand, you are designing a proper real time system, something like guiding a vehicle and responding to sensor data in realtime, you would probably discover that readability is not always justified, and that plain code will take up a much larger part of execution.
@zoran-horvat
@zoran-horvat Жыл бұрын
@@ivankorhner3403 That is precisely what I wanted to write, thank you. When speaking of a typical connected application nowadays, we notice that network latencies often account to dozens of milliseconds; querying a database takes milliseconds at best. At the same time, executing a function, mutable or immutable all the same, would still take nanoseconds. That can change in CPU-intensive operations, where little network and database communication causes lots of calculations. Those are the cases where we would usually favor a mutable design based on an algorithm with in-place writes.
@codingbloke
@codingbloke Жыл бұрын
​@@zoran-horvat @ivankorhner3403 Thanks that's exactly the answer I expected and is basically what I have used when discussing "premature optimisation". Very rational. Yet the irrational fear remains. 😱 I guess the solution is to put up with the discomfort. Make sure you are genuinely benefiting from this style (i.e. not fully committing to it has limited value). The reduction in bugs and maintenance effort will vastly exceed the cost of targeted optimisation in the unlikely event that it is genuinely needed.
@adambickford8720
@adambickford8720 Жыл бұрын
You're already on the spectrum of performance trade-offs; if performance is so critical, why isn't this app written in c or golang? I'd argue there should be an SLA, not just a hand wavy "its overhead so its bad" dogma.
@nicolascossio5961
@nicolascossio5961 4 ай бұрын
@@adambickford8720 C# is a perfectly capable language to make very performant programs (maybe not ultra-ultra performant but still), it offers memory efficient data structures and operations as options in the language but they have to be used, and more importantly be used properly
@lmao01
@lmao01 Жыл бұрын
I would love to see such functional c# propagator use all of their teachings in unity engine even though I know its a big ask.
@zoran-horvat
@zoran-horvat Жыл бұрын
In Unity, you usually chase different goals, most notably performance and reduction in number if allocations, lest causing frequent garbage collections that damages smooth rendering. Similar reasoning is valid in embedded development. Those are the niches where mutable design is more appealing than the immutable one.
@lmao01
@lmao01 Жыл бұрын
@@zoran-horvat Ok. Gotcha. Thank you for the answer ❤❤
@ivandrofly
@ivandrofly Жыл бұрын
Thanks
@aamirali8114
@aamirali8114 9 ай бұрын
Hi, want to learn more and in depth the programming you teach i am trying to go with Patron but unfortunately i am able to do payment transaction is there any other sites where you give learning materials like Patron.
@ReinkeDK
@ReinkeDK 10 ай бұрын
Excellent video :) Might you have a suggestion for a good book to read, to get into Functional Programming in C#?
@zoran-horvat
@zoran-horvat 10 ай бұрын
There is the new book by Simon Painter, Functional Programming with C#. I haven't read it myself, though I plan to, but I met Simon at one occasion and had a chance to hear about his views on programming. I'd expect nothing but the best learning from this book.
@ReinkeDK
@ReinkeDK 10 ай бұрын
@@zoran-horvat Thanks for the tip. Keep up your good work :)
@kave3299
@kave3299 Жыл бұрын
Sorry about my amateurish question. But if we reverse the question, that is trying to interop say C#/F# with non-immutable C++ code (Thread-safety aside) wouldn't that be a positive thing to reveal security run-time issues? I think here testing not performance. Thanks!
@zoran-horvat
@zoran-horvat Жыл бұрын
I am not sure I understand the question well. Can you please clarify what is the intention of that exercise?
@kave3299
@kave3299 Жыл бұрын
@@zoran-horvat I apologize. I myself wrote the question in wrong order. I meant to interop a mutable C++ class by creating a C++/CLI wrapper around it and later exposing it to an immutable C# data structure and try to access its members from there.
@zoran-horvat
@zoran-horvat Жыл бұрын
@@kave3299 That can be a tricky design, especially if the consumer doesn't expect side effects of calls. There are examples where a similar design doesn't cause a confusion. For instance, self-referencing builders are common (StringBuilder is one). They allow for call chaining, making an impression that subsequent calls are made on their own instances, but they in fact change the original object. The trick is that the caller knows that that will happen in a builder - that is what builders are meant to do, to accumulate state through mutation. The same kind of behavior in a general class could lead to bugs caused by misunderstanding.
@ThabangKhoza-gi5vu
@ThabangKhoza-gi5vu 4 ай бұрын
I need to redo the basics
@zoran-horvat
@zoran-horvat 4 ай бұрын
Oh, yes, we all do. Modern programming is changing fast.
@nickw656
@nickw656 Жыл бұрын
what's the best way to implement F# `type Key = Key of string` in C#?
@zoran-horvat
@zoran-horvat Жыл бұрын
A record of string is the exact equivalent in C#.
@dmitrykim3096
@dmitrykim3096 Жыл бұрын
Why your lines don't feel into your screen
@zoran-horvat
@zoran-horvat Жыл бұрын
Because of the large font, so that the video can also be viewed on a smartphone. And if I would break into two lines, the relevant code would not fit vertically (account for captions, which take 20% of vertical space at the bottom). It is generally difficult to record code so that the recording fits all devices viewers are using.
@JeremyConnor
@JeremyConnor Жыл бұрын
OneOf library in C# for Discriminated Unions is good.
@zoran-horvat
@zoran-horvat Жыл бұрын
I find it too verbose and, more importantly, when actual support comes to .NET, it won't be compatible with that method. You would have to change all the code that uses OneOf to switch to the any new syntax. That same is not necessarily true when using records: in fact, I expect new syntax to be based on records. I have a general habit of avoiding libraries that develop away from the language's own evolution line.
@JeremyConnor
@JeremyConnor Жыл бұрын
@@zoran-horvat All reasonable concerns, I don't use it too widely because I don't like the AsT0 and ToT1 calls; that said, it's very nice for the Rest API consumers which sit at the edge, giving a clean simple contract with the underlying services.
@segwitchannel2042
@segwitchannel2042 Жыл бұрын
i feel like I'm still new. haha
@Alguem387
@Alguem387 Жыл бұрын
Wont anybody think of the memory?
@zoran-horvat
@zoran-horvat Жыл бұрын
Yes. These are all short-lived objects. Done thinking. BTW, I have developed an entire online library which transforms sound recordings. The entire library is immutable. Many recordings processed in the server go well above 1 GB and yet the total amount of memory allocated per client is invariably below 1MB. That puts the question back in your yard.
@paulsanchez5030
@paulsanchez5030 Жыл бұрын
If this was a code review I would write this. Refactor pls => code structure is all over the place, statements sometimes indent sometimes are in line, standarize it pls.
@zoran-horvat
@zoran-horvat Жыл бұрын
The code base is following precise rules in that respect. Which lines of code are you concerned about?
Why is C# Evolving This Way?
15:02
Zoran Horvat
Рет қаралды 21 М.
This Dumbbell Is Impossible To Lift!
01:00
Stokes Twins
Рет қаралды 37 МЛН
Ik Heb Aardbeien Gemaakt Van Kip🍓🐔😋
00:41
Cool Tool SHORTS Netherlands
Рет қаралды 9 МЛН
RECORD TYPES | Exploring C# and DOTNET | Rahul Nath
15:32
Rahul Nath
Рет қаралды 3,1 М.
Favor Method Chaining Over Nested Calls | Clean Code
10:23
Zoran Horvat
Рет қаралды 25 М.
Main Hall 06: Why is Functional Programming so hard?
37:44
DDD Melbourne
Рет қаралды 2,7 М.
Functional Programming with C# - Simon Painter - NDC London 2023
1:09:05
NDC Conferences
Рет қаралды 16 М.
Dear Functional Bros
16:50
CodeAesthetic
Рет қаралды 498 М.
the cleanest feature in C that you've probably never heard of
8:13
Low Level Learning
Рет қаралды 134 М.
Manage Nulls Like a Boss and Never Fail!
21:43
Zoran Horvat
Рет қаралды 12 М.
How to Avoid Null Reference Exceptions: Optional Objects in C#
18:13
17 Pieces of C# Syntax That Make Your Code Short
12:41
Zoran Horvat
Рет қаралды 22 М.