Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which DI container will satisfy this

This is what I want from DI container:

public class Class
{
   public Class(IDependency dependency, string data)  { }
}

var obj = di.Resolve<Class>(() => new Class(null, "test"));

Points of interest:

  1. Can resolve both dependency and data in constructor.
  2. Can use type-safe syntax to pass constructor parameters (exact syntax may vary). Yes I can do it myself by getting constructor arguments from (Expression.Body as NewExpression) - but I'll need a way to detect what arguments are registered in the container.

Another major requirements is that I'd like my components to be automatically picked up, i.e. I don't want to register Class - I want IoC to pick it up because it knows how to resolve IDependency.

Also, Property Injection can be useful sometimes, but this is optional.

The question is really about the combination of features - to have all of them - type-safe, parameters, automatic pick-up... It's easy to check one feature, but a combination of them is not easy to verify unless one's familiar with particular container and knows its features. Thus the question.

like image 329
queen3 Avatar asked Nov 06 '09 10:11

queen3


People also ask

What is meant by DI container?

A DI Container is a framework to create dependencies and inject them automatically when required. It automatically creates objects based on the request and injects them when required. DI Container helps us to manage dependencies within the application in a simple and easy way.

How do DI containers work?

At its core a DI Container creates objects based on mappings between interfaces and concrete types. This will allow you to request an abstract type from the container: IFoo f = container.

Why do we use IoC container?

The most valuable benefit of using an IoC container is that you can have a configuration switch in one place which lets you change between, say, test mode and production mode.

What is DI container in C#?

IoC Container (a.k.a. DI Container) is a framework for implementing automatic dependency injection. It manages object creation and it's life-time, and also injects dependencies to the class.


1 Answers

I think you would be better off by defining an Abstract Factory that can create your class.

public interface IFactory
{
    MyClass Create(string data);
}

You could then create an implementation of IFactory like this:

public class MyFactory : IFactory
{
    private IDependency dependency;

    public MyFactory(IDependency dependency)
    {
        if (dependency == null)
        {
            throw new ArgumentNullException("dependency");
        }

        this.dependency = dependency;
    }

    #region IFactory Members

    public MyClass Create(string data)
    {
        return new MyClass(this.dependency, data);
    }

    #endregion
}

In your container, you would register both MyFactory and the implementation of IDependency.

You can now use the container to resolve the Factory, and the Factory to get the class:

var mc = container.Resolve<IFactory>().Create(data);

This approach is completely type-safe and nicely decouples the dependencies from run-time application data.

like image 119
Mark Seemann Avatar answered Nov 02 '22 14:11

Mark Seemann