Sunday, June 28, 2015

Dependency Injection with Autofac : Modules feature

I’m having fun with Autofac as IOC containers and this is third post in series. In this blog post we are going to learn about Autofac modules. If you have not gone through first two blog posts then I would encourage you to go through it. Followings are link for that.

Dependency Injection with Autofac- Getting Started
Dependency Injection with Autofac – Constructor Injection

What is Autofac Modules?

Autofac modules provides greater flexibility  for structuring large application. Register all the component in single class will work for small applications. But for larger applications that will be quite difficult class. In this situation Autofac modules can help.  Autofac modules allow us to break registrations of component in different classes. Then later all you can load all the modules at single places.

So what we are waiting for, Let’s write some code to demonstrate the power of Autofac Modules. For that I’ve created a simple console application called “AutofacModules” and added Autofac nuget package to get reference of application. In this application we are going to create two separate module for “Student” and “Teachers”. So lets start by creating student model class like following.


Following is a student repository interface which contains a "Print" Method to print Student.
 And here is the implementation for Student Repository Inteface.

Now it's time to create Teacher model class like following.

Following is a interface created for Teacher repository which contains "Print" Method to print Teacher.
And here is the implementation of Teacher repository interface.
Now we are done with creating basic classes and interfaces for Student and Teacher It's time to create a modules for them. To created a module for them we need to create a separate class for Teacher and Student Module which got inherited from the Autofac module class. Following is a code of Student module.
And same for teacher module.
 If you see code of both classes carefully then you will realize that it is inherited from the Module classes and It's got a Load method override where we have done registration of repositories. Now our modules are also ready so It's time to write our main console application code. Following is a code for that.
If you see above code carefully the important thing is "BuildContainer" method where I've registered our module with RegisterModule method.Rest I have created teacher and student object and created object with Autofac Container and print respective objects. When you run application following is a output as expected.

autofac-module-console-application-output

That’s it. Hope you like it. Stay tuned for more post about Autofac features.
You can find whole source code of this Autofac series at Github on following location -https://github.com/dotnetjalps/AutofacSamples
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews