banner



How To Use Automapper In Service Class With Castle Windsor

Before blowing anyone's mind with the capabilities of Autofac, let's commencement get the nuts right. Why Autofac? What is Autofac? What exercise y'all need to know before you jump into your lawmaking and add the Nuget.

The Hollywood principle

The basic idea right before we goto Autofac is that Don't telephone call us, we'll phone call you. In c# Terms Don't call around for your dependencies, we'll give them to y'all whenever nosotros need yous.

Let's get our basics right first before we do annihilation.

What is dependency Injection

  1. Any nontrivial application is made up of two or more classes that collaborate with each other to perform some business organisation logic.
  2. Each object is responsible for obtaining its own references to the objects it collaborates with (its dependencies).

There are a lot of ways in which dependency Injection can be washed. Here are some of them.

  • Constructor Injection
  • Setter Injection
  • Interface-based injection
  • Service Locator Injection

Just in this weblog, we will wait at the Constructor Injection pattern.

Containers / IOC

We need a container to manage all the objects throughout the lifetime of the request. It manages object cosmos and information technology's life-time, and also injects dependencies to the course. At that place are a lot of container libraries available. Here'south a list of some of the almost pop libraries sorted in no club

  • Castle Windsor
  • StructureMap
  • Spring.Cyberspace
  • Autofac
  • Unity
  • Puzzle.Nfactory
  • Ninject
  • S2Container.NET
  • PicoContainer.Internet
  • LinFu

In this blog, nosotros are going with Autofac and we volition also go through some of the common things that nosotros need. In any awarding, there is a

  • Object to DB Mapping lawmaking
  • Logging code using any Logger
  • Number of Services and Repositories

In our previous blog @Suhas covered on how to simplify the Object->DB mapping code using a library called AutoMapper. Now let'south inject it and have more fun with it 🙂

Recollect AutoMapper Profiles from @suhas blog? Yep, nosotros tin create whatsoever number of profiles. But look, what if nosotros have 100 profiles on an enterprise awarding?

var mapperConfiguration = new MapperConfiguration(config =>         {             config.AddProfile(new ProfileA());             config.AddProfile(new ProfileB());         });          

At present a question arises in your mind, O__o what ??? Do I demand to register 100 profiles similar this?

NOPE, WE ARE NOT DOING It. SAY NO TO DUPLICATING Lawmaking 🙂

Configuring Autofac

  • Install the Nuget and the easiest way to get started with Autofac is to follow their docs and get it up and running based on your setup.

Here's where Automapper comes into identify, nosotros create an AutomapperModule class which inherits Autofac.Module and give information technology whatever form which inherits AutoMapper.Profile

Yah! How simple was that? sixteen Lines of lawmaking, y'all at present have given AutoMapper whatever it needs for Mapping to exist done and besides we have told Autofac to resolve Automapper. At present it can exist accessed in the constructor of whatever class. Instance beneath

Resolving a Logger

Every application has some logging to discover out what'southward going on when the awarding is being executed. Y'all can have any blazon of Logger in your application.

In any grade/ Controller wherever you need your logger, what people exercise is either put

Logger = LogProvider.GetCurrentClassLogger(); or var logger = new Logger();          

This is the common mode of doing this. Just when I searched for the second line I found more than than 1000 instances of Logger objects beingness created in more than than 100 classes, controllers, repositories, etc.

Now allow'south remove all the duplicated lawmaking and circumduct Logger only one time using Autofac and get in available anywhere.

            builder.RegisterInstance(LogProvider.GetCurrentClassLogger()).As<ILog>();          

That's it. Only one line. Now you tin inject logger similar to Mapper.

References

Autofac documentation – https://autofaccn.readthedocs.io

Automapper documentation – https://automapper.org

Determination

SAY NO TO CODE DUPLICATION, SAY HELLO TO DEPENDENCY INJECTION 🙂

Similarly, you tin can resolve whatever Service/Repository and it can be injected into the constructor of any grade. There are a lot of best practices which can be followed. I will try to cover it in the upcoming blogs.

Source: https://www.techmeet360.com/blog/introduction-to-dependency-injection-with-autofac/

Posted by: chenaultanneized.blogspot.com

0 Response to "How To Use Automapper In Service Class With Castle Windsor"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel