Introduction to Dependency Injection in C# using Autofac

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi I'm Andrea and welcome to productive c-sharp in this video I want to tell you how you can get started using Auto fact I'm going to do that by taking an application and introducing the dependency injection framework using Auto fact so this application is made of a few classes and some interfaces the application class takes the option service and the printer and the option service take a numeric store and the printer numeric store will take a printer so you can see that there is a graph of objects that are initialized in the main here before running the application the details of this application are not strictly important for the purposes of this exercise so I'm not gonna I'm not going into the details okay so as you can see however this application is already using the constructor injection pattern in order to wider dependencies so as you can see the application for example class is taking all the dependency inmates in from the constructor and this is a very good pattern to follow and the same is for the optional service for example is taking the aromatic store and the I printer as a dependency in the constructor this allows you to keep you know the system loosely coupled element an ability and you know make the system step testable so however you if you have a big graphs of objects creating objects manually might be problematic so that's where dependency injection framework comes into place because it can helps you to do the initialization for you automatically okay so first things you need to do is to actually other dependencies to auto facts using new gap I already did that this is auto facts there from 48.1 ok so let me remove this code and try to use all the facts so the first things you need to do is to create a container and I'm gonna create a method build container and this method here we return and I container this is an object that auto fax provide in order to create a container I need to create a builder and you contain a builder the container builder is where you do the configuration so here I do the configure I'm not doing that now and I'm gonna build the container so the container is them is there an important opportunity now to fact that allows you to configure how you want to resolve the dependencies inside in the application as soon as we bed I have a container you can create a scope using the method begin lifetime scope and inside yeah I can resolve dependencies using the resolve method I'm gonna resolve the application type and then I get an application and then I can run it okay so this is really the Bewick and the most simple things I can do to get out of art wired in as you can see I'm not doing any new at all in the application so all the instantiation of the objects is done automatically by by the frame or bad so what happen is I'm going to run this application so that is going to fail as you can see is saying that the application has not been registered so by default when you create a container out of fact doesn't know doesn't know how to create your object so it requires explicit registration in order to be able to know which kind of up just to resolve so we're going to need to tell out of arts how to build our object so tell okay if you if you get a type application then create an application that's how you can you can say like if you need to create an instance of application use application if you said you want to use register type using an interface you need to do something like that I say the option service will be affected using the I option service right so now all the time the frame on its resolved an instance of an I option service will create an instance of option service I can follow the same pattern for all the others objects I need like for the printer that will implement a printer and then for the in-memory numeric store that implement I numeric store now if I run it let's see if I've got all the information I need yes and it's actually printing that enable the option and doing something ok of course this application is small enough and you don't really understand why I did all of this work like it was probably easier to understand before and I definitely do not recommend to use a DI framework unless you really have a complete complex dependencies of objects that you need to resolve but you get the idea ok so what up and is that when scope resolve is called the framework automatically that application takes two options in the constructor and basically recursively create an instance of all of those objects by using the configuration inside the container that's basically how it works however you can simplify this by creating a sort of implicit registration so it's quite common in the net that interfaces are I named and then the class will basically be the same without the I so we can actually use an implicit registration so that the frame are automatically does it for you without everytime you are a class to also other register type method in in the in the container so I can do something like this I can do register assembly types then you get the current assembly oops get current assembly executing assembly yeah then you do as self so this matter say every class if you actually look for a particular class not an interface or another class just resolve with itself also if you find an interface just automatically use the default donor convention so you just do one thing online and then the program should still work as before but now you can actually add a new type like and the outer frame automatically works without you having to manually register lies for example let me create a new class I can just copy printer and I'm gonna create another another printer oh let's say anymore service whatever it is right this is gonna be an email service that's not gonna be implementing anything for example it doesn't particularly matter what what I'm going to do let's say I'm going to send an email something like that ok if I then add the email service to the option service let's say I inject an email service here will this still work if I run it is this still works so the framework knows that you need when it creates an option service need to create an email service automatically so you don't need to do anything and just works and this is really is the basic of Auto fact of course in this case we have two implementation of an interface like we have an in-memory pneumatic store that implements a pneumatic store and we have a fine pneumatic store it implements a pneumatic store so in this case we don't really know which one has been used right between the two so let's see to put a red point and see what which which one is actually being created by the framework so in those situation we probably want to override the configuration to say is that which one you want so you don't want to rely on how the framework does that so you can always awesomely register the type you want explicitly okay so basically now you converted your application and if the application grows you can add the interfaces and types and you can wire them in in your graph of objects without having to manually instance see them so why do you want to use a framework because it helps to wire up complex objects relationships if you have a big application and allows you to control even when the objects are created and how many instances are created by using the scopes and the configuration of course we haven't really we're gonna look at this information in in other videos and ultimately helps you to write less boilerplate code and in big application the the part of the code actually does is the invasion of the objects and the configuration can be quite annoying so I work in big application where there was no di framework used and there was quite a lot of Association going on so the framework can helps to remove some on some of the boilerplate code so this is really a very quick introduction to auto fact and I hope you enjoyed this video and thank you very much for watching if you like this video I invite you to check out my productive c-sharp my productive c-sharp is the essential resource for any developer at any stage of their career to increase their practical skills and become master c-sharp developer so whether you are still figuring out how to get your first on a developer job or whether you are already an experienced to c-sharp developer and you are just looking for ways to stay up to date then my product is you sharp can help you to get to the next level with an extensive collection of extremely practical videos weekly news three licenses from sponsors exclusive access to me and a supportive community to help you along the way with feedback encouragement and advice my productive Sharpe is the perfect place for anyone looking to become a master and successful c-sharp developer so check it out our product souchong comm slash membership [Music]
Info
Channel: Andrea Angella
Views: 16,146
Rating: undefined out of 5
Keywords: C#, .NET, Visual Studio, Productive C#, Learn C#, dependency injection c#, dependency injection, dependency injection c# autofac, dependency injection c# for beginners
Id: 9Pha-pKUqWA
Channel Id: undefined
Length: 11min 3sec (663 seconds)
Published: Fri Sep 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.