Dependency Injection Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up what's up what's up what's going on everyone david good to see you my friend uday welcome welcome welcome what was that didn't expect a dependency injection rap concert i wrote a rap just for this stream too i'm about to break it out uh actually how was my uh how's the music level is it too loud too low just right i hope it's just right i'm still working on my my audio levels here as i make the the switch from twitch to youtube uh made some changes and hopefully you can just barely hear it right but yes dependency injection rap concert that's a great idea we should do that awesome awesome so how was everyone doing today today what's today today tuesday yeah today's tuesday so i am streaming today because normally you know i like to stream on thursdays uh but i'm actually taking thursday friday off uh got some family visiting from out of town and it's spring break for my kids so i'm taking thurs uh thursday friday off so i won't be streaming on thursday but you know i had a thought tell me tell me what you think about this okay so normally i i try to stream once a week on thursday but i work on a lot of interesting things well to me they're interesting they may not be interesting to you but i work on all kinds of different technologies and tools and products uh from writing samples to writing uh content to structuring documentation tutorials to learning content to support to marketing content to i mean i write product like a vast vast responsibilities and i started thinking to myself i'm like you know what what if i just stream when i'm working like just stream what i'm working on like for example i have to uh you know have to write some uno apps for a new uh for a new preview uh product for uno platforms like you know i want to write some some apps that deal with reading david uh data david from reading data from uh covet covet data like create a cova dashboard charting graphing app uh and uno i was like you know what maybe i should stream that you know i i help customers write samples and stuff sometimes like you know what when i'm writing those samples i should just hit the stream button and just do it live like i don't know i don't know if anyone would find that interesting you know like uh oh i gotta write this documentation that's fun i should show everyone how i write documentation i don't i don't know if you would find that interesting but what do you think about that should i just uh should i just hit the stream button whenever or should i have a set time or maybe a mixture of both you know ruba fix 989 go ahead i might just do that i might just do that one lube one hello my friend thanks for joining good to see you so uh so yeah i'm thinking about it i'm thinking about just i'm just gonna hit stream whenever i'm working on something and if someone's interested they'll uh they'll join and if not they'll ignore me and it's no big deal uh but yeah i do whatever maybe i'll give it a try stream it stream it i need a i need an alert for streaming stream it alright so today i've wanted to follow up on a question i got uh last week uh last week i got a question about dependency injection someone's like hey could you do a stream on dependency injection because i still don't get it i'm like yeah i could do like a very basic uh concept of what dependency injection is you know i don't want to get into like all the different containers and what they can do and all that stuff but basically i wanted to talk about the uh the very basic concepts that make up dependency injection and kind of point out that you're probably already doing it you just don't know right that's my favorite part my favorite part is showing something you're doing like oh what's this you're already doing it you just probably don't know that's what you're doing so uh let's go ahead and get started shall we let me open up visual studio switch to my uh my desktop there and what's the best way to do this probably let's just do a console app yeah console app let's do a console.net course fine i don't care what i call it i'll call it console app one and be sure to ask any questions you have in the chat now would be a great time to like this video because apparently there's some algorithm that has a whole bunch of likes uh it shows up so that would be cool and oh crap i don't even know what i named this stream what did i name this thing okay i named it i was worried i didn't name this mohammed hello my friend good to see you okay so dependency injection that is a fun topic isn't it well let's kind of break this down because there's there's another concept that we have to talk about uh before we get to dependency injection and uh let's start by just creating some objects uh what should we create let's create i know let's let's create a uh a person service class right so we have this this class person service uh you know it has a constructor we do stuff in it something like uh do stuff something like that right whoops didn't mean to do that okay i forgot how to write a method all right so most of the time what happens is let's say you have this class in your application right and in this application you're using it like all over the place so in our case we're just using it one spot uh but pretend this is like we have lots of different objects in our application lots of different views our view models or controllers or whatever you want to call them lots of these different things using this person service right and so everything's fine and dandy and we're using this and let's actually let's have this do something let's have it uh i don't know should we do like console.writeline or something like that right line uh i did something that looks that looks fine okay uh daniel says thank you uh brian explain everything so that i can even i can understand it uh i'm gonna try my best i might try my best uh now let's say we call this service and we're gonna call do stuff save control s okay there we go let me run this app real quick and we can see that i did something there it is right there at the top i did something okay so our service works and we're using this everywhere in our application right now we have a problem here is we're newing it up right so you probably have lots of code that news and object up this is very common in ooo right uh and i i want to point out that dependency injection is not c-sharp specific right the concepts i'm talking about they go cross-platform i'm just using c-sharp right but the concept does not change depending on if you're in java or c plus plus or you know uh javascript or whatever like the concepts are the same okay these are patterns these are design patterns and design methodologies okay okay sergi will you tell us about ioc containers yes i will tell you about that okay so now we we have this this class of service and it does stuff and throughout our application we could be calling this like 20 times everywhere like just we have this massive app and we are calling this thing everywhere okay well now what happens is we need to uh we need to change some logic right we want to add a logger okay so let's go ahead and add another class and i'm just going to call it logger okay uh now this is gonna have a method called log and we will console.writeline i logged something okay so now what happens is we're like we have a change we have a change in our application uh something happens like you know what i want to start logging uh things that happen in this service right like something happens in this person's service and and i want to start logging it so our first instinct is to do something like this oh well i'm just going to create a logger whoops new logger and then i can say logger.log okay so this is your first instinct we're creating a new object right we have a new object here we're newing an up an object here uh and this this isn't great we don't want to do this and the reason we normally do it this way is because if we were to do something like this it's like oh well i can just pass it in right i'll just pass it in like this and i can set the logger equal to our logger well now what we've done is we've broken our application like it's it's completely broken we have to go to everywhere that we are newing up this object manually and we have to create a new logger whatever we do whoops but we have to create this object and now we have to pass it in and we have to fix that code everywhere we're using this logger so we normally don't do that we'll normally oh well i don't wanna i don't wanna break our code so you know i'm just going to go ahead and create this logger instance here but the problem with that is now we're creating this coupling right we're creating a very tight coupling between the the person service and this logger meaning that wherever this logger exists we have to have a hard reference to that logger so if i had another class library here this would have to reference that class library that contains that logger and what if i have different types of loggers you know what if i had a logger for debugging what if i had a logger for data analytics or something like that what if i had a logger for telemetry data for usage of the application what if i had a logger that logs exceptions like i have all these different loggers but i don't know which one i want to use like whoa am i going to say hey var uh t logger for my telemetry logger right wait am i going to have a different logger for everything no you're not going to do that how do i swap my loggers out what about unit testing like i want a unit test but this is using my db logger this hits the database and in my unit test i can't be hitting my database like i need to i need to be able to switch this logger out right so by doing this we have created a sandbox when i don't say in sandbox i want to say something like we have boxed ourselves in right we we have imprisoned ourselves into a very tightly coupled not flexible not maintainable uh uh architecture in our application like this is not good like i would never i never knew up anything in my apps okay you'll never see new for the most part in my application i won't say never but for the most part so then you're like oh brian well i know what i'll do i have the perfect solution i'll say static class ah that's it brian i'll say static class and then i could just say logger dot log well as soon as that's static then i could just access the static part right oh yeah this is looking good brian i feel good about that no no no no no don't do that static is the devil unless you like the devil the static is something you don't like okay so static this is kind of referred to as a singleton pattern and the singleton pattern is not it's kind of like an anti-pattern it's when you're writing the code you know it's like oh this makes it very easy for me to write code because i can just make all this stuff static uh and then i can reference it anywhere in any class or object that i'm that i'm working with uh but that comes with a ton of other problems okay ideally what we want to do is we want to provide the person service with the type of logger that we want okay and to do that what i like to do is i like to start creating interfaces for objects that need to be injected i'm going to say injected for now okay they need to be provided okay so for example i would start out with an interface of i logger and it would have uh that single method on it uh what's it called i'll call it log right and then this would implement i logger okay perfect all right so all i've done so far is i've created an interface because i'm saying hey i don't really care uh what object i'm passing in it just has to know how to log so this interface is giving that contract for that class so the implementer will say i know how to log i can do that okay so now we have a class that implements i logger now let's talk about a concept that you already do now so we kind of talked about what happened if if we didn't do it this way right if you don't want to new up inside the object which is really bad you don't want to do this right we don't want a new up that means we have to pass it in we have to pass in the instance of the of the logger right so i'm gonna say i logger logger and then we can set whoops now we can set our our logger equal to our logger okay so this is pretty good but now we've we've broken the entire app however what we have done here this right here you do this already this is called inversion of control okay you are taking the responsibility of creating that object or that dependency and you're you're you're putting it somewhere else right before we were knewing it up inside the class so this class the service was responsible for newing up its dependencies and there's a few problems a lot of problems with that but one of the biggest ones besides testability extensibility maintainability all that stuff is that you have hidden the dependencies of this class you don't know what this class requires to function okay so what we've done here is we said hey this is [Music] this is a dependency that this service needs in order to function we have made that clear in the constructor so now we know whenever we new this service up we have a dependency that's required and this dependency is going to be provided by something else that's a version of control so what we would then be responsible for is creating that logger and then passing that logger in okay now the app's gonna work the exact same right right here i did something i logged something uh the difference here is that we have created a a an abstraction right we've created this abstraction between the service and any dependencies it requires now we did break our code by doing that right it's like ah crap now my code doesn't compile i gotta go fix all my my errors uh and then i gotta create my new logger and they're gonna say you know well what if i have a single 10 logger like do i try to use a static here uh you know we may have to go back to the static logger but the concept here that i want you to understand is what you've done here is a form of dependency injection okay you are providing that dependency yourself you are just newing it up right so you're creating this dependency and you're passing it in but this is a form of dependency injection because you are injecting that dependency right you said oh this service has a dependency of ilogger and you don't care how this works right so for example let's say a class another logger but it implements ilogger right uh let's go ahead and implement hey how come i don't have a oh do i have to be on that control dot yeah implement interface right so here i'm a cop oh i misspelled that why didn't someone tell me i misspelled that right so i have another logger uh i logged something again and now i can come up here var another logger equals new another logger and instead of the first logger i could pass in that logger my code still works nothing's broken uh i just changed the logger i'm providing so now i'm i log something again right but i i created that dependency and i'm providing that dependency so i have abstracted away the responsibility of creating this logger object onto something else right now that something else is me right hey how you doing i i can't read that kanji unless it's chinese i can't read that i'm sorry uh but welcome my friend okay so this is where we start kind of talking about uh dependency injection with a dependency injection container okay so everyone kind of has heard you know containers right a di container well the concept of a di container is actually really simple think of it like a dictionary of types right all it does it it's a dictionary of types and when you ask for a type it's going to create it for you automatically it's just going to give it to you and it just so happens that if that type that you're requesting has dependencies too well it's going to create those as well and it's gonna keep going down that dependency chain until it doesn't find any more dependencies okay so that that's that's all it really is and so if you wanted you could build a di container yourself a very basic di container just you know have a dictionary of types and when you ask for type you can use create instance activator dot create instance to create it for you right uh but there are there there are much better containers out there that we'll we'll play with okay but this is like the basic idea is we're taking these dependencies right we're surfacing them in the class like we want to make sure that we are aware that this class has these dependencies and are required to function we also want to make sure that we're using interfaces for those dependencies because this allows us to swap out the uh the implementation and you may be saying brian i've never had to swap out anything and then i'm gonna say you probably don't unit test okay you probably don't unit test because when you unit test you have to mock your objects you're going to mock your dependencies and pass in fake dependencies that you can exercise and test to make sure your code is working as expected so for example you know in this case this logger this might be production logger here so when i'm running the app i'm using production logger now a really easy way like if you were just hacking this you could say hey if debug oh no if not debug if i can spell which i can't right we're gonna do that logger uh else we're going to do this logger now to do this we're going to do this we're going to i logger logger and let's do this boom this is like the hacky way right if you're not using a di container or something like that so right now since i'm since i'm in debug mode i'm going to use this test logger so i'm going to run this and look we're using i log something again so this is my my another logger instance but if i swap this to release mode and run it we see that i have an error using just my code continue debugging i don't know what that means can i not console log can i not console in release mode i've never hit that before you are debugging disable just my code and continue stop wait you're debugging and release build using just my code with release builds using compiler break points will not be hit i'm not trying to hit uh continue debugging okay i don't know what that is but it worked sorry that that was confusing uh so here you can see that we're in release mode so we're using we're using our release logger which is i log something right so this is just like a very basic example but i hope it kind of drives home the point that depending on some condition being a unit test be maybe you have a debug build versus a release build versus a test build so you can create different builds and like here if an asp.net mvc app you can have different environments you target right production a dev staging uh well you can have different implementations based on those conditions like oh is this the staging server oh am i am i the staging configuration well then i want to make sure all of my dependencies are staging or dev right and when you hit release mode it's like oh now i'm going to use the debug logger right if i'm in debug mode so that is conceptually how we want to approach this uh it asks you to use the debugger and release mode yeah i think i just hit debug in release mode and saying hey you're trying to debug release mode uh this isn't gonna work i think that's what it was saying i've never seen that before actually that's the first time i've seen that uh okay but i any questions so far are are we all on the same page up to now okay so big takeaway our class should abstract away as dependencies right we're not creating these inside the class we are we are putting the responsibility of providing these uh these dependencies onto something else now remember when i said i said you know what you're probably using these techniques and you don't even know it you're using these patterns and version of control and things like that and you don't even know it if you have ever written code where you pass in a dependency like this you're using a version of control you're using that pattern uh you are injecting the dependency yourself so you're using the dependency injection i use that term very loosely uh but you're using dependency injection it's just you're creating that dependency and you're injecting that dependency into the object so you're using this pattern you probably just never realized it okay so now this is where dependency injection containers come in okay because now you start asking yourself like well brian you know i have done this but there are cases where i only need i need the same instance throughout the whole app and so that's why i use a static right i use that static logger because i need that instance to the whole application well if you are using a static logger i hope you're doing it like the way we did it logger.log right you're passing in the static instance uh and not using it within the actual class itself right because you don't want to hide that and statics are very very difficult to test right but that's where dependency injection containers come in because they then manage the lifetime of these objects for you so not only will they create them for you okay they'll let you control how they're created you're going to get a transient meaning are you going to get a new instance each time are you going to get a singleton are you going to get you know the same instance each time uh do you want a scoped instance right so there's i mean there's lots of options uh but for the most part they're pretty simple so let's use a let's add a container let's add oh crap what container should we use how about unity unity is a pretty pretty well-known container uh package king's union container okay yep so let's use the unity di container now there's all kinds of uh containers okay so what's a scoped instance okay so a scoped instance means that the instance will be created scoped to a certain resolve process i don't know if that makes sense so basically let's see if we can kind of get into that let i'll come back to that michael i'll come back to that uh because like for example prism uses scopes okay and let's start with the basic stuff okay first off uh let's create i unity container it's been a long time since i've done i unity container and we'll call this container equals new unity container okay first off when dealing with containers there's some there's a concept called your route and you always want to have just one route where your container lives and from that route everything is created okay everything is created from that point forward so you don't want to be passing your container around it to other objects for example if this logger i mean yeah if this logger needed to resolve something you wouldn't pass in the container to resolve it and so i'll get back to that okay so we've created this container and remember the rule is you know are not the rule the concept is that a container is just a dictionary of types right high level rule that that means that that dictionary needs to know about your types you have to register your classes with the container so it knows how to compose them how to resolve them so usually off these containers there is a register method so in the case of unity you have register instance register factory register singleton uh just uh register type those are the ones it provides okay so let's just start with very basic register type now on register type you're going to have your interface which is ilogger and then you're going to have uh you're going to have the implementation such as logger now okay so what i'm saying here farisa you're a great teacher hope you consider doing videos tutorials about advanced subject in c sharp like that tell me what you want to learn my friend i'll do my best to uh to to create some content about that okay so what we have done here is remember conceptually we're dealing with just a dictionary of type so we have to add our types to the dictionary okay so in this case we're going to use the register type method on our unity container to let the container know about our types what i am saying here is i'm saying hey unitycontainer whenever i ask for ilogger i want you to give me the logger instance right create an instance of this logger and give that to me okay so for example if i wanted the container to resolve my service my personal service i'm going to say hey container whenever i ask for i person service which we haven't created yet give me person service okay so let's go ahead and create iperson service really quick i person service and uh we'll put what should we put on here do stuff and then we'll implement i person service okay we're starting to create these abstractions ali good to see you my friend thanks for joining right so now instead of doing this newing up stuff right i'm gonna i'm not i don't want to new this stuff up so first off let's simplify our code just a tad let's simplify this so we can build upon that knowledge okay so now that our container knows about our types here okay we can then start asking the tater the container to create stuff so for example i'm going to comment these out and let's just start with the logger and i'm saying logger.log so what i'm going to do now is i'm going to say hey container dot resolve i logger right i'm asking the container i'm saying hey whenever i ask for i logger what's it going to give me it's going to give me the logger dan my friend dan the man explaining dii you reviewing your notes for me explaining yeah funny okay dan dan's a joker he's a joker okay so whenever i'm asking for the logger i'm going to ask for the ilogger and what's it going to give me well because of my registration it's going to give me an instance of the logger so let's go ahead and run this here's the app i logged something okay great it worked okay nice uh but brian how is that different from just saying new logger well it's a lot different so now let's let's comment this out and let's move on to the service okay because well actually let me back up just a little bit let's uncomment our service and i'm going to run this and comment that out because the container created that logger for us and we're passing the logger in manually right i did something i logged something works the same well here's the real power of this is we're going to take this service and now we're going to say we're going to have the container create that service for us right so hey resolve i person service so once again i'm asking the container for i person service and since the container knows because i told it i said hey container whenever i ask for iperson service i want you to give me the person service instance okay so when i get when i ask for iperson service i'm going to get the person service so let's go ahead and run this and it worked i did something i logged something did you see what happened i'm a break just for a second i'ma pause for a second because something happened here very subtle that you may not have noticed from when we were manually creating this dependency when we were manually newing up that object what did we have to do well we had to pass in the logger i'm looking at this constructor i'm like huh that person's service needs an eye logger but we didn't pass that in right we didn't do new person service passing in the logger we didn't do that weird but it still worked yes it still worked because our container knows about the logger right our container says oh hey i'm resolving this iperson service okay oh hey look this person's service has the dependency of ilogger and it's going to go back and say hey i know what that is i know what an ilogger is you know what i'm going to create that for you and i'm just going to pass it in because i know how to create that so it creates it for us right and then our app continues to work but we didn't have to new any of that stuff up ourselves okay hold on dan if you use a useful container yeah we're not getting into advanced stuff dan okay we're keeping this simple very simple angelo good to see you from brazil all right this is the basics this is the basics dan we're not getting uh super complicated here all right so now check this out now remember that problem so i want to rewind re rewind all the way back to where we had uh we had that problem where you know what we added something to the service right we added something to this constructor uh and we broke our application right we broke it uh and we had to go throughout the entire app like however many hundreds of views we had or objects that were using this service and we had to go update uh we had to go update that uh that that constructor and create the object and pass it in right so what i mean is like okay before let's say we have another class i don't know uh i dependency right i dependency we'll just call it idependency uh it's not going to do anything and then we have public claw why'd i say public nothing in here is public class my dependency implements i dependency okay so now for example this person's service sometime down the road right somewhere throughout the history of this app we had to add functionality because you know that never happens uh but we had to add functionality and so we had to add another dependency to this to this project now so i i modified our constructor right to add a new dependency because this dependency is going to do something whatever it is is going to do it uh well what's weird is we don't have any compile errors right that's because we are not newing up any objects anywhere we are you know relying on the person service to be resolved by the container right our containers doing all the heavy lifting so but what happens if i run this as is what do you think is going to happen oh got an exception why did we get an exception well for one it doesn't know anything about our dependency here it doesn't know about this so remember that unity container or that di container that dictionary of types needs to know about our types so we can say container dot register type i dependency and when i ask for eye dependency give me my dependency class instance okay so now that it knows about that dependency it works just fine right it resolved this dependency for me as well so i was able to extend it this class add new functionality whatever that functionality did calculations or or whatever but i didn't have to go fix all these different areas of my code anywhere the only code that changed was scoped to this class right right here and then whatever logic i had to add and then i have to go update my unit test because i have to test this new functionality i just added right but because my container knows about that well then it can just do it for me right dan says advanced stuff is where all the fun is at benjamin hi brian two in a row yeah okay so hackle says how how now to do the different loggers with di containers so your loggers you could change your registration a number of ways first a lot of these uh a lot of these containers allow you to register with by convention you can do it in code you can do it with like some type of xml file config file uh so for example you could still do the the if def if you wanted to right hey if i'm in uh debug mode i can't spell you know if debug i'm gonna register that way else give me another logger did i delete that oh i deleted that right uh but that's one way to do it right the trick is you know if you use something awesome like prism you would get a container resolution exception yes dan yes if you use something awesome like prism you would get great information about errors you would get right okay the point is the container or using di gives you the flexibility to swap out your dependencies depending on your scenario so for example in unit tests you would you could mock your these interfaces with objects and register those objects with your container that way when your container resolves them then it would use your mock instance instead okay and we do this in the prism uh tests all over because it depends on on uh on di so linus says how how is it able to create the instance without you providing it that's what the di containers do that is their only job their job is to know about you know your dependencies your or your your objects right and you don't have to use interfaces you don't have to uh i would always recommend it uh but you register your types and then it knows whenever it runs in the into that type it's gonna say oh yep ah i need to create that and since it already knows about it it creates it right so when it looks at this object it's creating it looks at a constructor uh and just a just a a tip if you had multiple constructors on here you know like ctor and you had one like this right the di container is always going to take the constructor with the most dependencies or the most arguments here okay this this your parameter list here it will not resolve using this okay so i'll hit this break point we'll go ahead and run this and it failed because would i do it oh what did i do with iperson service it's because i had multiple it didn't know which which didn't know which constructor to use now there are like dry ioc it automatically takes the constructor with the most uh parameters i think unity makes does it crash it looks like unity just throws up i haven't used unity in a really long time because i've been using dry ioc let's run this again i don't think it would give me the actual error yep fail to select a constructor so unity throws up if you have more than one constructor i think you can add rules to unity uh but it's been so long since i've used it i don't i don't remember how you would do that but uh dry ioc for example dan correct me if i'm wrong but dry ioc will pick if he had multiple constructors i don't think it throws up i think it actually picks the constructor with the most arguments but i don't remember that all right uh linus you mean how does it know which constructor to use oh oh oh okay sorry i misunderstood linus question okay so i'm going to remove this because i'm using unity and it doesn't like that and now it should work again oh i removed the eye logger oh i broke something let me undo oh there we go there we go i loggers back i person's back okay we're back to where we're perfect yeah dan says generally they pick the constructor with the most arguments prism configures dry lc to pick other one that's able to resolve everything for assuming it failed yeah well we're not talking about prism today prism's special because prism's uh an advanced framework for building apps and so we do a lot of cool stuff to handle those things but if you're just getting started from scratch right these are things you have to consider okay so the container knows how to resolve all this stuff now like you can do really cool stuff with the containers and most of them will support the same features so for example let's say that you had that singleton instance of i logger that you wanted okay uh so for example you could say register singleton now it it's hard to tell but for example let's go to this this class this my dependency uh and let's add a c tour and this c tour is gonna take i logger two okay so this is going to take i logger uh let's add something so we know we're dealing with the same instance how about how can we know we're dealing with the same instance maybe like a date time maybe uh let me add a property on here date time actually i wonder if this will be if this will happen too fast i always forget that there we go i always forget if this is gonna happen too fast we'll find out if this happens too fast uh but the idea is i can register the singleton okay i'm gonna run this this logger okay 43 seconds crap that's not going to work because oh no that's i got to set it in the constructor because this is going to set that on the yep that's going to set it every time i request it don't want to do that needs to be set one time there we go there we go fix that okay let's do this again okay logger 17 logger 17. okay so we have the same instance just to confirm this i'm gonna put this back to just register type which should give me a new instance oh you know what i should probably do probably like a random number like a random int instead of time that's what i probably should have done so 41 yeah see this happens too fast so let's not use date that was a bad idea brian okay let's like ant uh number whatever oh a good even better even better thank you oliver uh string or i could do good type now let's do string let's do string this is a string uh we'll call it good now we can say good equals gooey dot next is it next or is it new good i forgot i haven't done i haven't used a good in a long time okay yeah new guide all right man i have not used that in a very long time all right so now hope we can read this it's gonna be so hard to read this oh what i got zero zero zero that's not gonna work does anyone remember the syntax for a new good anybody is it like goodwood i thought it was like good.new something is that not oh angelo oh thank you my friend system.guide.new there it is thank you nukoit dot 2 string and that was the hardest part of this demo okay all right let's try this one so our good ends in one four one one this one is different yes okay yes thank you oh thank you thank you thank you okay thank you angela i appreciate that thanks for the help all right so because we said register type it gave us a new instance each time however we could say register singleton and this will make sure that it's a singleton so this is this is analogous to your static approach right you're gonna get the same instance each time okay so we'll run this we'll see the logger here ends in cbd here's our logger ins and cbd so that is the same instance all right now besides like just registering singletons you can also register instances like so the difference between like a registered singleton is like the object won't be created the first object will not be created until you ask for it right so right now we're just registering it and we're saying hey whenever i ask for ilogger i want you to give me the logger and anytime i ask for i logger after that you're going to give me the same logger instance right i'm getting the same instance each time however we can do something like this so we can say you know i have an instance of a logger that i care about uh so for example i could say var logger equals new logger uh did we add a dependency to logger no okay good right so and then i could say i don't know logger dot good equals good okay uh that is setter only oh dang it get her only so let's add a setter all right so now i'm setting it uh and actually to prove that we have the same instance let's uh let's create this so we can compare right now what i can do is instead of register singleton i'ma say hey container uh i want to register an instance and whenever i ask for ilogger i want you to give me the logger instance that i have created here right so i have manually created this for whatever reason i have set some state i have done some stuff let's go ahead and hit a break point here let's run our app now our guide ends in b2 right so now we're going to register this instance now the instance is resolved in the my dependency object the good ends in b2 now the person service here the logger ends in b2 so it is indeed using the instance that we created and said hey whenever i ask for i logger i want you to use this specific instance every time so it's still a singleton okay this is still an analogous to your to your uh static uh logger okay but this is a much better approach because you can still you know have all the benefits that you get with using dependency injection i see dan making jokes we can see brian doesn't actually write code these days [Laughter] no i just write too much code and different technologies i'm all over the place i'm just writing react yesterday not javascript i like typescript better though but sometimes you have to be in javascript okay so any questions so far any questions so far okay does anyone does everyone kind of understand you know what were what you're accomplishing by using di like do you see the benefit of it do you have any questions about uh frederick says it's impossible to configure the container to use the decorator pattern well i'm not sure what you mean by that i'm not because a decorator pattern is something different you can have an object that decorates other objects and register that object with your container and then your container can resolve your decorators so yeah i mean you can totally do that uh so what about constructors with parameters well these constructors do have parameters see that and so i can add another dependency to this and it will just work and i believe i believe unity will actually so what you've seen now what you see right now is i am registering the uh i dependency an interface with the implementation type but i'm gonna do a test right now because i i think it's been a long time since i've used unity let's go public class test class uh some containers will actually resolve concrete types automatically i think unity i think unity is one of those let's try it real quick yes okay so unity will automatically resolve a concrete type so i created this test class a concrete type no interface and i did not register it you notice that there's no register test class up here because the container can't well the unity container automatically resolves concrete types okay now there's some containers that don't by default like dry ioc doesn't by default but you can set it up to where it does i believe the microsoft dependency container injection container i don't think it will do that but i don't know i don't really use that one much i'm just trying to remember from my frustration days with it because i really can't stand that container uh see getting uh lost in my okay jester said i registered the resource as an instance to the view model first to add the data template but i don't know if it was a good idea i'm not sure what you're getting at there uh registered resources as an instance to the view model first to add the data template i'm not getting that one not getting that one can you try to explain that one a little more uh jester i i'm sorry i'm not i'm not understanding that question okay if you want to compare instances one lubo one says if you want to compare instances you could just call it get hash code it's used to check if this uh i used to have uh check the same event aggregators yeah so if you're using prism and you use the event aggregator the event aggregator is a singleton so you'll get the same instance uh no matter where you're at in your in your app so okay let's take this a little bit further let's take this a little bit further like what does this di stuff really really buy me like let's really look at how cool this can be okay let's i'm gonna create another project uh let's create a new project uh let's i need a net class library let's do a class library yeah we'll call it class library one that is perfectly okay with me and let's move some of this stuff around add a project reference to our class library let's move our person service our ipers insert no let's just move let's move person service oh okay i have an idea let's let's just go insane with this i'm going to go really ridiculously crazy class library 2 i'll say classroom one dot interfaces i'm going to abstract the crap out of this okay all right so we got that i want to delete that class library or that class this class so here we're going to add our interfaces so actually i should have just kept that i could have put all these interfaces in that one freaking okay where's my interfaces interface uh get rid of that logger i'm gonna put all my interfaces in one project uh i dependency okay okay now let's just add another class here and we're going to move we're going to move these class class test class that's fine person service we're going to move all these into this project so we're we're separating our uh our interface implementations into different projects okay so we're getting really like high level abstractions so i had a project reference to interfaces i should have named these better so now i can't read my own project out of interfaces all right so let's add our using statements here interfaces i know i added the interfaces there why i added the project reference right oh what's this what's this net core 31 net core three one these are class libraries net core three one this is all net core three one oh public golly thank you mahesh thank you that's what happens when you just start just writing code not paying attention okay those are all public music all right so we got all our interfaces now yeah there it is there it is thank you very much oh my hold on follow behind on the chat here i always talk about the factory method have primitive get the default value what brian let's showing how it works okay dan's just causing trouble okay so we have abstracted our classes out uh let me add our using statements here okay so what we have here is we have put i don't know why i'm getting this this uh little exclamation point there these are all net core 3.1 apps see 3.1 why does this console app not like that 3.1 class library how weird is that let me run this i want to make sure this works and it still works so visual studio is doing something funky okay now what i have done is i have abstracted out the interfaces and the class uh in the classes themselves into different projects okay so this project has all our interfaces no implementation this class library has all of our implementation no interfaces okay but check this out i i want to try to drive this point home okay if we look at our dependencies for the console app only our main application is referencing the container okay only the main application it's referencing the container these have zero dependencies like there's no dependencies on our class libraries however all of the functionality are in those class libraries right so i still need my logger i still need my my eye dependency and my test class for this thing to work okay so we have abstracted away all that information i'ma run this and it's still going to function the exact same it still works even though those other libraries our class librarian or interface library they have no clue how they're being created right they don't know how this uh how these objects how these dependencies are being injected into them okay dan thanks for joining my friend i always appreciate you coming and showing your love man i'll catch you on i'll catch you on discord right michael says it's because uh if you guys are excited you're writing code there uh but yeah so i want to point out the power of this is that these these class libraries they don't care how these dependencies are being created right they have no knowledge of the container they don't reference the container none of that however they still function normally right so i can throw this in a completely different application right i can put this in a nougat package bring it in into my app and it's gonna function just fine okay because we have abstracted away everything and there's no knowledge whatsoever on how they're being created because the container is creating all these objects for us all right benjamin do they really need to know how they're made no they don't but if you're just now joining us you know we talked about we talked about how a lot of people do code classes and they don't use di so they're in here saying oh yeah a new logger equals new logger right and so for example you know what maybe this doesn't drive the point home far enough let's uh let's add another library here let's add a new project a new class library i'm gonna call this library1.logger right and then i'm going to move this logger into this project and then i have to make sure i add my uh not that one add project reference to my interfaces right make sure i got that in there i'll have to come add my using statement that looks good uh see now because we're newing this up ourselves we can add a reference to that okay i probably should have done that myself i can't tell i'll do it myself you're too slow add project reference to my logger all right now i have my using statement okay so this is this is working now but now this person's service once again we don't even have the logger class in here right that logger class doesn't exist so i couldn't create a new instance of that logger if i wanted to it's impossible now it doesn't even reference the class right this this this uh this class library doesn't reference the class library that contains the logger so i have protected myself architecturally from making bad decisions uh because i can no longer new up this dependency inside this class it has to be passed in by something right it has to be passed in by something and that something is done here i just let my container know about my logger and it will create it for me so it's still going to function just the same see i did something i logged something it still functions the exact same but we have completely abstracted away the knowledge of any logger class or logger implementation from the consumer of that logger right it's you you can't write bad code now because you have you have taken that freaking logger out of the project and you're not referencing it so you can't do this never do this i never want to see anyone write code where you're creating dependencies inside the class that's it's dependent on don't do this but now you can't you can't do it and it's a beautiful thing right so i mean theoretically we could put all of these in different projects if we wanted to all right benjamin the only thing that needs to know is the container whatever type you use exactly that's it so this this makes this makes your code so nice and beautiful extensible testable you're like brian what do you mean extensible well because i extended this i can extend this class and it's just going to work like let's add another dependency another class right i'm going to extend this service with new functionality because something happened and whatever uh so imagine you have this huge application hundreds and hundreds of views and objects and you wanted to change just the person service with something new well i didn't have to break my app compile time or run time to do that it still works perfectly fine but i'm able to extend this class without impacting any of my other code because i'm using a container that is responsible for resolving any dependencies right and since i'm not newing up a person service anywhere i don't have to go throughout the whole freaking app looking for all the build errors so i have to add a new constructor argument or parameter right so it's freaking awesome not only that you are now 100 testable we can start writing unit tests for this because we can mock this logger we can mock this dependency we can even mock this test class but ideally that would be i test class really i mean honestly that should be an i test class but that's that's essentially the gist of it okay that's all dependency injection really is it's just a way to move the responsibility of creating your objects and creating your dependencies and move that to something that's that's its job right and that's what a dependency injection container's job is that's its only job its only job is to create objects and create objects dependencies and those dependencies and so you know earlier we had a question this is more along advanced advanced stuff but we had a question about scoped you know register scope and creating scopes and things like that uh so a scope is essentially when i call resolve here right and this resolve creates this i person service okay now if we go back to our person service then we get a logger we get a dependency we get a test class and then if this dependency also has a logger then it gets this logger okay now when you start scoping okay so for example this register singleton right if we did this register singleton here okay and if we didn't do this register instance let me comment that out just as an example i would get the same instance within my scope okay so if i set a scope and uh in unity i'm not quite sure how to do that in unity i don't know if you pass it in the overrides oh well the idea is that once you figure out the api to create a scope anything within that scope it's scoped to that process right to that to that scope block so if i put a scope around just this resolve then this singleton so let's i don't know the api for it but let's say i did a scope here right oops and then i decided to do another scope there and another scope there right let's say i decided to do this that means within the context of this scope it's just like variable scoping if you will uh when i call resolve all the dependencies for this scope are restricted or scoped to this scope [Laughter] that's a lot of scopes right so for example uh in our case if we had this register singleton and we had a logger even though it's a singleton everything resolved all the dependencies and everything resolved to this chain would get the same instance of that of that logger however on the second scope during this resolve chain this instance of the person service would get a different same instance throughout that resolve process right so for example okay we're resolving person one and scope one this is logger one this is logger one done if i go onto scope 2 and we resolve person service guess what this is going to be logger 2 this is going to be logger 2 because that that singleton logger is scoped to that specific call right does that make sense that chain so all these registrations that you that you put in here when you create a scope you're basically scoping those registrations uh to that scope chain if that makes sense i hope that makes sense that's really confusing that's even confusing for me to say team in technically when you extend you may not have to go through all the new statements and change them but you still need to go through your tests and fix them don't you well it depends if you're adding like if you're not using di if you're using the old the old bad way of newing up equals new logger right uh if you were to add a constructor parameter then you broke everywhere you're you're newing up that logger and you'll have to go add your parameter to the constructor right that's what i mean so it depends on what you're extending but most the majority of apps that i've seen do not properly use a di container and so they have to fight with that they fight with that everywhere you make a change to constructor find replace or you got to go new up these objects pass your statics around whatever you got to do to fix your compile errors and hope that it functions right man my phone is going off now make sure these aren't important just make sure my boss isn't trying to get a hold of me brian why are you working right now i need you in this meeting that would suck i hope he's not watching no my boss is cool he's cool he wants me to stream more he's like brian you need to stream more like okay all right i can do that uh so i i hope i uh i hope i didn't confuse anyone too bad on the whole scoping thing i hope that makes uh sense hackle says where would you create the container in different types of apps where normally the containers created at the root of the application right normally it's it's at the root of the app uh so for example you know if you're doing something like wpf i'm use that because uh the prism library is a framework for building uh for architecting and uh maintainable testable extensible uh xaml applications and so it's containers created at the root application so like on startup essentially do i have a quick sample maybe i could show let me let me see if i can find a really quick sample to give you an idea it's a little more advanced because it's like a real app how about i think ig outlook might be a good sample to look at and so this is a like a real uh application that has modules right so we'll look at the app xaml here oh this is an old version of prism this is very old this needs to be updated so in the case of prism what happens is you create what's called uh modules and these modules for example this male module this is where you do your registrations with your container right so this application is composed at runtime so the main executable uh is only one view this view shell like you can see there's no views in this main main application i mean there's nothing here it's just like a master page right it's just a master page resist i am a high school student from india just starting to learn csharp.net could you give some advice on how to start everything's very confusing so many so many project types very confusing resist let me get let me get back to that one let me get back to that one all right okay so this like this project here this is just a placeholder there's nothing in this main project except for some theming information some resources and images okay there's one view one view uh but all the main functionality of the application are in these modules mail contact calendar actually will this build i haven't run this in so long i'm not even oh it did holy cow oh my cool little ah no no it won't build this is a very old i need to update this actually you know what i should do i should update this on stream we should update this application to.net core net 5 we should update this to.net 5 because this is probably what is this this is as soon as visual studio wants to show which it's not it's not showing is it that old it won't even show the properties window oh my gosh it's so old the properties window won't even show that's insane okay hold on let's unload or unload unload project dot oh four okay.net four six so yeah this is a very old application reload project one dev has your boss noticed an increase in sales on air forget since started streaming i don't think so i don't think i'm helping with sales at all i don't even talk about our stuff i don't even talk about her stuff i'm talking about other stuff i should probably talk about her stuff more honestly oh my gosh yeah i better i better start talking about some informatics stuff i get the idea uh only one container per app right yeah so this this ig out this main executable is just a master page and then at runtime we have these regions that you inject content into if i could find a region pop up oh right here see this has a region name right and then if you go into uh if you go into a module you can see that you can register things and into regions right add things to regions all this great stuff so yeah it's it's complicated this is much more advanced much more advanced but this is how you would properly architect the enterprise application right but what i was trying to get at is all those modules those mail contact all that stuff those are separate projects and you register at the root of the root class which is the module class and that's where all the registration happens right oh hackle you've done some prism awesome okay let's get back to a resist question about can you give some advice on how to start uh actually you know microsoft learn has some good they're building some good content if you go to microsoft learn uh i don't want to sign in let's just look uh github.net learn c sharp ah here i'm gonna i'm gonna put this in the chat for you there they're right there resist i'm gonna put that in the chat uh but this would probably be a good good place to start uh it walks you through you know kind of teaching you the uh the basics of c sharp and things like that and so you know man that's a great question because i've been doing this so long i haven't really thought about what i did to learn i should probably start creating some type of list or some type of uh collection of learning content for people because you know i've been doing this so long it's i kind of forget and i've kind of reflected a little bit on myself and i start to think it's like when i used to reply to questions i used to say oh that's easy just do this did i start thinking like wait a minute brian don't be a prick like not everything is easy to everyone just because you've been doing this 20 years you you know things right you have some experience uh so i've been trying to take it's easy out of my vocabulary and uh because i realized like oh crap no it's it wasn't easy when i was getting started it's only easy now because i know it right and once you learn it it'll be easy for you but until you learn it it's not going to be easy so i gotta stop saying that right uh mahesh also said oh yeah mahesh great point pluralsight has some great content for net development uh actually i'm going to put in a link if i can find it yeah yeah yeah okay here cannot copy and paste anything so i have a pluralsight course uh for learning how to use prism for wpf and i have some custom control courses but if you if you click that link there it'll take you to pluralsight and you can browse you can browse the uh the catalog and learn and they have like a here i'm gonna click on that link myself they have a it's a 10-day trial 15-day trial right you could try for free i forgot how many days it is oh 10-day it's a 10-day free trial uh so yeah so if if you're looking to learn pluralsight has a ton of great content that you know goes across all kinds of different technologies so just click that link and click on uh try and you get a free 10-day trial so learn as much as you can in 10 days and then also every now and then they'll send me like a a coupon or something like that like i'll give out like a third a month free uh every now and then so if you follow me you know watch my stream subscribe to my channel and then i do giveaways every now and then for these for these codes and i also do uh also do uh announcements from when they like have a free weekend because sometimes they give you like a whole free weekend three days of just learning as much as you can and so i'll i'll announce that as well right uh luciano but the 10-day trial has hours really really it has hours limit i did not know that they don't make it sound like it you have an hour's limit it makes it sound like you have access to everything but hey it's still get get what you can out of it right get what you can of it okay apart for brian please also check i am tim corey he has a lot of great stuff.net and general stuff like solid yep i've heard i am tim corey i haven't really checked this content out but i've heard great things about it uh you know i'm actually not a huge fan of the solid principles i think they're uh i don't think they hold water honestly i that's a that's a conversation for a different time i think uh i don't think the solid principles really hold up no not at all they're actually actually pretty easy to debunk yeah what pc specs do i have oh i have i'll show you what i have i have a digital storm uh digital storm what's our website is this digital storm so i have a digital storm works no not workstation sorry desktop i have a lumos and then it's been a while can i just bring up my this is probably the easiest way so yeah so i have a amd ryzen 9 3912 core processor 3.8 gigahertz uh 64 gigs of ram and then my where's my graphics card it's like uh 28 what is that thing called what is that thing called nvidia 20 something 2080 technolo where do you build it oh select model i think they gave me options here where's the graphics card i forgot which graphics card i think it's the 1080 check the task manager okay let me check the task manager will it tell me my uh tell me that so i have my ryzen 9 where do i find my graphics card oh nvidia nvidia geforce that's it 2080 super thank you very much one day i've got to go thanks for joining good to see you my friend hope to see you next time yup so that's what i have the rtx 2080 super by digital store man i love this machine it's pretty too it's purty it looks nice well i don't have this i don't have all that no i'm too cheap for that nope not buying all that liquid cool stuff mine's just normal [Laughter] okay what's wrong with solid that's uh that's a whole stream by itself well actually has anyone ever written about that why solid sucks something like that no that solid works uh why solid isn't good or is wrong how about is wrong ah someone did write about this why every element of solid is wrong someone see someone's beat me to it oh he has a slide deck oh i guess this is oh this was a talk they must have given this is a talk they must have given let's see yeah so here's all the uh the solid principles single responsibility open and closed the list of substitution interface segregation i i actually like i like the uh the pension version stuff though but yeah yeah ghost i don't know if you search like i just did i'm sure someone's written a blog post about it as well but yeah i i agree that i think uh solid is it's not what it's cracked up to be personally crop the canyon my friend good to see you how long will this go on oh actually whenever i'm done i don't set a time i don't set a time uh normally i go at about lunch time which is 30 minutes from now but if i talk i talk right it's just the first time oh this is the first time i'm learning any language seriously so not familiar with the learning curve at all nor how to start stuff and making projects from ground up uh well resist what kind of projects do you want to make my friend uh what are you curious about what kind of projects do you want to write one dev uh can you stream someone on your channel like you do on twitch so i could add your stream to my channel for others to see do you mean like uh kinda like hosting is that what you mean is that what you mean like host someone i'm not sure i don't think you could do that on youtube actually raj and i probably joined late which di container do you prefer so i actually prefer dry ioc right now i'm using unity because the api is i think easier in my opinion uh for for beginners but dry ioc is definitely a better performing much much much faster uh and so that's what i i always use is dry ioc and you know what there is a di container performance comparison i think this is it that will go through all the different uh containers yeah i think well that's taking a long time to load your website's slo and he keeps it up to date this person keeps it up to date so dry oc is one of the fastest for net in general and it has a ton ton of features ton of features uh unity is not for games okay so this is a different type of unity okay so uh what we're talking about is a di container called the unity container uh you're thinking unity 3d for building games mobile games right so in production app how many interface uh do you expect to have in a vm uh depends on what that vm does i don't i don't really put a number on them at all uh my vms are normally pretty simple not the only thing vms really do or they they manage your state that's really all they're supposed to do right you'll have services in there for getting your your data fetching your data and then you'll have properties backing properties for data binding in your ui uh so there shouldn't be like a ton ton of of uh services required but it really depends on how complex your application is too because i've written some highly complex applications uh for financial services i mean if you've done a financial services app jeez working with quants can be challenging i'm at xamarin dev and wanted to know when and if prism was i don't know if prison will support shell because shell sucks shell sucks i would never write an apple shell i'm sorry it's a piece of crap i'm sorry it is what it is you know as of today unless they do a ton of work unless they do a ton of work shell is a piece of crap and prism will not support it until it's not a piece of crap right that's just my opinion but shell has so many limitations as it is honestly if you're if you're writing a new xamarin forms app just do a standard xamarin forms navigation don't don't use shell you're gonna you're gonna hate yourself if you show unless unless it's a very simple application if it's a very simple application then you could probably get away with a shell app because it's so basic and simple uh but yeah i will never touch a shell out i am never going to write a shell out they have a lot of work to do there i would i would switch to flutter before i created a shell app let me put it that way that's how strongly i feel against you know how crappy shell is but that's just me that's just me personally you have to make your own decisions right so i suggest you play with it if you like it that's great if you like it that is awesome it's a tool that you can utilize and be successful with more power to you me personally i think it sucks but i'm i'm opinionated so if you can't tell okay well i think i'm thinking you know as far as uh as far as dependency injection goes i think we kind of i think we got it like that's that's really all there is to it it's it's pretty easy it's pretty easy stuff what do you all think we're just finishing up the record uh do you recommend a resource to learn creating charts you and using skia sharp uh actually i don't know to learn let me see if i can find something real quick i don't know of anything there's some dots out there there's some blogs some microsoft blogs but they're not really great they're very basic right like showing you how to uh to clear the canvas and right to the can draw some shapes uh drawing some pads but they don't really show you how to like draw charts because charts drawing charts with ski a sharp is not something i personally would would undertake i would much rather much rather just pay a hundred bucks and get charged for xamarin so for example hey just go get 99 and buy these charts okay keep going scrolling right donut chart data chart category chart funnel chart uh pie chart sparkline uh 100 bucks you get all these controls linear gauge real gauge all that crap scheduling excel library so if you want to work with excel uh all that kind of stuff just yeah go spend 100 bucks man 100 that's all it is and you get all those charts for xamarin and xamarin.forms and if you're using if you're doing something like uno or uwp or win ui those are actually freely available on nougat right now uh as a preview so if you go to nuget.org and type in like infogistix and for okay i can spell inforgistics.uwp for example here's all the charts data visualization gauges data grid charts uh yeah go at it it's all on it's all on nougat right now right oh crap you can't [Laughter] you can't see my stream my bad whoopsie yeah thank you for that linus i appreciate it thanks for the heads up uh do you play an instrument i i play guitar i used to i haven't played in a long time i've not played in a very long time what about razor pages talking about uh whoops uh for razor plate pages ignite ui ignite u i dot blazer uh if you're doing blazer and you want to do razor pages that's what you want to use now you want to use ignite ui.blazer so this has the grid and charts in it charts maps editors all that great stuff actually we don't have maps yet why they put maps there do we have a map for blazer no way did i miss that we have a map for blazer never mind we do have a map for blazer that's hilarious i didn't even know that oh my gosh well now i know now i know yeah it's lunchtime okay how to integrate with google calendar using dependency injection okay so that's not real so let's talk about what you would need for that okay first off you would need a service okay you would need your let's go am i within this no i want to be outside of this i want to be outside of that you would want something like you know your class geez i cannot type uh class google calendar service right and then you would have methods in here like get items right whatever it is it would be a void to be a collection of something uh actually i'm just gonna stub this out so it'd be i interface right uh add item and you might add something i don't know what it would be but so you would basically create a service right that you would use in your application and you would call the apis for the google calendar uh to read your calendar to add or modify to all that stuff right update delete and then you would just go through your service which would then in turn call the apis for google which i assume they're they do expose some type of restful api for that right uh and so for example uh i have it might look something like this hold on let me let me show you this app to kind of give you an idea of what it would look like if you were to wrap something okay so for example i have a task service that hooks into a product we call slingshot which is like a product management app like so let me go to a slingshot so amphoterogistics has this product called slingshot oh my gosh just let me get to the slingshot right so we have this product here and we have tasks and all these teams and all this stuff right uh but i i wanted an api to interact and add tasks programmatically based on you know certain actions i take so what i do is i created an application that uses our the http client that basically calls the api right we get the data the json response i read the string i parse it and then i populate my objects and then i have objects in my view models right so here's my view model where i have a i have a selected task my collection of tasks all this stuff right so for example my view model here has a dialog service my teams task service and a task list service that's all the services it has and so all these services are responsible for uh doing all the the real work right so i have business objects that represent my ui and then i take those business objects that represent my ui and i pass the the state or the data to these services and the services actually do the work which will add these objects uh via those those restful calls via the api into the slingshot application so that's that's kind of how it works so you can see i have my my task service task list service my task service and my team service right that's my oh my head is in the way sorry let me minimize that there right so i have three services that i use that's kind of how it works how would you serialize an interface items so you can store it to a local file i'm not sure i understand exactly what you're getting to know i'm not sure which what you're getting at uh it really depends on like what you're doing i i don't know exactly what you're doing there but you could have you could have a method called serialize on your on your interface or you could do an extension method like public static class uh serialize extensions and then public static void serialize this i i'll say i logger and then you'll have your logger and you could do something with it like serialize it right like it just it really depends because then you can come to your logger where i had an instance of it see where i have logger and i can call serialize on it but the serialize is this extension method serialize right so i can now do something do something with the logger right so i could write an extension method do that if i wanted to right just depends just really depends on what you're doing lots of different ways to accomplish that uh do you use node.js with react.js to create api or netweb api so on my end my responsibilities is i don't really create the apis uh i consume them right so my job in the fragistics isn't on the back end my is on the front end where i help customers uh create apps and solve complex problems and architect their applications uh to set them up for success right that's where i lie so i lie more on the front and middle front if you will if you will like middle front ish uh i haven't been in a back end in 10 15 years so but i do think the apis that i showed you in that last that last sample i opened up i believe that was actually an asp.net uh api if i remember correctly matt what's your general flow for consuming a new a new api in c sharp any tools generate classes or anything uh no actually so that's a great question matt usually what happens is you know there's there's two different concepts here you have your ui right you have your ui and that that represents your your your application interactions right so you're going to have business objects that represent the state in your application this is one of the big reasons i hate orms i hate orms because when you use an orm to represent the business objects for your ui your database looks like an object-oriented program and that's not what a database should look like i can always tell when a developer designs a database versus when a db designs a database because a database is meant to be designed to be you know high-performing crud operations right normalized structured uh and meant for high performance read write updates right all your cruds your database should not represent the ui in your application it's just not how it works and you know one of the perfect examples i give for this is like maybe you have something called types whatever a type is but you can have different types in a database all your types there'd be a key value pair right a key and a value the description and the value that's all you need it's a type so all your types in a database will be in one table as an example but you may have 20 different types business objects uh in your ui you know what i'm saying like i'm trying it might be a dumb example maybe not a good a good example but i'm just trying to drive home the point that your ui the business objects that power the application should not match the database 100 so i don't like orms because it drives you down that path right so the first step is i need to understand the requirements of the data that i need okay then i will create my interface right i need to know what i like i need to identify the data right so maybe like if we're doing calendar service and i'm trying to understand okay what data do i need from this well you know what uh i need i innumerable i need a collection of my uh calendar items right so i have you know something called get items oops this is an interface sorry right i start stubbing out my interface of what i need uh and then i'll just start implementing those one at a time see so that's kind of how i do it i don't do it all at once so i only get what i need and hopefully there's an api that gives me the access to that and if it's not i got to go back to the team and i got to say hey we need this or this doesn't make any sense you don't need to provide this right things like that so it's really uh i guess i look at it very differently i don't like to go from database up because to me the the app the user experience is the most important right i need to make sure that my app kicks butt it makes sense it's designed well the interactions make sense and i can store the state now that i've understood how this has to work i can start building that that bridge between the state and the business objects and the underlying data how does this need to be broken out i have all these type objects these enums if you will in the ui that represent things how should i how should i parse that out and save that to the db now what what what does my service need to be to take my business object uh parse that to where it's in a proper format that the database can understand right uh hi brian uh so you prefer apis over oh yes i prefer apis over orm any day any day like uh like any framework i i never used nd framework didn't like it didn't like it at all uh what i did like was csla because i don't know if anyone's ever heard a csla uh what is it csla.net wait that's going to bring up.net i don't got to search for no no i want to search for that the only hard part is searching for it because if you put in.net it's going to go to this this website uh csu.net yeah right here cslanet.com so this is a business object framework this is a framework focused on nothing but business logic business rules uh data access right all that stuff you kind of see the the diagram he has here and so it's i prefer to use like if i'm in.net i'll prefer to hit the database directly because a lot of the apps i deal with uh their internal applications and they'll hit the sql server uh you know directly good old ado.net but if you have to go through an api well now you're hitting web services and things like that but if you care about like business logic and authentication authorization like all that kind of stuff on an object level like for example oh this person person a is a manager and it can access this property on this object if you're not a manager you can't read write this property on this object that's the control that you would get with something like csla.net uh you can actually attribute roles on properties and methods of these objects and if you don't have that role the person interacting with the application cannot read write or anything uh with that property or method which is pretty awesome for desktop applications do you feel the same about api i do not use orms period even for desktop even for desktop i do not use orms oh yes i worked on a project where i replaced csla with ef oh oh what the how much you do that you're just blowing from stupid down why would you do that oh my god you just lost so much functionality i don't know some i guess it depends right if you don't mind writing or interacting with ado.net and you just want to whizzywig drag drop link statements to generate all your stuff i mean that's fine right i'm not going to judge you but you lose a lot of functionality by using ef and i guarantee you since you're using ef your database looks just like your object model in your application i guarantee it guarantee your table structure your table dependencies everything in that database looked just like your business objects uh in your application and that's not how you should design databases in my opinion but that's just me that's just me i you got to keep in mind my my experience comes from large enterprise applications um you know for example one of the biggest applications i worked on was called uh case which is a commuter a computer assistant estimating system or management basically what it allowed you to do it was for a construction a company and we built like nuclear power plants bridges dams military bases things like that wind turbines like all this crazy stuff they would use this application to go out into the field and they would estimate to the bolt to the nut and bolt of what it would cost to build that thing right what equipment who would install it like it was a crazy system uh so those are the types of systems i would build things that you would build a nuclear power plant with uh so oh i'm falling behind on here if you don't use ef what do you think we should use i'm i so i'm not gonna tell you what to use i will never tell you what to use uh i say use what you're successful with if that's if that's ef right if that's entity framework then that's entity framework if you're successful that's awesome as long as you're providing value to your end users and selling your product beautiful uh i personally don't just because i will either handwrite my objects i'll use csl i'll use a true business layer and then i will have a a data layer that populates my business and my data layer will be responsible for making that transition between my business layer and the actual database underneath right so that that data access logic will control that information back and forth okay and sometimes i might have what's called the data transfer object which i'll have my huge like just massive business object with all these rules and business rules and business logic and then i'll have a very simple dto that's responsible for transferring state right sometimes i do that too so it really depends uh dapper i don't use dapper actually uh actually i've never never used it don't know anything about it i should look into it though one says i also don't like ef even though we had to work with one from a db first perspective yeah but the app sucks big time those who wrote it quit the job after completing oh that sucks stop looking at my database it's dangerous to talk to a database from a desktop app because connection string can be retrieved oh no you're doing it wrong you're doing it wrong if your connection string isn't is readable from your desktop app uh alex no no no no that that that problem has been solved many many many years ago you never have your connection string uh with your with your app and playing text like that never never never never so yes do i use domain driven design principles no i do not use uh ddd you see uh all that client specific they want to get rid of coachmen oh codesmith oh my gosh i totally remember cody are they still around i totally forgot about them hold on uh coach smith are they coach smith they are still around are they it looks like they're still around i can't believe it so for those of you who don't know codesmith has been around for ages and there are basically co-generation they're they're powered basically off t4 templates and t4 is really cool if you've never done t4 templates you should totally look into that i may have to do a stream on t4 because it's an old technology but it is super super useful for automating things i actually used t4 to build my own ef framework like designer for csla business objects so i can design my business objects drag and drop with properties and it would generate all the code using t4 so yeah t4 is pretty cool i can't believe these guys are still around good for them good for them that's nice yeah efst for uh basically anything uh when you're generating code like that's t4 t4 has been around a long time so if this is the first time you've heard of t4 please check this out uh generate code i'll put this in the chat for everyone this is really cool i really like uh i really like t4 i'm big on uh automating tasks i i create a lot of tools to make my job easier especially if i'm working on a big project i'll actually spend if i'm working on a big project and i've identified something that's going to be kind of repetitive throughout the life of the project i'll create a visual studio extension to automate that process right i'll spend the time to do that because it pays off huge and t4 is a big part of that so for example has anyone ever seen app map i'm way off topic on dependency injection but have you ever seen app map let's do this so one day i got really frustrated on because i have to build a lot of apps all the time but they're like quick to prove a concept to prototype something to show a customer something right uh app map is it installed don't tell me it's not installed oh here it is right so i created this project called appmap and this app map is for xamarin forms okay and it lets me choose like my it uses prism but it lets me choose my uh my target platforms and what it allows me to do is check this out is it last week oh yeah you know what i need a login page so i'm gonna call this a login and uh you know if login fails i'm gonna go to a page called you know failed and this is gonna be a navigate uh but if i succeed i'm gonna go to a master detail page that has you know these pages on it so i'm gonna navigate to this on a success right and then i don't know this might be products might be a products page right this might be i don't know what to call it i'll just say items orders i don't know whatever whatever you want to call it right oh maybe there's a button on this page that navigates to some other page right basically i got tired of having to like generate all these apps right click add new item add view model uh so i did this instead i wrote this tool that allows me to drag and drop and create these uh these pages and i generate all that code right yeah so it's like it's always like adobe xd but for visual studio so now there's all my views all my view models and if i open up one of these uh my buttons for all my navigations there like the navigation's all hooked up for me right so these are the type of tools that i'll build that make my life easier right uh so yeah it's pretty cool and then all my view models you'll get a base view model somewhere oh it's under infrastructure i want to rename that here's my base view model everything's like it's all commented and works it just works you just hit f5 and it just runs and works right sure save uh but yeah so that's a tool i wrote and it uses t4 it uses t4 to generate all that code okay can you share your future are you can you share your review on feature of wpf wpf is huge wpf is huge right now it's not going anywhere not going anywhere there's a lot of uh there's a ton of new wpf apps every day and i know because i help customers write them and the reason you don't really hear much about wpf is because the the company's writing these these are internal applications these aren't like customer facing apps right uh they're they're internal you'll never see them for example the 911 software that runs half the united states 9-1-1 emergency calls is a wpf app using infrajs controls and prism right so i like to say that prism helps save lives because it's used in 911 software and it's a wpf application so whenever you call 9-1-1 and ask for help chances are the person on the end of that line is a wpf app dispatching police and medical help right so you'll never you'll never know about these apps uh so it gives wpf a perception that it's not popular not being used but when forums and wpf are still here still apps being created it's it's crazy it's awesome uh yeah a lot of tools that generate objects whenever i hear other people talk about coding i feel like i know nothing corey i am the same as you i feel like i'm in so many different technologies i have to jump between like a react or angular javascript typescript you know i'll be in freaking c-sharp blazer like i'm in between all these different uh programs are these technologies and i get so lost i'm like oh my god i know nothing i know nothing uh yeah i remember john i remember when people said would die yeah silver light it's the wpf killer oh man where's my oh i don't have a laugh do i do i have a laugh nope i don't i don't have a laugh but yeah i remember that i remember that and then silverlight died and guess which which is still around guess which one's still around yeah john johnny h yeah i totally have the same mindset of programming random extensions extensions are awesome man if you're if you work for company and you are not writing extensions you're doing it wrong i'm telling you i'm telling you i am internal dev tools dev can confirm use wpf oh thanks for sharing matt that's awesome john's making one for a motorcycle company internal johnny h have you thought about just using typescript for everything with uh i think it's called express for desktop apps uh no i mean typescript's cool i'll use it where it belongs uh but no i don't believe in the the typescript for everything you know when i when i pick uh i know i know what you meant electron and with electron you can do uh with electron apps you could do a react app you could do an angular electron app uh you can do all kinds of technologies but it's basically javascript right typescript is javascript uh but yeah i know it depends on the project right it depends on the app on the technology you use this it's not a one to rule them all doesn't exist right you have to think about your requirements and then choose because you may not want to do electron or maybe you're doing a react native instead right if you're doing react uh so it just it really depends uh what about apps being installed in customer machines wps2 of course of course corey not only that like wpf like whoops uwp's butt like ewp has not been adopted by the masses at all and now that you could put wpf apps in the in the um windows store i mean come on there's no point in even writing uwp no no no there's no point in it uh cash what's your opinion on wpf for beginners well what do you what do you want to know why not why not yeah john gwp does suck it sucks bad alan my friend good to see you uh personal free wp needs to be a thing auto fact as well no no not auto fact no no yeah you totally trolled idle fact will never be supported by prison uh what do you think about tdd especially with across doing wpf you know what zoltan that's a great question uh i do not personally believe in tdd uh there's a there's a number of reasons for this a big one being i do not believe that tests should drive the design of your application it should not drive the design of your architecture at all no zero no uh however you should test for sure you should test one thing i like about tdd is it does force you to test which is really the only positive i can think of uh but i do not personally buy into the tdd stuff but i do i do tests after development so i will write the requirement and then write my test to verify the requirement and if anything changes then i know uh it's been broken right so yeah what about behavior driven tests i don't i don't i don't have a lot of experience with behavior-driven tests actually uh actually what i don't even know what behavior driven test star what is that is that a new is that a new thing b is an uncommon term yeah yeah okay yeah it's uncommon i well just because i haven't heard of it doesn't mean it's uncommon there's a lot of things i haven't heard of focus on the behavior of users rather than the technical functional of software i'll have to do some research on that i haven't really uh haven't really played with that at all i'm not sure what this entails but i'm more of a test during development or to you know like as i write the method or as i finish the function right after that i'll go and write the test right sultan we test after yeah as long as you get that test in it doesn't matter it doesn't matter did you compare xamarin versus react native versus fluttering performance uh no i haven't but i do know xamarin performance is i've heard i don't want to say i know i have not confirmed but i have heard that flutter kicked salmon's butt in performance from my personal experience i would agree with that uh because i write xamarin apps xamarin forms apps i don't write native xamarin but xamarin forms apps i've written and i've written flutter apps and flutter is much faster on android much much much much startup is insanely fast compared to xamarin forms react native i don't have a lot of experience with react native i was just experimenting with it two weeks ago but i don't have enough experience with it to make a comment on that actually i need i need more experience with react native before i'll throw an opinion out there good thing i didn't write the short form uh have you ever used winforms yes i have used winforms i have most definitely have just win forms in just about every one forms app you can think of as just a big monolithic monster of spaghetti code those are not fun to maintain i think there are way too many options to choose from people choose the one well first wpf it's evolving so it's worth to use it a lot of options for sure a lot of options what is your secret way to store something like connection strings and desktop apps securely isn't it more secure to go through an api uh it's not a secret not a secret there's a couple there's actually a few different ways to do this but essentially you encrypt everything everything's encrypted blazer question mark so blazers cool it's it's cool right it's a new it's new it's got some things they're working on uh but right now i would not personally write a production app with blazer too many problems performance problems performance is pretty freaking bad in blazer like really bad really bad for example if you create a blazer webassembly app just file new blazer web assembly create i don't know a person object with like three string properties on it and an ent or you know puts a couple properties on it then create a loop to create say 10 000 of them something small you will bring that app to a screeching halt just loop through ten thousand creating ten thousand people objects or person objects and you will bring that thing to a halt that's how bad the performance is very easy to prove out how sucky the performance is michael says have you talked about shell and forms what do you think about it uh yeah we actually talked about shell earlier uh shells in my opinion sucks really bad i would never use it never ever ever use it john says he's debating updating an nvc app to blazer no no don't do it don't do that if if you're going to if you really really want to use blazer go server side at least because the performance is better there because it's asp.net core right so this web assembly is really bad the the uh server side which is asp.net core is much much better but you so the issue with that is that it uses signal r so for every interaction because it's server-side uh it's gonna send information back via signal r uh it's gonna send a response back to the client and re-render whatever was being changed right and so signal r itself has some limitations with you know how many connections it can have and all that kind of stuff right so i don't know how well a blazer server app would scale personally because of the signal r limitations but i mean i don't see why it wouldn't work fine with a a small app you know and john says yeah and i'll need to update the api to signal r yeah i don't know what would you be getting out of it that's what i would ask why what would you be getting out of it you're already using asp.net mvc i mean it's still of perfectly valid technology right uh corey says i know you love prism having worked on it but are there any other frameworks that you're interested in or envious of oh well it depends on the platform for wpf or xamarin no uh there's nothing out there that i think does it better than prism uh but there's lots of frameworks for other technologies for sure wpf for the first project a cool simple app it depends on what you want to learn like are you do you want to do you want to be desktop dev do you want to be a web dev uh what kind of apps do you want to write single page applications do you want to be in python do you want like there's so much out there so much out there it's i mean if you choose wpf it's still you learn xaml you learn a lot of things that can take you into other example-based platforms c-sharp works across a lot of different platforms as well so it just depends on what you're into really should we unit test simple getters and setters like auto properties what would you test i don't what would you test i wouldn't test a set and i get would you just would you just be testing if you can set a property and get a property like no i wouldn't personally test that uh do the event aggregator use rx looks like it no it doesn't use rx nope event aggregator is just a publisher subscriber event aggregator pattern that's that's all it is and actually the code is freely available for you to go check out it's on github under source uh core events uh this is the event aggregator right there so click on that link and if you want to see how the event aggregator works uh go check it out it's pretty simple actually not much to it okay i want to design beautiful pages and control your suggestions on it where do i start well it really depends on the platform so if you're doing wpf uh you know you might want to start with let me find this link here so well crap i have a wpf course uh wpf custom controls course on pluralsight so if you're looking at getting into that i would suggest clicking that link that i just put in there and it's going to take you to pluralsight and then just do a quick search for wpf custom controls and i teach you how to write custom controls right here so you might want to start there that'd be a good place to start uh what is your opinion on a reactive ui i haven't used it yet no opinion haven't used it yet getting there all right all right all right all right maggie speaking of di what if we end up calling lots of services in a class constructor is that a design problem uh well it depends not necessarily uh but it depends on what's a lot like you were talking like 20 uh yeah you probably got something going on there probably some type of code smell there if you have like 20 services like hmm but you know if your app is complicated i don't you might need that i don't know it's it's really hard to tell but if you have like 20 something dependencies in your constructor i would say that's a code smell you want to look at do you think xamarin is gaining more market share for people and companies to create apps you know i'm not sure they're winning anyone over really i think uno is doing a better job at the moment uno platform i think uno platform is doing a better job i think that you know with microsoft basically killing xamarin forms and replacing it with maui is going to break a crap ton of people and xamarin forms i think is going to die in like a year after the maui release you won't you know so you'll have to convert to maui hopefully they'll have some tools that you just right-click and upgrade your project to maui we'll see we'll see what happens uh but i don't know i think what those kind of breaks and changes you might really alienate some people and uno has been doing it right for a long time now so it's hard to say i don't know i know that the customers i talk to it's more about angular react and web components that they're looking at they're not looking at xamarin to write their apps they're writing apps for the web and they'll share that code for their mobile apps right but all right everyone well if you haven't already make sure you like this stream because i i think i've been watching other people stream on youtube and they're like like to stream youtube algorithm i don't know what the heck that means uh and also if you haven't subscribed yet can you do me a favor and subscribe real quick and let me know you subscribed uh because i'm supposed to have a little alert go off and i haven't seen it go off i haven't seen it go off so i don't think it works that's kind of annoying uh you and brian noyes tutorials and wpff vm prizm of pluralsight excellent thank you very much i appreciate that so subscribe just now sultan thanks and i did not i didn't see it come up what the heck hold on hold on i'm gonna check this right now i'ma check this right now why didn't that work i am checking my uh my alerts seth just subscribe yeah and it's not coming up yeah it's not getting it like everyone who just subscribed it's the the last subscriber i have is from like 10 hours ago that is weird that is really weird so what's supposed to happen i'm gonna show you what's supposed to happen this is what's supposed to happen see that that's what's supposed to happen but it's not working very strange okay well i'll need to look into that maybe i'll need to get in contact with uh support or something to figure out why those alerts are not working uh because i wanna know i wanna know when you guys subscribe already a subscriber i appreciate it my friend thank you so much uh yeah i need to know why this isn't working [Music] filter super chats nope don't care about super chats [Music] yeah i don't know i don't know it's not working the arab developer fix it yourself i might have to i might write my own chat bot for youtube so i can do this myself i thought about it i actually thought about it i have some ideas i have some ideas for sure all right well everyone i want to thank you for you know spending time with me today uh i know you can spend your time doing anything else anywhere else but you chose to spend it with me uh i greatly appreciate you all i love you all and i will see you all next time have a great day up
Info
Channel: Brian Lagunas
Views: 5,720
Rating: undefined out of 5
Keywords: dependency injection, dependency injection explained, dependency injection c#, dependency inversion, dependency injection .net core, inversion of control, c# dependency injection, inject dependency, inversion of control explained, inversion of control vs dependency injection, c# dependency injection explained, c# dependency injection container, C# dependency injection tutorial
Id: ASDmrUaO5cE
Channel Id: undefined
Length: 144min 46sec (8686 seconds)
Published: Tue Mar 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.