BrunoMartinsPro
BrunoMartinsPro

Reputation: 1846

Microsoft Unity Dependency Injection with WebAPI

I have the following architecture:

In WebApiConfig.cs(App_Start) i register the unity container the following way:

// Unity Container Resolver
var container = new UnityContainer();

//Registers the repository interface in Resolver(IoC Register Layer)
var UResolver = new UnityRegisterContainer();
UResolver.RegisterContainer(ref container);

//Configures WebAPI DependecyResolver to use UnityResolver
config.DependencyResolver = new UnityResolver(container);

My Resolver(IoC Register Layer):

public class UnityRegisterContainer
{
   public void RegisterContainer(ref UnityContainer container)
   {
        container.RegisterType<IUnitOfWork>(new HierarchicalLifetimeManager());
        container.RegisterType<IService>(new HierarchicalLifetimeManager());
   }
}

Controller:

public static KeyService KeyLibrary{ get; set; }

// GET api/values
[Route("Keys")]
public IEnumerable<KeyDTO> Get()
{
    var Keys = KeyLibrary.GetAllKeys();

    return Keys;
}

KeyService:

public class KeyService: IService
{
    IUnitOfWork UOW { get; set; }

    /// <summary>
    /// Get all Keys
    /// </summary>
    /// <returns></returns>
    public IEnumerable<KeyDTO> GetAllKeys()
    {
        return Mapper.Map<IEnumerable<Key>, IEnumerable<KeyDTO>>(UOW.Keys.GetAllKeys());
    }
}

IService

public interface IService
{
}

IUnitOfWork

public interface IUnitOfWork : IDisposable
{
    IKeyRepository Keys { get; }
    int Complete();
}

How can i inject the class libraries and repositories with unity?

Upvotes: 2

Views: 3177

Answers (2)

Nkosi
Nkosi

Reputation: 246998

You can use constructor injection and let the DependencyResolver do it job and pass the necessary dependencies to the classes.

public class KeyController : ApiController {
    IKeyService keyService;    
    public KeyController(IKeyService keyService) {
        this.keyService = keyService
    }

    // GET api/values
    [Route("Keys")]
    public IEnumerable<KeyDTO> Get() {
        var Keys = keyService.GetAllKeys();        
        return Keys;
    }
}

public interface IKeyService : IService {
    IEnumerable<KeyDTO> GetAllKeys();
}

public class KeyService: IKeyService {
    IUnitOfWork UOW;

    public KeyService(IUnitOfWork uow) {
        this.UOW = uow
    }

    /// <summary>
    /// Get all Keys
    /// </summary>
    /// <returns></returns>
    public IEnumerable<KeyDTO> GetAllKeys() {
        return Mapper.Map<IEnumerable<Key>, IEnumerable<KeyDTO>>(UOW.Keys.GetAllKeys());
    }
}

public class UnitOfWork: IUnitOfWork {
    public UnitOfWork(IKeyRepository repository) {
        Keys = repository;
    }
    IKeyRepository Keys { get;private set }
    public int Complete(){...}
}

Upvotes: 2

Andr&#233;s Robinet
Andr&#233;s Robinet

Reputation: 1537

Though constructor injection is preferred (and property injection is sometimes not recommended), you can also use the [Dependency] attribute in implementation classes that have dependencies, like this:

public class KeyService: IService
{
    // Public setter, private getter, so you can mock and manually assing in Unit Tests
    [Dependency]
    public IUnitOfWork UOW { private get; set; }

    public IEnumerable<KeyDTO> GetAllKeys()
    {
        return Mapper.Map<IEnumerable<Key>, IEnumerable<KeyDTO>>(UOW.Keys.GetAllKeys());
    }
}

See Annotating Objects for Property Injection

Upvotes: 2

Related Questions