Unity Container Resolve
Asked Answered
E

4

12

I just starting with Unity Container and my registration looks like this:

static void UnityRegister()
{
      _container = new UnityContainer();
      _container.RegisterType<IBook, Book>();
      _container.RegisterType<IBookRepository, BookRepository>("Book");
      _container.RegisterType<IBookService, BookService>();
      _container.RegisterType<IBookRepository, DatabaseRepository>("Database");
}

Now when I try to resolve doing this:

var service = _container.Resolve<IBookService>("Database");

I get error below:

Resolution of the dependency failed, type = "UnityConsoleEx.IBookService", name = "Database". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, UnityConsoleEx.IBookService, is an interface and cannot be constructed. Are you missing a type mapping?

At the time of the exception, the container was:

Resolving UnityConsoleEx.IBookService,Database

Can anyone point what I am doing wrong?

Entomb answered 8/9, 2015 at 4:4 Comment(0)
L
14

The main issue is that you are not using a named instance for your BookService.

_container.RegisterType<IBookService, BookService>();

But you are trying to resolve with a named instance.

var service = _container.Resolve<IBookService>("Database");

You need to resolve without a name to get that instance.

var service = _container.Resolve<IBookService>();

But it is unclear from your example why you are using named instances in the first place. If you post the constructors of your services, it will be more clear how to make your configuration work.

Linearity answered 8/9, 2015 at 4:26 Comment(0)
E
3

I figured it out, I needed create named instance for the service and inject constructor, as such:

static void UnityRegister()
{
    _container = new UnityContainer();
    _container.RegisterType<IBook, Book>();
    _container.RegisterType<IBookRepository, BookRepository>();            
    _container.RegisterType<IBookRepository, DatabaseRepository>();

    _container.RegisterType<IBookService, BookService>("BookService", new InjectionConstructor(typeof(BookRepository)));
    _container.RegisterType<IBookService, BookService>("DatabaseService", new InjectionConstructor(typeof(DatabaseRepository)));
}

And resolve it as below:

var service = _container.Resolve<IBookService>("DatabaseService");
Entomb answered 8/9, 2015 at 4:37 Comment(0)
U
0

I think, you try to resolve BookService with should contains DatabaseRepository as a parameter. You can't do it in your way.

You can do it like this:

var service = _container.Resolve<IBookService>(new ParameterOverride("repository", _container.Resolve<IBookRepository>("Database")));

Maybe, better way is register repository once, conditionaly:

  _container = new UnityContainer();
  _container.RegisterType<IBook, Book>();
  if (useDatabase)
  {
      _container.RegisterType<IBookRepository, DatabaseRepository>();
  }
  else
  {
      _container.RegisterType<IBookRepository, BookRepository>();
  }
  _container.RegisterType<IBookService, BookService>();

And now, resolve service. Also, you can configure you container like this:

_container.RegisterType<IBookService, BookService>(
    new InjectionConstructor(                        // Explicitly specify a constructor
        new ResolvedParameter<IBookRepository>("Database") // Resolve parameter of type IBookRepository using name "Database"
    )
);

This will tell the container to resolve IBookService using constructor with single IBookRepository parameter, and resolve IBookRepository with name of Database.

Uptotheminute answered 8/9, 2015 at 4:9 Comment(0)
P
0
using Unity;

public interface IService
{
    void DoSomething();
}

public class ServiceA : IService
{
    public void DoSomething()
    {
        Console.WriteLine("ServiceA is doing something");
    }
}

public class ServiceB : IService
{
    public void DoSomething()
    {
        Console.WriteLine("ServiceB is doing something");
    }
}

public enum ServiceName
{
    ServiceA,
    ServiceB
}

public class DependencyResolver
{
    private static IUnityContainer _container;

    public static void Initialize(IUnityContainer container)
    {
        _container = container;
    }

    public static T Resolve<T>(ServiceName serviceName)
    {
        var serviceNameString = serviceName.ToString();
        return _container.Resolve<T>(serviceNameString);
    }
}

public static class Resolver
{
    public static IService Resolve(ServiceName serviceName)
    {
        return DependencyResolver.Resolve<IService>(serviceName);
    }
}

internal class Program
{
    private static void Main()
    {
        IUnityContainer container = new UnityContainer();

        // Register dependencies with names
        container.RegisterType<IService, ServiceA>(ServiceName.ServiceA.ToString());
        container.RegisterType<IService, ServiceB>(ServiceName.ServiceB.ToString());

        DependencyResolver.Initialize(container);

        // Example calls to ResolveService method with enums
        var serviceA = Resolver.Resolve(ServiceName.ServiceA);
        var serviceB = Resolver.Resolve(ServiceName.ServiceB);

        serviceA.DoSomething();
        serviceB.DoSomething();

        container.Dispose();
    }
}
Pruchno answered 13/6, 2023 at 10:9 Comment(1)
You should add explanation.Avantgarde

© 2022 - 2024 — McMap. All rights reserved.