Goodbye controllers, hello Minimal APIs - Nick Chapsas - NDC London 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay that should be it hello everybody I'm making this video not today later today at 12 30. today we're here for the second day of NDC hope you had the first fantastic day we had to talk about controllers and minimal apis and this talk is as much about controllers as much as it is about minimal apis and also what they mean for the future of.net in general so just a quick intro I'm Nick Heim I'm a bunch of stuff if you know me you probably know me from YouTube actually how many of you know me from YouTube that's a good amount there okay for the rest of you you have to go and subscribe that's a requirement for this talk um I'm also turning 30 in April so I thought it's a great idea to start my own podcast which seems to be also a legal requirement so if you want to see some interesting podcasts with some amazing talks um or discussions with people like Damien Edwards mods for the future of.net and C sharp I highly recommend you go ahead and check that out um and also just a quick plug if you like topics like minimal apis testing or dependency injection or other things that can really take your skills to the next level then I have my own courses at nickjazza.com I highly recommend you check that out now there's a show of hands has any of you seen this talk already from NBC London last year the hell are you doing here like I mean it hasn't really changed that much but um thank you for being here now how many of you know what minimal apis are you see okay that's a good number that's way better than last time and how many of you have them in production that's really good and if the remaining people who actually know who they are who who do you think or just raise your hand how many of you would never use them in production or think will never use them in production don't be afraid this is a safe space I saw you okay so fun fact minimal apis are actually the most loved feature of the dotnet6 release by far I mean when they came out everybody just instantly loved them it was amazing to see the community just come together on Twitter and really Embrace and hug this awesome feature that really removes that barrier to entry of making a new API with tons of controllers and the startup.cs and program.cs and making it extremely easy obviously this is a very long-winded joke people hated it um and some of them really still do they think it is not for them and they think that the asp.net core team is actually wasting their time doing all this now let's see if that is true by the end of this talk but you might be for those of you who did not raise your hands what are minimal apis Nick well I could have a very fancy quote and just explain what it is but really there is nothing that describes the minimal apis are better than this this is fundamentally what minimal apis are these four lines of code are a valid API you can take right now and run ingot.net six and Beyond code base and it will work there's a bit of a lie on this quote there's actually a CS prods required for this to run because you need to specify that this is a.net project but as long as you have that there you can do dotted run and we run you're going to have a single hello world endpoint now for those of you who haven't touched C sharp in a long time you might be like okay how is that even possible we're gonna see that but before we see that I want to talk about the five fundamental points you have in here so the first thing we have is this Builder the Builder is responsible for configuration logging Service registration for Di and all that then between the Builder and the next line is where you would have your log providers being added Your Service registration all that goes there and then you materialize that configuration or that definition into a web application which is this new abstraction of what is a web app in.net the gaming.net6 and then it's your time to shine it is your time to add your end points no more controllers no nothing you just say map get map post map whatever and then you can return a delegate you can return an object enter whatever you want and that will be serialized and return to your browser the last thing is just running the application that is it but many of you who have worked with other Technologies and other languages as well might be thinking wait damn minute that thing over here that looks a whole lot like that thing over there now I don't know if you can see it but those are not both C sharp the bottom thing is express.js from the JavaScript world so Microsoft was that's blatantly ripped up sorry my my lawyer they get inspired by express.js heavily to go ahead and make the same thing and there's a reason express.js has seen more use in web applications than anything MVC or ASP network has done in the past 10 years don't quote me on that by the way but clearly there was a need for that because why is Jas so popular if it's such a bad language but it is so widely used well it is because the barrier to entry is just so so low so what if or sorry this is not actually A New Concept at all python for example with fast API it's the same idea all you do is you import fast API you create your app which you can imagine as the same web application Factory and then you define that endpoint you define what you return and that is serialized as a Json object simple as that go with more cool language and modern language does the exact same thing with a project called gen so Jin has this idea of a gene.default which is the same thing as well application Factory then you can do Jane dot get and you can Define your delegate you have your gen context which is the same as a HTTP context and then you write a string you write Json you write whatever you want back then you run it even Nancy effects we have this in C sharp how many of you remember Nancy FX yeah that was good old days so Nancy effects was this thing in asp.net where you could just have a module and in that module you can define an array of get post put whatever you want and then your endpoints and then have this delegate that returns hello world and in this case that would just return that which at the time it was unheard of now again it wasn't new because Nancy in itself was actually copying Sinatra which was a Ruby on Rails framework that was very very popular and even here you can see that exact same Simplicity requests inatra get them the route do and then end and that is it now when I go back to what we saw before with c-sharp because if you stop writing C sharp in.net framework or C sharp seven points two or whichever one is the last version and you're probably like how is that even possible well I made a bit of an animation to show you how it's possible and it took me three hours to make so I appreciate it so all of these features are flying and coming together all of this were added in C sharp 9 10 and Dot net six and that's how we get it and just because I spent so much time for just well five ten seconds we're gonna see it again just appreciate it it's good it's you can tell how it has a watermark like I don't want people to steal it like that guy made it anyway so let's talk about each one of those features individually so we have top level statements now top level segments are actually a C sharp nine feature not even a C sharp 10 which is the one that was paired with a.net 6 release and basically what happened is a.net team took a step back and said you know what that looks stupid because it's the same every single time you always have well the name space is optional but you always have a program class you always have a static void or static int mainstream array args I know it sounds stupid there's more stupid to write it every time and we have three levels of nesting just to get started which is just obnoxious so what happened in C sharp 9 is top level statements were introduced where now you just have the using statement and then you say console.writeline and you have the text that you want the rest of it is implied it is still there the code that you eventually compile is that same program.cs but you don't have to have it there because it's supposed to be there by default and it's what causes tons of confusion in my comment section when people who don't do C sharp see me write something like this and like what is this scripting it's like no it's C sharp it's good Okay C sharp has changed I'm gonna give it a go which is fundamental to this whole effort for Simplicity now some of you might say hey Nick you could make it even simpler by just removing the using and saying system.console.writeline I'm just matching it into one line and be like we're cool you can have a right um you can have a Hello World with just one line of code however that would sort of kill this awesome Segway to the next feature implicit using statements which is this idea that hey there's like eight using statements eight in spaces that we're adding in every single one of our files or most of the files and those are things like link system.task system itself is many of them so now what you can do in C sharp 10 is you can specify the implicit usings as enable which is now is the default actually and this will remove the need for the using system to be there and a bunch of others because behind the scenes.net will do that for you and how will it do it well with a new feature called Global using statements so Global using statements is this thing where you can have a single CS project CS file and drop a global using Global and something and this will be globally added in every single one of your files and really what happens when you do enable on implicit usings is a source generator kicks in and says oh you don't have one of these I'm going to go ahead and drop it and if you say disabled you've just deletes it if it's there and now all of these is just implicitly added in every one of your files removing the need for you to have this using system in the case of minimal apis now another small feature that made this possible is called inferred Lambda types so previously in C sharp 9 even though the compiler has all the information it needs to know that this is a delegate function that accepts a string and returns a string if you went ahead and you said VAR on the funk then it would not compile it says I have no idea what you're talking about I don't know what this is so what C sharp 10 did is made that possible and actually it was very hard to do apparently I don't know why but it was and now the last small thing was actually attributes on lambdas so you could not have something like this before.net6 to say like obsolete on the Lambda function but because that.net um asp.net core sorry team needed it then now that is possible and this is very indicative of the effort that went on during the net six release and the C sharp 10 release because the interesting thing about it is that the c-sharp team pushed the dotnet team and the dotnet team pushed the c-sharp team because the C sharp team had to make those changes but net to do this the minimal API is to exist and this will actually continue with future releases and it's still happening now at this point I want to go and take a look on a live demo and I'm not using Azure so don't worry this will work and I'm gonna show you how minimal apis really look like so let me know if you can see this all the way to the back good good so I'm going to show you how to create a minimal API from scratch and I'm not going to cheat by using a template I'm just going to make a console application so I'm going to go here in the getting started and say in.net 7 in this case and say minimal API dot demo and console I'm gonna go ahead and create that I'm going to delete all of that and the only thing you need to do to actually turn this into a web project is actually go to the Cs project over here and specifying the project SDK that this is a microsoft.net DOT SDK dot many dot web that is it the moment I save you can see something is happening in the background it is just installing a bunch of packages implicitly that you can't really see here in the dependencies so now I can go ahead and say VAR Builder which is my first component so web application Dot createbuilder and at this point I can actually pass down the arguments of this project because they still technically exist because it is wrapped within that main method like we mentioned before then I'm going to create the app which is Builder Dot build and then I'm going to run it and this is it now if you're coming from dotted 5 you're probably thinking wait a second there was supposed to be a startup.cs and I can configure services and now I can so no you can you can imagine that this is the configure Services area so anything between here you can register and anything between the app and the app run is actually your configure method from the startup.cs the one where you configure the middleware anything between this doesn't really need to be ordered unless you materialize your dependencies because they will be materialized on app creation anything here however needs to be ordered so if you use something like Swagger and you want to have it outside of authentication then you need to register it before your app.ad.netication call and so on everything is in sequence here and one of the big problems this can cause if you're not careful is that you can create as many apps as you want out of that Builder because that Builder is just instructions on how to make web app so this would start to Applications so be careful also if you try to retroactively add the service let's say a Singleton after you created your app then this app won't be retroactively updated with the service the moment you create it it's there you don't touch it so let's go ahead and create our first endpoint and I'm going to go with a very simple app dot map get for hello world so hello and I'm just going to say hello NDC so let's go ahead and just run this and I'm going to use this at dot HTTP and file which has some predefined HTTP requests and I'm going to say run hello so simple as that I have something that returns effectively hello world hello NDC now let's make it a bit more complicated because just returning raw text isn't really fancy so what we can say is hello object and I can just simply return an anonymous object here and by default it will be serialized so hello equals NDC just run this real quick and by default the Json generalizer is used you get a response back now we can take this a bit further because how often do you just work with Anonymous objects you don't really so I'm going to create a record not that here we go a user and I'm going to have a full name so string full name and I can very easily scroll down say app.map get user and by just returning the object we have full serialization let's do this make chapsis yeah you know what ciao us fine so let's rerun this unfortunately hot reload doesn't work for this because you can't retract reload register endpoints so I have to stop and start every time but again full serialization you get it but Nobody Does this nobody just has endpoints without parameters now the good thing is dotnet or minimal apis support some special parameters to be injected in the Lambda by default so I can say map get special and I'm going to start with I don't know the most basic one which is the HTTP context so if you just say context here by default or not not my Nemesis parenthesis here we go then I can just say context dot response dot write a as Json is saying and I can have the same user copied over and then I have more control over what I'm writing so if I also want to specify things like the status code the Response Code I can do that now for this to work I have to turn this into an async function so this is a sync and this is awaited one of the biggest problems this causes is that you can technically return at any point or you can technically not have anything here and nothing will start stop compiling but this will not you know it will fail only when you call it it's very very dodgy the way this is implemented which is why right as Json async is not really used that much in normal applications but in the same way if you wanted to access things like the request query string parameters you have everything here you have full access but maybe you don't want that maybe you want to narrow it down to some other special parameters like HTTP request or HTTP response you can do that you can have HTTP response here and only write that you can also let's go back here you can also have a cancellation token so if you want to cancel your request after some time you do that and then you can pass that into your async methods so you're not missing out on anything by going down this route you still have all the goodies of controllers here or controlling actions now I want to show you how extra this can get and I'm going to start with a get request I'm going to say Extravaganza here and I'm going to show you how we can inject different parameters because minimal apis and this sort of model structure depends on a lot of implica and implied or assumptions sorry so if for example I say int age where would this be coming from I didn't specify where but if I say return new age then this has to come from somewhere so if I just quickly run this it show you it is going to be the query string parameter by default 19. so you don't have to specify from query by default any parameter on a get request that is a primitive like this will be mapped from the route you can also be explicit about it so if you want sorry from the query string if you want it to be from the route then you can say age and you can even have constraints like end so if you do that then this will come from the route you can also go down a different path so if I say here here and then end year here then age is query string year is route this can get very confusing because you have all this cognitive load to keep in mind you know this is coming from this this is coming from this and it only gets worse so let's say that age actually comes from from query string nope comes from query string but it has a different name you want it to be a in your query string we can say from query and then name equals a it turns out two equals doesn't work here we go so if you do that then you can map something like this where the equation parameter is just an a you can still call it whatever you want internally do you want the header from header do you want to inject a service from your di container you can say from service however this is another thing if I just quickly grab this date time provider and I put it here this isn't something magical by the way it just is a way to test your daytime while you're using it into your code you just inject the interface and then you can mock it if you need to so if I just go here and I register this here we go then I can just simply say I date time provider and I can use it there we go and even if I don't specify where they should be coming from on Startup minimal apis will actually detect that and say oh this is coming from query this I can map to Route this I don't know but it's not a body because this is a get I don't have a converter for it it exists in dependency injection in a container that's what it is and if you do that then you can see everything here automatically mapped you still have control to be explicit and it goes even further if this was actually a map post then you can even have a user coming from the body and you don't have to specify anything so you can say something like this it can get too much and that's why in dotnet 7 there was actually a new feature added where you can say something like um presentation request and you can wrap all of that into a record so I can have everything here I'm not going to rename anything just do not waste any time but you can do this and you can say as parameters here and you can pass down your object so presentation request request and then you can use it here so if you want to access the age you want to have everything in a single contract you can which is a pretty neat thing to do the problem is if I go down that route I would be explicit about where things are coming from just to show you how this will fail if I don't have the service in my di container but I actually try to resolve it then let's see what happens so nothing fails on Startup the application just started which you know depending on who you ask isn't really great because I'd like something like this to be detected and startup but if I do run it response body is empty because in the Run you can see I got this error saying hey age is coming from an attribute route and body inferred request is as parameter but the user is unknown and there's a bit of a clash with a provider here so it assumed that the provider came from the body and the user came from nowhere so I'd be explicit you're gonna you're gonna write safer code by doing this but fundamentally this is what it is about now there's more features that we're adding.7 and we're going to talk about them but here we are so let's continue from here so there are actually a few unsupported features or at least upon release there are quite a few first there was no support for MVC filters so if you have any iasync action filters or result filters or exception filters there was none of that when it launched and you still technically can't use those there was no API version now not the concept of API versioning you can still if you want have V1 V2 V3 folders and you can use route versioning or whatever you choose to do maybe header but there was a nuget package that Microsoft has in the dot net organization that at the time did not support minimal apis also there was and there still isn't no built-in support for validation so if you want to validate an incoming object you actually have to write your own validator which in my case that is fine because what I always did is I wrote my own fluent validation sort of class and I passed that through a custom middleware so I could still work my way around the problem is there wasn't any filters there so I had to do it every single time for every endpoint if I want to be fast and there's also no support for all data how many of you use old data how many of you like it yeah I thought so there's no support for that but there it might be coming mainly because Microsoft themselves use our data and that might be added in the future but it's not here now in dotnet 7 we actually did get filters in a new implementation it is not the same type of filters but they're very nice and they can be endpoint specific very very nice clean implementation and also the API version you get package now supports that so if you want to go and plug that in you can do that and I do have a video on my YouTube channel on how to do that now validation still does not exist built in however Microsoft is thinking of ways to add it the problem is it needs to be very unopinionated and generic and also very very fast so they're thinking Source generators and from what I hear from David Fowler he said it is so hacky you're gonna love it so I don't know what that means let's say and the last thing is you know for the old data you might get it eventually it's not here but to be honest consider using a graphql with hot chocolate I think it's a better experience if you can now I want to go to a very interesting phenomenon called program.cs hell so this was very much a common sentiment when the minimal API has launched which is this idea of oh donus 6 is out I'm going to give it a go I'm going to try to run a minimal API build one and then they end up with six app.map get methods and six classes in one program.cs they hate themselves they think just you know web API is better we're going backwards and that is a very interesting observation to me because often do you make a controller when you make a web API and then in that controller you add let's say the domain you're working with is a customer you want your customer service your customer contracts your customer repositories your customer mediator handlers validation you don't add all that in a single controllers class you split it nothing changes here minimal apis are not one file apis all they are is a strip down version of everything you could do before and broken down to its bare minimum so let's talk about what minimal apis are not explicitly so like we said not one file API is just forget about it when someone says that just correct them minimal is about minimizing first the cognitive load in getting started and the entry level enabling anti-level developers to build basically any API they want also that they're not just for demos as you just saw however they're fantastic for demos because I can very easily just have everything in a single page scroll up and down and you don't have to worry about oh this is in this file and this in this file and this in this file and you know everything is there everything is easy to do the problem with this is that Microsoft also heavily use them for demos and Microsoft demos are like 2000 lines of code and people say that and think that's how you build an API it is not how you build an API so don't do that and in the same way that they're not just for demos they're not just for beginners they are great for beginners but in the hands of someone who really can't structure a project they can get really creative so I think maybe the middle level is who it is really not for or maybe not for but beginners and experts Prime candidates for this and then the last thing is because I hear this quite a bit actually then I'm going to kill controllers the reason why this came about quite a few times is because Microsoft has been very vocal about saying we expect 40 to 80 percent of all new use cases to use minimal apis which is a bit optimistic in my opinion like it's not really gonna happen um but certainly for some use cases it's going to be very very good very very viable as well but at this point I actually want to take a second to just address the elephant in the room because I do believe that controllers are building apis are a really weird concept and I I did not get it so I went back to see why we're using controllers for for building apis you know how do you think they came about do you think that the 10 smartest people in Microsoft sat down and said okay let's put it down performance engineering architecture structure what's the best way to build an API and they said oh yeah control this is the way to do it no what happened is they needed to ride the whole rest API web API to wave and they had this thing called MVC so what if we take the controller aspect which has validation model binding and Json XML and HTML response and just keep the C because really what is the last time you used a view in your apis you haven't so what are using controllers which is an MVC construct in your apis well it's just rebranded MVC also models in NBC they're not really API contracts and the way you we use them because in API contracts and apis in general you have an input and an output object and they're separate they're not the same thing so it's not quite models so we're using just the C from MVC and the truth is it was just the easiest way to get it out of the door and have it in the hands of consumers in a reliable way because MVC did work and it still works people still use it with success there's nothing wrong with NPC however I have an issue with that structure because they have all these methods these actions within that controller that they never call each other and the only thing that really calls them is the router so you have a bunch of public methods in a public class that nothing explicitly calls it doesn't really make sense why they should be together and this becomes more obvious if you think that really they're actually violating the Open Class principle because what you do to add the new endpoint into your API you open that controllers class and you edit it what do you do to remove one you open that one the controls class and you delete the method so why are we doing that what's the separation of concerns what are the same responsibility oh my responsibilities is a controller well why didn't you go more now and have full control into that responsibility because it is not uncommon to have Services injected on the controller level that are only used on a single action but because those Services could be transient or scoped Independence injection those objects are being instantiated every single time and you're wasting memory for things that will never be used by that service or by that action so why are they sharing real estate I really do not understand this another thing is that they never share State between actions because controllers are actually scoped per request so you don't have something that will be reused by all the methods so why are they all together just sounds very dumb in isolation it sounds smart in the context of MVC because you have a direct correlation between your view calling the controller but between the API calling the controller there is no correlation and also when you have private methods because you thought I'm gonna just extract this private method I'm gonna because it's getting too big and I'm going to give it a nice name and use it they usually used just by a single action so you have a private method in a class that's only used by a single action okay then should it be a local function oh that looks too big in a single method and then maybe you don't even need that well why don't you just split that then they do invoke middleware and filters that you might not need implicitly all that validation all that model binding there's tons of things behind the scenes that's happening that your actions don't need but you're paying the price of those objects being instantiated and that memory needing to be Gap is collected every single time they have tried to make it efficient but there's so much you can rip out of a system like MVC that has been running for years now they can also grow to that thing called fat controllers I don't know how many of you have heard of this term but they can grow to quite a lot a big size and very commonly the the way the way people solve this problem is they actually introduce mediator and then they move all the logic into a Handler so they brag that okay my controlum actions is actually just request mapping mediator send and response mapping well that sounds something like you can put somewhere else and not have the controller in the first place like why do you need this if it's so simple um and the truth is that the reason why we have just accepted it and use it is because the only viable option for building apis in.net or they were the only viable option if you really want to be a nice rest API yes Nancy effects was a thing but at the time people were not really happy to base their whole livelihood on the whole company on an open source project that might die and in fact it did eventually die now it is sort of reincarnated with a different project called Carter but I still wouldn't use that because it's competing with minimal apis and you're just playing catch up so let's talk about what this talk is yeah let's talk about what this talk is about really and like with everything in life this talk is about Lego it is always about Lego and I promise you this metaphor will make sense so what do you see here just think in your mind yes it is a Lego Castle but just try to think outside of the box so what you have here is I have started or have finished castle and I can see a door I can see Foundation I can see Windows there's a structure there is another one over there and the great thing about a half started or finished structured is that if I go to my nephew who is five and obviously five-year-old not the smartest and I say build the castle well here's watch Harry Potter so he's gonna build something like this without any experience just because there is enough structure in something like this to eventually lead to that now it might not look that great because like I said he's five but it is good enough but the good thing about this is that if you give it to me who has never really built any fancy customers but I do have the imagination to use Lego then I'm also sort of set in a path of success because there's enough here for me to get inspired and finish what someone else has started now here's what's very interesting to me let's say that you take all of that away and you give to the same five-year-old ground and a bucket of infinite amount of Lego bricks with any color and any size and I say hey build a castle what he's probably gonna build will look something like this now is this a castle it depends on who you asked to me it looks really bad to him it does look like a castle that's what he wanted to build it serves the purpose he got a cracker that he used his imagination and he built it but then if I go to his mum who's actually an architectural major medieval architecture and Gothic architecture and say hey can you build a castle with those same ingredients then what she's going to end up with is potentially a masterpiece do you get it this is what controllers look like to me there is enough structure you start from this controller class from this from this action that there is no room for experimentation in order for experiment no room for experimentation means very low likelihood of failure you're gonna end up with something that on something that is quite good not perfect but good and that's why we have things like clean architecture be so big because experimentation actually starts past the controller we sort of agree that controller is a problem we need to solve and then we add 15 layers of abstractions because we're obsessed with it just to complicate life and say oh if in the future I need to change my database layer how many of you have changed your database service in your project ever just show me how many of you during that process didn't have to change the interface that you abstracted your thing around yeah exactly one person like if you go from something like an rdbms to a nosql database your access buttons will change you won't be able to do the same things that you did before so This Promise of in case I change it is a bad healthy diet in my opinion but the good thing is Junior developers meet developers and Senior developers will end up with something that is pretty good but in the same way that a junior developer will be the very bad API at least the barrier of Entry was pretty low you don't have to even worry about something like this okay where do I put the brick I have to find the right brick to put the thing and then it's like even that makes things complicated but such an approach to a junior developer with no experience makes it very easy to get your foot through the door and say you know what this thing Returns what I need this thing can work and here's the good thing about this yes they built something bad but next time they do it they're going to try to do it better and better and better and better and as the time goes on they're going to improve and also the way to get started was very easy there was nothing you need to know about go crazy but take a staff engineer a principal engineer or a senior engineer too whatever we call them these days and we say Okay create a properly very nicely architected API with all the tools you can ever need then they will certainly build something like this because now they have the freedom to do that and the freedom to control their Foundation you cannot build something like this on this Foundation it's just impossible you're going to build basically a castle on top of a toothpick which is in my opinion what clean architecture is so what are minimal apis really well again in my opinion I think they're in an opinion the way with high performance apis inductment with minimal amount of implicitly added features and the flexibility to opt in the features and the structure that you need is about you is about choice so now with all that said let's take a look at what is possible with minimal apis when I go back to this project over here I'm going to show you this abstraction API now this is on GitHub so if you wanna use it and do something with it just says Nick shops is GitHub you're going to find it under a minimal API so what do I have here this is an API built on minimal API technology you wouldn't know just by looking at it because it has folders it has settings it has validation it has structure well this is only possible because we now have minimal API technology backing it up there are no controllers in here I'm gonna start this and show you how it works so your regular apis here and then I'm going to go to the structure.http and call some endpoints so what I can do this is a rest API with customers being the object you're dealing with and I'm gonna go and try to get all the customers in the system now there is no customers so I'm going to get an empty response what I can do is I can create a customer so I'm going to go here and just say create that customer and the customer was created I used update to do that or did I no I just create that's good so you have 201 created the right status code you have the location header over here this is exactly how rest API would be I can go ahead and edit that user using the ID and delete that user excuse me and you can also retrieve that user so this works and behaves as a rest API um it also if I go in the console has full Swagger support so all my documentation about this API is here customers endpoint the object is here that accepts is here everything is here very very nicely the request did not pass validation checks customer was created successfully this is all the documentation you need in your minimal API how is that even possible let's take a look how it is my entry point for this structure is actually these endpoints folder so I have all of my endpoints for my customer in here so if I want to create a customer all I do is I say httpost customer and then in this case I say allow Anonymous I have no Authentication and then I create a class that extends the class endpoint and then I specify my input and my output my request contract and my response contract and that is it I don't have to explicitly make it be referenced anywhere because in program.cs it will be automatically grabbed so all I need to do is actually create a class extend endpoint and that is it I have an endpoint and now in here I have everything that is just explicitly required by this endpoint and nothing else customer service that is it do I need a logger it's only injected here do I need something specific to create like publish a message for customer creation only here I don't need to inject for that anywhere else like they get which is what you would do in controllers and then I have this handle async method where I'm getting the request I do have the cancellation token I'm converting it to a customer and then create map return and that is it that's all create does I can go to other things like get all same story in this case endpoint without request because get all doesn't have an object that you need to get it's just a customer's call same thing you return it and please buy do not think that this is the way you should be building an API this is just one of the ways you can build them just because now it is truly possible yes technically you can say Nick you could do that by having single action controllers you didn't need this but this is actually way faster because minimal apis are faster and now you control the full pipeline which you do not do in Old School MVC another thing is that now if I want to have a validator the only thing I need to do is just create the validator class using fluent validation and then specify the request object and that is it nor do I need to say this validates this it is implied based on what the abstract validator is created for so if it's not empty it's gonna fail or if it is empty it's gonna fail same with everything else the other thing is the summaries if I want to document an endpoint the only thing I need to say is create a summary class that extends summary and wraps that endpoint so summary for that endpoint and all of my documentation for that endpoint is here you could of course use XML commands as well on the 10 point class that's totally fine but you can also have it in a single class with which I personally prefer and then you have other stuff like your mapping if you want to have any of that and the startup.cs which is where all the magic is happening so that's what my startup.cs is my project.cs looks like sorry program and the only thing I have here is this add fast end points call now fasting points is a third part library that is using minimal ABS to make that possible I was actually working on something that is effectively What fast and points and end up being and because the developer was way faster than me I actually just reused that so fast and points is what provides all this logic and then we have a middleware to catch everything like I said sequence does matter and then I'm not cheating here the only thing I'm hiding from you is this error response building so if we catch a failure we wrap it into a specific object and then we'll just retire it but really that is it so maybe it is not for you but it is definitely for me and what I needed and this is again what is this is about it's about you it's about choice now let's talk about the phenomenon now I like to call minimal Valley just to sort of describe what's the minimal API viability per project and even though the horizontal line is supposed to show time um it's really type of project because the life cycle of a product usually these days looks like this so I think that minimal API is extremely viable for beginner projects demos like we already saw proof of Concepts and will actually approve Concepts so for those things you can just very really get started you can very quickly have a demo very quickly have a proof of concept and the amazing thing about the last one is that you can take your POC exactly as it is and turn it into aerial application without too much effort because you have a blazing fast structure with minimal apis you can just reuse that you don't need to migrate or graduate into controllers which is the Assumption of many people but I don't think that it's really viable for this chunky Enterprise applications now chunky Enterprise applications doesn't really mean you know monoliths Beyond saving it can still be a very well architected and modular monolith but I don't think that the type of project company and developer really will benefit from moving away because if your API or your app is slow you're not gonna you're not going to get anything from the benefits of using window apis however I do think that the best case scenario for minimal EPS in the best use case is actually microservices minimal API micro Services both of them are supposed to be small I've never written one that's more than 1.5 000 lines of code the complexities on the architecture so why don't we keep the code very very simple and easy to work with so that's how I feel about this now I want to wrap this talk by talking about the importance of minimal apis about.net in general and about you let's talk about how people get into a programming language so there's many ways and this is not an exhaustive list but they probably have an idea they want to build that's how many of you maybe got started um my cousin wanted to get started with programming because he wanted to build Tetris from scratch how do you do that well I have an idea I'm gonna just do it University School College I'm sure many of you have also started program because of that curiosity as a beginner how do you do that or that person on YouTube said they can make I don't know 150 000 dollars a year by just taking a bootcamp I want to do that as well that's a lie by the way it's never going to happen well you know they're selling the dream also curiosity is an experienced programmer this is another one I personally really like to get into other programming languages to see where c-sharp is actually getting inspired from because it's just all of these features being added they're not features you've never seen anywhere else they're all coming from somewhere usually a shop and the last thing is because the company is using it that's how I got started with C sharp I desperately in the job the first person who said yes um just happened to use C sharp in the company and I just got into it but here's the thing these are the only ones that really are relevant for c-sharp now you can say Nick the unity game engine actually gets a ton of people into c-sharp but the truth is for any of you who have seen Unity code it's a whole different story it's the wild west over there there's no rules in the way you can write C sharp in unity so it's not the type of developer that would really graduate into an aspen core developer and maybe it's not developer you really want to hire so let's focus on these three so here's the thing about this only this one is one that we can sort of control with c-sharp because universities change what they teach schools or colleges and companies migrate from java to node to ABC shop to rust eventually to whatever they want so let's take that keep that in mind and take that and talk about cranes and you know how the Lego thing actually worked this one won't this is just completely random it's not gonna happen so how do trainers work so you get on the train you stay on the train you get off the train and the trainer will keep doing this journey as long as there's passengers and this very much applies to many things in life as well as in programming languages so let's talk about the programming language Express people pick up a language people use a language people stop using a language and the language is dying when a plus B minus C is decreasing over time so effectively the more people the less people you have using a language over time that's when your language is dying but there's also a catch-22 with this situation because the more people use the language the more people are likely to use a language and the more people stop using a language then more people will stop using a language because it's not popular anymore so if you get into that downhill Trend it's very hard to recover c-sharp is good at B and C at people using the language they love it I absolutely love it and I've written many languages since I got started with C sharp and always something is missing and also people stop using C sharp because it's been around for 20 years maybe you picked it up at your 40s you're retired now you don't need to use it anymore so with constant amount of people you know retiring or stop using it we also need an equal amount of people picking it up if we want the people the language to to survive so ultimately minimal apis are a step towards increasing a it is a step towards getting it to the hands of new Developers that are the developers that will eventually be the future of.net so when you hear people say things or you say things like they've done the thing should not be working on things like this this is not something that they should be wasting the time with this is not for me well the truth is that if you want to be in a situation where you can hire the next generation of C sharp and Doctor Developers then it is for you even if it's not for you thank you very much and keep coding foreign
Info
Channel: NDC Conferences
Views: 27,283
Rating: undefined out of 5
Keywords: Nick Chapsas, AOI, C#, .NET, NDC, Conferences, 2023, Live, Fun, London, Controller, .NET 6
Id: pYl_jnqlXu8
Channel Id: undefined
Length: 54min 27sec (3267 seconds)
Published: Mon May 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.