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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay put a finger down if you thought this talk would not happen and you just down the pint and now you have to give it anyway let's see how it goes so welcome everybody i'm super excited to be here with you today uh for quite a few reasons actually it's the first time i'm speaking in ndc london and it is the city i've spent the last 10 years of my life so super excited to be on this and and talk to you about controllers and minimal apis so quick intro hey my name is nick chapstos this slide is a bit outdated i'm not working for checkout anymore however you should check that company out great company great employer you should give it a chance if you're thinking into moving into fintech a bunch of other stuff you probably know me from my youtube channel and i also have some courses on my own website the website nickjabs.com if you want to give that a go for topics like minimal apis as well so let's dive straight into the topic which is minimal apis now how many of you know what minimal apis are sort of hands okay and how many of you think it's something you will never put in anywhere near production okay i mean so the goal is to not think that way by the end of this talk now in case you didn't know minimal apis are by far the most loved feature of the dot net 6 release i mean everybody just loves them it is amazing to see people come together and embrace the simplicity of using minimal apis and really thinking that adopting something that other languages have been doing for years and moving away from controllers is something that we really can do in this community this joke is going for way too long in reality people are a bit mixed about them to say the least i mean some say that they're junk okay let's see if we can change that opinion so what are minimal apis now this is the point where i could be very sophisticated and have a fancy quote and explain what they are but you do know what they say one picture is worth 1 000 lines of code so here's a picture with four lines of code so this is fundamentally what minimal apis are about it's this idea that we can strip down everything about me building apis in.net and this file is a valid api in c sharp now that's a bit of a lie the only other file you need for this to be a valid api is actually a cs prog file which specifies this as a web project but once you have that there are no using statements there are no name spaces no classes no public static void main string array odds and so on and so forth you just have that now some people are a bit intimidated by this simplicity and they think that this is immature this is not something they ever want to attach where are my controllers well let's investigate that claim a little bit but i want to emphasize something this is not something you you might be thinking what a damn minute that thing over there that looks a whole lot like that thing now blink it you miss it the top thing is minimal apis in c sharp and dot net the bottom one is express.js from the node world so this idea of going from point zero to point i have an api very very easily and very very quickly is not novel it's how jes has been building api for years and it's not really limited in the js world for example python with fast api this idea that all you need is that fast api app and then you specify the endpoint and you have the hello ndc very very simple very very easy to get started let's go ahead go lovely language many people love it when people use it same thing you have gin a framework which allows you to create an app and then have that get or post or put or delete and just quickly return a string or anything that you want and build an api on that let's go ahead anyone remember nancy over here nancy fx do you remember this being a thing so this used to be a thing in c-sharp all we need you need to do is have this get and then you point to a delegate that returns a string and that would return that text now note kind of faded away for other reasons what which we are going to investigate as i go on and it was reincarnated in the form of a project called carter but i don't think you should really use that as opposed to minimal apis at this point but we're going to talk about that as well as i go on and also i'll go on with sinatra which is what nancy affects nancy sinatra sinatra that's the whole name thing um you just have get you have the end point and you have the hello world and that is it so clearly for years this is very very old actually the need to get from nothing to something that returns some data on the web has been around and if you actually look deep down inside other languages controllers and that whole concept of building apis with them isn't really much of a thing unless they have mvc into a framework for example ruby on rails but you might be wondering how is that even possible how can we just have these four lines of code with no using statements no namespaces no classes no nothing and that is an api well i made a bit of an animation to show you how that is possible all those features from c sharp 9 um 10 and dot net 6 apis are coming together you can tell about like how proud i am about this because it has a watermark with my name on it because i don't want people to steal it it took me a long time to make but this is how minimal apis are coming together let's see it again it took me three hours to make so these features are just coming together to give us minimal apis i'm going to skip this early no let's just say i don't know how big the power rangers were in here but back home in greece they were a bit of a deal so this is how this is possible and we're gonna see every single feature uh to see how we got here so top level statements are actually an c-sharp 9 feature which i kind of hated when it came out but in c-sharp 8 you have to have that using statement that namespace which is optional but you had it anyway then the program class then the public static avoid the mainstream array args as if we're insane and we're writing java for some reason and then we have the actual meat of the application which is the console.writeline and then hello world so with top level statements we no longer need that all we have to do is to say using system and then console.writeline.helloworld and yes i could cheat if i did really want to and just merge system with the console but then i couldn't have this amazing segue into the next feature which is implicit using statements so that problem we no longer have that because the c-sharp team said well there's actually around seven namespaces that you're gonna import on every single file most of the time anyway so why don't we just imply that they exist already and then as long as you say yeah use implicit using statements in that cs profile then you can just say that you don't need system which is another awesome segue to the next feature global using statements because now what i can do is i can have a file a cs file that has global using and the statement hence the name and then that will be automatically imported in every single file which is coincidentally exactly what this flag will do on the cs proj the enable flag because it will actually kick in a source generator behind the scenes that will drop that exact file so implicit using statements are a bit of a mixture of global using statements and source generators also in third lambda types until c-sharp 9 the compiler here has all the information it needs to determine that this is a function that accepts a string and returns a string but if you try to do var or try to maybe inline this lambda somewhere the compiler will be like i really don't know what this thing is supposed to be can you please specify it and you either have to cast it or create a new func of something so it was a bit frustrating and there wasn't really need for them to make it being fed but now in c sharp 10 we do have that because the dotnet team kind of needed this for minimal apis to make sense if you actually saw early demos of minimal apis the people who were demoing them especially scott hunter i think he had to cast it and it did look a bit ugly so it's great that we have this here now then the last picture that we got is attributes on lambdas so previously this would not compile you could not have the attribute the obsolete attribute on the lambda itself now you can and this makes sense because if you think of minimal apis if you have seen them before and we're going to see them in a second now you can actually specify things like allow anonymous or authorized attributes on the lambda itself but enough with that what i want to do is i want to go into a demo just to make sure everyone is on the same page so let's go and do this i don't know why i'm doing a live demo i probably hate myself but let's give it a go and hope it doesn't fail so what i'm going to do is i'm just going to create a minimal api and the first thing i need to do is just say minimal api dot demo and create a console app i'm explicitly not making an asp.net core application or a web project just to show you how from nothing you can actually create a minimal api so project is created can i have a thumbs up if you can see this or if not i can all good okay if at any point you can't please shout and i will change it so i'm gonna go ahead over here and i'm just gonna go to the cs proj of this project and i'm going to change the sdk to have a dot web and that's all you need a console app now magically is turned into a web api now the web api has two fundamental components the web application builder which is where you do all the configuration for your api and then the web application itself which is where you configure all the middleware so let's go ahead and see that i'm going to create the builder first so i'm going to say builder equals webapplication.createbuilder and i can actually access the arguments of the program.cs as if they were there so i'm going to pass them down in case someone wants to pass down some settings so that's my first construct then i have the application itself so it's a materialization of the web application builder so dot bill goes here and then app.run and that's it i have my api at this point it doesn't have any endpoints obviously but i can add them in here and effectively all this really is because you might be thinking well where's startup.cs where is my configuration where's everything well you can treat this area over here as the configure method of the startup.cs and you can treat this area over here as the configure services of your starter.cs so anything in here you configure once you materialize your application you cannot actually retroactively update anything so if i was to access services and add a service once my app has been created well that is no longer applicable it won't change because i have my app already here so let's go ahead and create our first endpoint and all i need to do is say app dot map get and i'm gonna have your typical hello world so that's all i need hello ndc and i got that now in order for me to run this without having to stop and start on my id i'm actually gonna go to the terminal and do one of these so i'm gonna say minimal api dot demo here we go and i'm gonna say dot net watch run but i'm gonna choose not to use the hot reload feature see that's my choice i don't use it because i want to imagine if someone thought removing this feature is a good idea anyway that would never happen anyway so this will now start let's give it a second and now every time i update this file the application would restart so i have that and the way to call this without having to go to postman or another application is i have a bunch of predetermined requests in this http file and writer my id allows me to call them so i can call that endpoint and of course it didn't show me what the response is but you can see it here hello ndc i get response back so if i go back i can do other things as well i can say map get and i can say for example hello object and i can just create a brand new anonymous object here it doesn't have to be a real one all i can say is message equals and then have my text hello ndc very easily and i can also actually mix a couple of other things as well i'm going to create a record over here called user with a full name and i'm also going to use a class called results so i can say user and then return from the results class which effectively has a bunch of predetermined ways to return a request so text stream unauthorized bad request sign in sign out all the typical things you would also have in the mvc world all of them are here so i'm gonna go ahead and say just okay and create a new user and of course it will be me okay so with all those done i'm just gonna pretend like i'm pausing so the hot reload restarts hello hello and this works you can see serialization takes care of the case as well it's not capital m you can customize all that and also if i want to get the user everything just automatically works out of the box now i can actually also access a bunch of special parameters here because until now everything is parameter-less we don't really use anything from the request we just return something so let's go ahead and do that i'm going to go ahead and say special over here and i'm going to access a few special parameters and i'm just going to return an anonymous object so the first thing i want to access is actually the http context so i can say context and this now will be automatically injected and gives me access to things like the connection the features the items the requests the response everything now for my taste that is a bit of a quite wide object and i wouldn't really use that in any case really so what i want to do instead is access the http request directly if i want something from the request and i can also break it down and access the http response directly right so i can do a few things with this first i can actually write to the response with a json object which means i no longer need this new return i can take control and return that and we're gonna return a few things from the request but it also means that because this is an async method i can change this delegate to be async as well let's make this like that and let's delete this actually so you can see what's going on and i can say a weight over here so now i can grab something from a request for example request dot query and that's the query collection and if i save and i give it some time to recycle and go back and run this special request then you can see that it's grabbing that age parameter and it's printing it over here so very simple there's other special parameters you can access as well for example you can get access to the cancellation token if for some reason you want to pass it down in fact rider my id is telling me hey i can do something with this cancellation token let me just pass it down for you so here we go and then the last thing i want to show is how you can do a more extravagant request which brings in a lot of the stuff that i just showed you for example what if you want parameters like a query string parameter or route parameter or the body of a request so many things to choose or even a header so let's go ahead and do that i'm going to say map get in fact map post again and yes there is a map method for a bunch of different things map delete map get map post map put there is no patch i don't know if they're going to add it it's not like i'm using patch anyway so it's fine for me extra is the endpoint i'm creating and let's say i want to pass down a parameter from the route so let's say i want to pass down the year parameter from the route it's the exact same thing you do in controllers you specify it like that and then you can map it to a parameter over here let's say you want to have a query string parameter called age well you don't really need to do anything for that it just magically works because it implies that if it's not specified here then it's probably coming from the query string what i'm going to do is i'm just going to print them so you can see them when i call this so i'm going to say age now let's say i want something from the header because if you see over here what i'm going to call this i actually have an accept header so what i can say is string accept but here's a bit of a problem this doesn't know how to map the accept header it will assume that accept is a question parameter because it's more of a common thing to do so the default behavior is this so instead i'm gonna say from header and be explicit about it and last but not least because as you saw on this request i also have a full name i'm going to pass down from the body a user and as if this wasn't chaotic enough let me just pass this down i'm gonna go ahead and copy from this file this datetime provider class which allows me to have mockup date in my application so for that to be able to be injected i need to go up here and register it so builder.services.add singleton i daytime provider and then daytime provider cool cool okay so what i can do now is i can also inject i date time provider now that's quite a lot of implicit statements but if i go back here and i call that api request then it all magically gets mapped well with code so you can also be explicit if you want to for example if for some reason the h parameter you want it to have a different name in the header or the query you can also say for example name equals a and then what you can do is go back here age is a and so on and so forth those are the basic constructs and you still have it mapped over here so i hope we are now all on the same page on what this thing is because we kind of need to put that in context because i'm assuming all of you know what nvc is and what the controller is and what web api is and this talk will be a bit critical about those constructs now so with all that said then let me just cancel this because if i forget it i might fail the next demo so let me move on let's address something there are a few unsupported features in minimal apis it's not it's not all happy days uh if it was everybody would be using them hopefully the first thing is there is no support for filters so so things like the async action filters the result filters the exception filters those things are not supported this support is actually being added in dot net seven however they're a bit of a different thing i mean it's still the same concept but you cannot reuse your filters that you have on mvc in minimal api so you're going to have to remap them but it is coming then there is no built-in support for validation so as you saw we did have model binding but a model validator would not work for example if i tried to have a required header in my model even if the value wasn't there this thing would not throw an exception so you'd have to write your own validators which for me is fine because i'm using fluent validator and i'm wiring it up in nvc anyway so there's not much of a problem for me but i can totally see how this can be of pudding for some people there is also no support like i showed you for json patch again something i never really had to use but i can totally see how some people would benefit off of having that and likewise for our data our data is not supported and that's all i'm going to say about that and also last but not least there is no built-in support for api versioning now this is not the concept of api versioning you can have api versioning if you want you can just make a folder call it v1 put it in the namespace yada yada but you cannot use the api versioning mvc package which has all of that automated for you and you can have it in a header the media accept media i think header or a question parameter or part of the route that is not supported i think that's because it's based on filters for some stuff and we didn't have filters so someone might remake that in the future but as far as i know it is not supported currently but i want to address another problem which is actually caused by me showing you that program.cs file and it's called the program.cs hell so the other day which when i wrote this was the other day this is actually seven months ago is this tweet by someone who said that hey i chose to build the minimal api because dotnet six is out and minimal api is the new thing so i'm gonna use that and i ended up with six methods the mapget methods in that program.cs six classes they hate themselves they feel that this is wrong why are they doing this web api just feels better who thinks the minimal api actually who thinks that web api is better than minimal api can i see a show of hands okay okay so what's the last time anyone not not just the people who disagreed what's the last time that you you created an api let's let's say it was a customer's api an api that manages customers in your domain and you created the customers controller and then in that controller class you also slapped your customers mapper customers validator customers service customers repository any of core stuff maybe mediator was in there as well you didn't because your software engineers and you structure your code minimal apis are not different minimal apis are not about having everything in a single file they're about stripping down everything to its basics so you can choose your own destiny and build the structure that you want so let's recap what minimal apis are not they're not one line one file apis i know it's very convenient for demos for people like me to show you everything in one file because i don't have to jump around in 50 different files and explain what this controller and this service and this repository and how everything is injected you just have everything in that single file and it's very convenient they're also not just for demos but like i just said they're very convenient for demos because everything is in that single file i have this choice do you think that if that was the choice in the past i would have a program.cs a startup.cs a controller a service a repository and so on and so forth no because that can be confusing for demos but also not just for beginners some people think it is just for beginners but as we're gonna see later in this talk i really don't think that this is the gate the case they're great for beginners because it delivers to that promise of from nothing to an api that returns something very very easily the same promise that sinatra nancy and those other apis had but it's not just for them in fact the concept of a minimal api can be tied very nicely into another minimalistic concept that many of you i'm sure are using and last but not least they're not going to kill controllers i know many people think that this is the case and that microsoft will actually make controllers obsolete in favor of minimal apis a lot of that discussion has actually started because someone who is working on minimal apis by microsoft has replied in a comment that they expect 70 to 80 percent of all the future use cases to be done with minimal apis i think it's a bit of an optimistic metric i think many of them will be eventually adopted but 70 to 80 yeah i don't think so and i also did crunch the numbers after extensive research and i did find out that if microsoft were to choose to kill controllers they would break approximately 100 percent of all the software out there running apis in and dot net it's not going to happen so i want to take a minute now and before i go to this mini rand i want to take a second and just address the elephant in the room it is it is a bit of a rant this one it's i won't bore you too much it's only like 50 bullet points but we'll get there um controllers for building apis are weird there i said it how many of you think that they're not weird then they're great for building apis so fans okay so they're weird for a few reasons now why do you think we're using controllers when we're building apis in dot-net do you think the 10 smartest people in microsoft sat down and they said well it does look like that architecturally and structurally and everything really it makes sense to have controllers as the way to build apis and net that is not the case the only reason why we have them is because ruby on rails was a thing and then microsoft thought well we kind of need an mvc framework as well and they built nbc and controllers do endpoint routing model binding and validation so they were like can we just rebrand this to another thing call it web api and call it a day and just give it to people and say this amazing new way to build apis is here just use it that's the only reason why they're called controllers they could be modules there could be endpoints that would be anything as far as like here i i don't care but we have to acknowledge the fact that this is not the starting point controllers are just one of the end goals and it's one of the many end goals usually you start with minimal apis or the equivalent in a language and you get there but here's the thing because we're using control s which is the c on mvc but what's the last time you use the view when you're using a controller because the whole point of having a controller is it's part of mvc but you don't use views so why are we using mvc contracts to build apis and likewise m the models in mvc is more about business logic it isn't really about dtos and api contracts and in fact when you're building apis a different thing is coming in a different thing is going out usually so what's the point of using we're just left with c why are we using an nvc thing and we just normalized that yeah this is this is fine this is acceptable it is not but the reason why it is is that it was actually the only viable way to build apis and yeah someone might say hey nick nancyfx was a thing i was using it it was great well the truth is for it to be dead not enough people were actually using it and also people are just prone to not adopting something that it isn't supported officially by microsoft minimal apis are and now controllers are as well and we're gonna see how that goes as the time moves on they also have these methods these actions that never really call each other you would never have one action call another action and if you're doing that don't and the reason why they exist in that controller is because it made sense because in the mvc world the view would actually refer to both the controller and the action by name or sometimes just the action by name so you'd have entirely sense it would make sense that everything would be grouped logically because it isn't the black box that is calling those endpoints it is your view so why are we using that again and then the kind of low-key violating responsibility because you might think it's not because you say hey nick my domain is the customer i have customers controller customer controller and everything is in there my create my update my delete everything is there no violation well i would argue that it also kind of violates open closed on top of that because every time you want to go ahead and add an end point you have to go and change that existing class that has nothing to do with you adding an end point or deleting one in my opinion they should be separated in separate endpoints so let me just quick intermission because i'm a bit tired okay so they also have these services injected into them into the actions or the controller actually but they're not used sometimes by all the actions so you would allocate an object in that controller being instantiated and then you would waste all that memory for allocating that object and have to garbage collected as well even though it's not used by all actions so why are we doing that and someone might say hey nick i'm using the from services attribute and i'm only injecting it in the method itself if you're doing that great i haven't really seen it all i see is the lazy way of just go with the constructor and just hope it we don't care about memory basically um and also they never share state between action so why are they sharing real estate because they're all scoped anyway and you wouldn't call one another so what's the whole point of having them together i don't get it and when you have private methods they usually are only called by a single action they wouldn't be called my multiple actions most of the time and middleware and filters that you have because it is coming from nvc a lot of that was actually just left in there from mvc and for example validation is called everywhere so you have to be comparatively slower than minimal apis as well because you have to invoke and allocate those objects and even if it's just a nif check to walk out of the flow uh minimal apis have a 10 to 15 realistically percent performance boost over controllers i won't dive too much into the performance point because performance is contextual and it doesn't mean the same thing for everyone like if you have a database that takes a second to respond minimal apis won't just magically make your apa fast um they can also grow quite a lot and we have this questionable term called fat controllers and we just do everything we can to just remove stuff from those controllers because someone is like so proud when they have oh like my controller is just three lines of code that's how simple it needs to be well how do you how about you remove that controller together if it needs to be so simple you don't need it move the mapping outside send the thing to mediator which is what you're going to do anyway and just move on with your life so this is what it can look like for a new developer working in as a junior developer or as a seasoned developer from a simpler ecosystem like node like python like go so they just want to build an api but then they have all those things dropped on them and it can certainly be overwhelming and sometimes we forget that some people just want to build apis they don't want to build sorry just want to build products they don't necessarily want to build your clean architecture with three layers and onion and hexagonal and all those fancy words that you probably don't need so what is this talk about really well like with everything in this life it's all about lego it's always about lego i promise it will make sense what do you see here just silently in your mind think about what you see here if you say you see a half-finished lego set of hogwarts you're wrong i mean that's the right answer but that's not what i'm getting so what you have here is the foundations of some castle i can see some windows i can see a door there is some structure to this thing it's not finished but there's certainly some structure i can also see a window this way and there's no back wall now what's great about this structure is that if i give it to my nephew who's five and i say completed just because of how much foundation there is there and how much work has been done by default they will probably finish with something like this i mean they're five so maybe it looks a bit worse but something around that but also me who is not an architect and i don't know how to build castles if i take that and i'm i've done my fair bit of lego but i'm not a castle expert i can still create a fairly good looking castle something like this now let's take a step back i go to my nephew again and instead of giving them an unfinished castle i give them ground and i give them a backup a bucket with infinite amount of lego of any size any color any length and i say build a castle well they're gonna probably build something like this now does it look like a castle if i ask him he'll be like yeah he's certainly a castle i can see a door i can see the ground there's a torch somewhere there it is amazing now i wouldn't say it looks like a castle i think it looks bad i wouldn't tell it to him because he would cry but at least he's engaged and he got into using lego and to his eyes he did build a castle and he can play with a castle and you know what next time he builds another costume maybe maybe it will be less shitty but if i go to my uncle who studied architecture and i gave the same tools then most likely he would make a masterpiece do you get it that's what that's what minimal apis are all about this is what controllers look like like to me there's a lot of opinions about something in there there's a lot of stuff on how it should be built your model validator your model state how you can extend that specific controller base how you need an api con controller attribute all of those details on how you should structure your project and what it should do by default are there and what's great about that is because a junior developer most likely will build an api that looks like this because once you have that controller set up it's very unlikely you're going to fundamentally mess it up but someone who's mid or senior as well can take the exact same thing and build maybe a bit of a of a better one uh of a castle but again a nice looking customer but here's the thing with minimal apis if i break down everything to the bare minimum and i go ahead and i give them to a junior developer or someone who has never built an api before but all they care about is to build the product and the product happens to need an api and i say look how simple it is to build an api you have all of these lego bricks and a ground go ahead go crazy they will be happy because they did build an api that works for them is it a well architected api no does it matter if it works for them no because next time they're going to build it it will be pretty good but what happens with someone who's a true senior staff principal whatever word they invented today for calling very similar engineers if i give that to one of those people one of those architects then they most certainly will build a masterpiece and a masterpiece that works for them and that's exactly what minimal apis are about it is about you it is about choice so let's go back to the first question what are minimal apis well to me minimal apis are an opinionated way to build high performance apis in dot net with the minimal amount of implicitly added features and the maximal amount of flexibility to explicitly obtain the features and the structure that you need it's about you it's about choice so let's see what is possible when you can have that sort of power in your hands let's go back to the first demo actually the second demo and i'm gonna go ahead and i have this api over here which i'm not gonna show you what's in there but i'm just going to run it and all you need to know is that this api is a customer's api microservice and it is a rest api so i'm just going to make this smaller i'm going to expand this and i'm going to show you the end points so you have your classic rest get all resources in a given api thing so no customers in my system this is using a real database by the way with the right responses and headers and everything then i can go ahead if i want and create a user so let me just mess up my email a bit so that is definitely not my email but if i am to create a user and let me just find it here you can see that a.com is not a valid email address so i have full validation here i can also for example use the date in the future and try to call that and it says your date of birth cannot be in the future so full model validation and domain level validation for my thing um that's definitely not when i was born now i'll be ancient uh and i'm gonna go ahead and create me and i can go ahead and get me as well from that getting point so i'm back i can also mess that end point a bit and point to something that doesn't exist and i get a 402 exactly as you would do in a rest api if you say that this should return 204 you are wrong and i can go ahead and update this as well so my username will be different so i have exactly what you need for rest api and then just to wrap this up i'm just gonna call delete and i'm gone 204 no content the user was deleted and if i do it again 404 resource not found now let's take a look at something else i'm going to go to swagger i'm going to refresh this page and this is the schwager please move okay and this is a swagger from my api now i have to look at this because it wasn't updating and you can see my endpoints here documented creates a new customer in the system i have my request body i have the success code so 201 400 get delete yet again and put everything is here with exactly what you'd expect from an api that respects itself now how is that done with minimal api technology because all of that that you just saw is built with minimal apis now here's the api remember apis minimal apis are not about the minimal amount of code it is about the minimal amount of setup code and stripping down everything to its bare minimum so for me an endpoint looks something like this all i need to have is a class an endpoint class which for this to be registered all i need to do is create it and then i extend the endpoint class i slap an http post here and allow anonymous and then i have my request and my response very explicit very descriptive i can inject any service i want in here and i only have a single handle method because that thing is supposed to be one thing and one thing only to handle this create customer request and then i'm sending that to a service because i don't need mediator in this case and then i'm just returning it back as a created ad with the like the right location header and all that and if i wanted to add an extra endpoint to my api i just create a new class extending the same things now i also have validation for those things and again those are implicitly registered all i needed to do is extend the abstract validator which if you are familiar with fluent validation this definitely rings a bell and then point to that request over there so that just magically works with code and then last but not least the summaries those are what is used for the documentation in swagger so if i went ahead and i deleted that then that documentation would be gone and again i didn't have to explicitly register any of that now how this all hangs together and you can find this code by the way on my github under a clean minimal api which is ironic is like this i have a package installed called fast end points which i helped steer the direction a little bit uh because i found it very interesting and it's what i wanted to do with minimal apis as well and i was glad someone else was doing that and i can just help with that and then that's all i need to do to register my services and then this is all i need to do to register my endpoints and that's it i'm not cheating by the way all i have in here is just some error response mapping to a validation contract which again i can just extract to a different class but that's it so i just want to recap this is still technically using minimal api code now you might think that this is too much this is a lot this is bloated however it is the path i chose and this is the half the custom i decided to build and that's my choice which is what minimal apis are all about so back to this let's talk about something that i call minimal valley and it's definitely not the danning trigger diagram with change labels it's totally original i made it so on the horizontal on the vertical axis you have the viability of a project of of a specific type of project using minimal apis and then on the horizontal you have the type of project the way i imagine it when it comes to project lifetime because i think projects start in a certain way and they end up in a certain way if you have enough money but you can also start at any point it doesn't have to go through all the life cycle so first beginner projects it makes so much sense to use minimal apis for beginner projects it's a no-brainer you can just start with just four lines of code and you have something there so we established that also demos as you saw on the first demo very easy to demo an api using minimal apis and also proof of concepts it's great to have a piece of technology that you can just start building something with and then convert that if you want to something that can go to production and be very very very performant as well now where i don't think they're going to have an international career necessarily is those chunky enterprise apps now those are not limited to monoliths beyond saving modular monoliths can also be part of this but i think that just because the type of company the type of developer who works on those projects is a specific again type of developer it doesn't make much sense to move from one thing to another waste all of that knowledge that exists in each company and for that developer as well for something that really isn't significant because yes even if you did move to that other structure you wouldn't really gain much if you took that risk so i definitely don't see that being adopted there in my opinion for good reason now if you make a new app and you say we're going to give that a go then maybe try it but i just can't see it happening but where i do see that something amazing will happen is microservices now microservices are small by nature is in the name microservices so minimal apis from microservices just makes so much sense for me and it also allows developers to work on microservices to choose their own destiny on the structure i want to wrap this up with the important the importance of minimal apis for net because i think it goes beyond the code and beyond everything we talked about until now i want to talk about how people get into a programming language in general so first they have an idea they want to build they don't know what programming is that i'm just going to make a game i'm going to remake tetris i don't know how do i remake tetris on the programming language what's the easiest programming language to remake tetris then university school quality just have it as part of the classes and someone might be like well that programming thing sounds good i'm going to give it a go and you just depend on whatever the university or school decided for you to learn then curiosity is a beginner this is how many people are starting i want to get into programming how do i program how do i make a calculator and it's all downhill from there or hopefully uphill um curiosity is an experienced programmer is another one this is how i usually nowadays get into programming languages because of how many features are added in c shop i really want to see where they're getting inspired from spoiler alert it's usually f-sharp but other languages as well like kotlin rust are a heavy influence to the c-sharp team so i'm getting into those languages to have a more spherical view on how things are added into c-sharp and then last but not least just because you get hired into the company that is using it this is how i start with c-sharp i desperately need for a job i was doing java beforehand for some reason my future would be building desktop applications in java maybe not the smartest idea but someone said here's a job so i took it thanks phil you kinda changed my life but this is in general how people get into programming languages there are this as well but i'm going to focus on this you know a parent is also another one and so on but we're going to leave that outside so these three are the ones directly applicable to c sharp now you could say no nick three and one is also applicable because of unity the game engine and that is true but unity the game engine if you've seen c sharp from unity the game engine you wouldn't really want to see that code trust me it's it's weird it's a different thing it's not the same thing it kind of gets there but it's not the developer you would necessarily hire and it's not the developer that would want to work for apis anyway so let's leave it outside so this is what we have but this is the one that is actually really on our own destiny our own hands we can choose the destiny because universities schools colleges they can change which language they teach and companies they can change which language they use so curiosity's an experienced programmer is actually the one thing left again outside of unity on how people can get into the language and even unity if they decide to move away from c sharp which i don't think will ever happen but if they do then we lose that as well and that is bad and the reason why this is bad is a train so let's talk about trains this one would won't make so much sense as the lego i'm just warning you so people get on board the train people stay on the train and people get off the train and the train will keep making the journey as long as there are passengers the moment there's no passengers the train will just stop doing that because it doesn't make any sense for the journey to be made so let's talk about the programming language express a people pick up a language b people use a language and they keep using that language and c people stop using that language now a language is dying when a plus b minus c is decreasing over time which makes sense the more people that ditch the language or the if the people ditching the language are more than the people picking up the language and using it then we kind of have a problem and there's a bit of a catch-22 when it comes to programming languages as well because the more people use a language the more people will use a language and the less people that use a language the more people will stop using a language which is how it's an interesting dynamic but it is how it is um so c sharp is good at b and c but a is a bit stale people picking up the language especially in the context of asp.net core and apis and that is not c-sharp to blame because c-sharp is a great language people love using c-sharp and they just stick with it for years upon years upon years but we need to make it more accessible and make more people come in and say hey we're cool too you can also have an api very quickly or you can also have a system that you can deploy in the cloud very very easily c is inevitable as people retire like it makes so much sense for net being 20 years old that you have some people doing it for so long that they can just retire and they don't work for it anymore minimal apis are ultimately a step towards increasing a to get more people picking up the language so when i hear things like the dotnet team should not be working on things like this this is not for me why are they wasting my time this is not what i asked for this is not what i need well if you want to be able to hire the next generation of csharp and dotnet developers than it is for you even if it's not for you thank you very much and keep coding do we have time for questions uh sure we have nine minutes so if i can see anyone so if you have any questions ask me now if not we can just call it here and you can come over and ask me okay let's do the second [Applause]
Info
Channel: NDC Conferences
Views: 140,199
Rating: undefined out of 5
Keywords: Nick Chapsas, .NET, API, .NET 6, NDC, Conferences, 2022, Live, London
Id: hPpvlKLeYYA
Channel Id: undefined
Length: 52min 1sec (3121 seconds)
Published: Thu Jul 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.