Search code examples
c#asp.net-web-apidependency-injectionunity-container

Microsoft Unity Dependency Injection with WebAPI


I have the following architecture:

  • Data
    Database Layer
  • WebAPI
    Presentation Layer
  • Resolver
    IoC Register Layer
  • Services
    Business Layer

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?


Solution

  • 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(){...}
    }