Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac - A method injection example

Can anyone provide an all-around, method injection example using Autofac?

I have checked the documentation, but I find it hard to understand how this works and how the method is resolved.

So, OK, it is pretty straightforward how to register everything, but how can I use it? For example, I'd like to have a method which gets an HttpContext injected. So, having something like this:

builder
  .Register<MyObjectType>()
  .OnActivating(e => {
    var dep = new HttpContextWrapper(HttpContext.Current);
    e.Instance.SetTheDependency(dep);
  })
  .InstancePerRequest();

Note: This is possible with constructor injection but I'd like to understand the method injection way.

Question is how to use the resolved instance? Is it possible using the method injection to get a result back by the method which receives the dependencies? The behavior is not anywhere near the parameter injection but somehow close to property injection?

Update

@fknx essentially answers my question, by saying:

Method injection simply means that your dependency is not passed as a constructor parameter or directly assigned to a property, but that it is set using a (setter) method instead

So, if I decide to use method injection, the method essentially is behaving like a setter (kinda like the Java setter methods), so it is possible to use this dependency throughout the class entity?

What is the reason to do that and how it benefits from property injection?

like image 711
gdyrrahitis Avatar asked Aug 08 '16 09:08

gdyrrahitis


People also ask

What are the three types of dependency injection?

There are three main styles of dependency injection, according to Fowler: Constructor Injection (also known as Type 3), Setter Injection (also known as Type 2), and Interface Injection (also known as Type 1).

Why should I use Autofac?

AutoFac provides better integration for the ASP.NET MVC framework and is developed using Google code. AutoFac manages the dependencies of classes so that the application may be easy to change when it is scaled up in size and complexity.

How do you inject a property?

Property injection is a type of dependency injection where dependencies are provided through properties. Visit the Dependency Injection chapter to learn more about it. Let's understand how we can perform property injection using Unity container. Consider the following example classes.


1 Answers

Here you can find a small example to get you started:

using System;
using Autofac;

public class Program
{
    public static void Main()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<MyService>()
            .OnActivating(e => e.Instance.SetMyDependency(new MyDependency()));

        var container = builder.Build();
        container.Resolve<MyService>();
    }
}

public class MyService
{
    private MyDependency _myDependency;

    public void SetMyDependency(MyDependency myDependency)
    {
        _myDependency = myDependency;
        Console.WriteLine("SetMyDependency called");
    }
}

public class MyDependency
{
}

As stated in the documentation you can also call the method in the lambda expression which creates your service instance, if you are using Register instead of RegisterType.

but I find it hard to understand how this works and how the method is resolved.

This sounds a bit odd. Method injection simply means that your dependency is not passed as a constructor parameter or directly assigned to a property, but that it is set using a (setter) method instead.

I've created a .NET Fiddle so that you can check out the example.

Update

So, if I decide to use method injection, the method essentially is behaving like a setter (kinda like the Java setter methods)

Yes this is true.

so it is possible to use this dependency throughout the class entity?

This is also true, but this also holds for constructor injection if you store your dependency in a (readonly) field or property, and it is always the case for property injection.

What is the reason to do that and how it benefits from property injection?

To be honest I've never used method injection in C#. I would guess that it is more common in Java if you want to set a field through the corresponding setter method. In C# you can do the same with property injection.

Maybe I would use method injection if setting the dependency would involve rather complex code that I don't want to put into the property's setter or into the constructor.

However, in most cases I would advice you to use constructor injection whenever possible and property injection when it is not for whatever reason (e.g. circular dependencies).

like image 83
fknx Avatar answered Oct 27 '22 01:10

fknx