Unleash Your Functional Modeling Superpowers | C# Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 มิ.ย. 2023
  • 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.com/go/beginning...
    ⭐ Collections and Generics in C# ► codinghelmet.com/go/collectio...
    ⭐ Making Your C# Code More Object-oriented ► codinghelmet.com/go/making-yo...
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    ⭐ CONNECT WITH ME 📱👨
    🌐Become a patron ► / zoranhorvat
    🌐Buy me a Coffee ► ko-fi.com/zoranhorvat
    🗳 Pluralsight Courses ► codinghelmet.com/go/pluralsight
    📸 Udemy Courses ► codinghelmet.com/go/udemy
    📸 Join me on Twitter ► / zoranh75
    🌐 Read my Articles ► codinghelmet.com/articles
    📸 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 TH-cam 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 ปีที่แล้ว +16

    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 ปีที่แล้ว +11

    Your example functional mapper using pattern matching is beautifully elegant.

  • @tristanstricker967
    @tristanstricker967 9 หลายเดือนก่อน +3

    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

  • @shmupful
    @shmupful 4 วันที่ผ่านมา +1

    Art of coding at its best

  • @geesysbradbury3211
    @geesysbradbury3211 ปีที่แล้ว +3

    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  ปีที่แล้ว +1

      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 ปีที่แล้ว +4

    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  ปีที่แล้ว +2

      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.

  • @GiovanniCostagliola
    @GiovanniCostagliola ปีที่แล้ว +4

    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 ปีที่แล้ว +2

    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  ปีที่แล้ว +3

      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 ปีที่แล้ว +1

    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 9 หลายเดือนก่อน +1

    Как Боженька пишет... Thank you, Zoran! It's very impressive!

  • @RichardONeil
    @RichardONeil ปีที่แล้ว +1

    Whoa...that was super!!!

  • @ivandrofly
    @ivandrofly 11 หลายเดือนก่อน +1

    Thanks

  • @aamirali8114
    @aamirali8114 8 หลายเดือนก่อน

    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.

  • @ThabangKhoza-gi5vu
    @ThabangKhoza-gi5vu 3 หลายเดือนก่อน +1

    I need to redo the basics

    • @zoran-horvat
      @zoran-horvat  3 หลายเดือนก่อน

      Oh, yes, we all do. Modern programming is changing fast.

  • @codingbloke
    @codingbloke ปีที่แล้ว +2

    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 ปีที่แล้ว +7

      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  ปีที่แล้ว +7

      @@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 ปีที่แล้ว +1

      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 2 หลายเดือนก่อน

      @@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

  • @ReinkeDK
    @ReinkeDK 9 หลายเดือนก่อน

    Excellent video :)
    Might you have a suggestion for a good book to read, to get into Functional Programming in C#?

    • @zoran-horvat
      @zoran-horvat  9 หลายเดือนก่อน +1

      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 9 หลายเดือนก่อน +1

      @@zoran-horvat Thanks for the tip. Keep up your good work :)

  • @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#.

  • @lmao01
    @lmao01 ปีที่แล้ว +3

    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  ปีที่แล้ว +7

      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 ปีที่แล้ว +1

      @@zoran-horvat Ok. Gotcha. Thank you for the answer ❤❤

  • @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  ปีที่แล้ว +1

      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  ปีที่แล้ว +1

      @@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.

  • @dmitrykim3096
    @dmitrykim3096 ปีที่แล้ว

    Why your lines don't feel into your screen

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว +1

      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  ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

    i feel like I'm still new. haha

  • @Alguem387
    @Alguem387 11 หลายเดือนก่อน

    Wont anybody think of the memory?

    • @zoran-horvat
      @zoran-horvat  11 หลายเดือนก่อน +1

      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?