Multiple Interfaces, Single Implementation

Oct 18, 2011 at 9:46 AM

Hello,

It is possible to register two interfaces to the same singleton implementation?

This is what I'm trying to do with Unity Auto Registration:

public interfance ICustomerService : IService { }

public class CustomerService : ICustomerService{}

container.RegisterType<ICustomerService, CustomerService>( new ContainerControlledLifetimeManager() );

container.RegisterInstance<IService>( "CustomerService", aContainer.Resolve<ICustomerService>() );

container.RegisterType<IServiceContainer, ServiceContainer> ( new ContainerControlledLifetimeManager(), new InjectionConstructor( aContainer.ResolveAll<IService>() ) );

Thanks,

Oct 18, 2011 at 11:08 AM
Edited Oct 18, 2011 at 11:08 AM

Should be something like:


.Include(If.Implements<ICustomerService>, Then.Register().UsingSingletonMode())
.Include(t => t is IService, Then.Register().UsingSingletonMode().WithTypeName())
.Include(If.Is<ServiceContainer>, (t, c) => c.RegisterType<IServiceContainer, ServiceContainer>(
                                                   new ContainerControlledLifetimeManager(), 
                                                   new InjectionConstructor(c.ResolveAll<IService>())))
Oct 18, 2011 at 12:07 PM

Thanks for that,

Had to move WithTypeName() to the first line otherwise ResolveAll<IServiceContainer>() would return an empty collection.

.Include(If.Implements<ICustomerService>, Then.Register().WithTypeName().UsingSingletonMode())
.Include(t => t is IService, Then.Register().UsingSingletonMode())
.Include(If.Is<ServiceContainer>, (t, c) => c.RegisterType<IServiceContainer, ServiceContainer>(
                                                   new ContainerControlledLifetimeManager(), 
                                                   new InjectionConstructor(c.ResolveAll<IService>())))

 

Oct 18, 2011 at 4:45 PM

How can I achieve the following rules?

[1] Only implement 1 instance of CustomerService

[2] Register both ICustomerService and IEventService to the singleton instance of CustomerService

[3] Do not specify ICustomerService in the container, only IEventService can be specified.

The idea is that new IEventServices should be automatically registered in the container, but DI should work with constructors that take ICustomerService etc.

This is what I have, but it's creating two instances of CustomerService, what have I done wrong/am I missing? Thanks,

public interface ICustomerService : IEventService
{
}

public class CustomerService : ICustomerService
{
    public CustomerService()
    {
    }
}

public class Program
{
    static void Main( string[] args )
    {
        var container = new UnityContainer();
        
        container.ConfigureAutoRegistration()
                 .ExcludeSystemAssemblies()
                 .Include( t => t.Implements<IEventService>() && !t.IsInterface, Then.Register().UsingSingletonMode().WithTypeName() )
                 .Include( t => t.Implements<IEventService>(), Then.Register().UsingSingletonMode() )
                 .ApplyAutoRegistration();
                 
        var customerService = container.Resolve<ICustomerService>();
        
        Console.WriteLine( customerService.ToString() );
        
        var services = aContainer.ResolveAll<IEventService>();
        
        foreach( var service in services )
        {
            Console.WriteLine( service.ToString() );
        }                
    }
}

Oct 18, 2011 at 11:54 PM
Edited Oct 18, 2011 at 11:54 PM

You can try something like

.Include(If.Implements<IEventService> && !t.IsInterface, (t, c) =>
{
    var concreteInterface = t.GetInterfaces().FirstOrDefault();    // concreteInterface should be your ICustomerService 
    if (concreteInterface != null)     {         c.RegisterType(concreteInterface, t, new ContainerControlledLifetimeManager());         c.RegisterInstance(t.Name, (IEventService)c.Resolve(concreteInterface));     } })