2016-08-04 55 views
-1

注入仓库请代码示例,如何动态地在控制器通过统一DIASP.Net的WebAPI:如何通过统一DI

我现在做这样的事情的方式不统一DI注入仓库讨论。

public class CustomerController : ApiController 
{ 
    static readonly ICustomerRepository repository = new CustomerRepository(); 

    public IEnumerable<Customer> GetAllCustomers() 
    { 
     return repository.GetAll(); 
    } 

    public Customer GetCustomer(string customerID) 
    { 
     Customer customer = repository.Get(customerID); 
     if (customer == null) 
     { 
      throw new HttpResponseException(HttpStatusCode.NotFound); 
     } 
     return customer; 
    } 

    public IEnumerable<Customer> GetCustomersByCountry(string country) 
    { 
     return repository.GetAll().Where(
      c => string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase)); 
    } 

    public HttpResponseMessage PostCustomer(Customer customer) 
    { 
     customer = repository.Add(customer); 
     var response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer); 

     string uri = Url.Link("DefaultApi", new { customerID = customer.CustomerID }); 
     response.Headers.Location = new Uri(uri); 
     return response; 
    } 

    public void PutProduct(string customerID, Customer customer) 
    { 
     customer.CustomerID = customerID; 
     if (!repository.Update(customer)) 
     { 
      throw new HttpResponseException(HttpStatusCode.NotFound); 
     } 
    } 

    public void DeleteProduct(string customerID) 
    { 
     Customer customer = repository.Get(customerID); 
     if (customer == null) 
     { 
      throw new HttpResponseException(HttpStatusCode.NotFound); 
     } 
     repository.Remove(customerID); 
    } 
} 

静态只读ICustomerRepository库=新 CustomerRepository();

这里我的存储库是硬编码。我怎么能通过团结注入它。

请告诉我方式。感谢

+0

HTTPS://forums.a sp.net/t/2105895.aspx?Repository+inject+into+controller+by+Unity+DI –

+0

https://www.codeproject.com/Articles/1114179/Dependency-Injection-using-Uni​​ty –

+0

https:// jasenhk.wordpress.com/2013/06/11/unit-of-work-and-repository-pattern-with-unity-dependency-injection/ –

回答

0

Dependency Injection in ASP.NET Web API 2

构造方法注入

public class CustomerController : ApiController { 
    readonly ICustomerRepository repository; 

    public CustomerController(ICustomerRepository repository) { 
     this.repository = repository; 
    } 

    //...other code removed for brevity 
} 

配置统一的Web API

public class UnityConfiguration() { 
    public IUnityContainer Config() { 
     IUnityContainer container = new UnityContainer(); 
     container.RegisterType<ICustomerRepository, CustomerRepository>(); 

     // return the container so it can be used for the dependencyresolver. 
     return container;   
    } 
} 

public static class WebApiConfig { 
    public static void Register(HttpConfiguration config) { 

     // Register Unity with Web API. 
     var container = UnityConfiguration.Config(); 
     config.DependencyResolver = new UnityResolver(container); 

     // Your routes... 

    } 
} 

您还需要一个DependencyResolver

public class UnityResolver : IDependencyResolver { 
    protected IUnityContainer container; 

    public UnityResolver(IUnityContainer container) { 
     if (container == null) { 
      throw new ArgumentNullException("container"); 
     } 
     this.container = container; 
    } 

    public object GetService(Type serviceType) { 
     try { 
      return container.Resolve(serviceType); 
     } catch (ResolutionFailedException) { 
      return null; 
     } 
    } 

    public IEnumerable<object> GetServices(Type serviceType) { 
     try { 
      return container.ResolveAll(serviceType); 
     } catch (ResolutionFailedException) { 
      return new List<object>(); 
     } 
    } 

    public IDependencyScope BeginScope() { 
     var child = container.CreateChildContainer(); 
     return new UnityResolver(child); 
    } 

    public void Dispose() { 
     container.Dispose(); 
    } 
} 
+0

为什么需要DependencyResolver? –

+0

这是框架架构可扩展性的一部分。检查链接包括在答案 – Nkosi

+0

我已经给你的投票你的答案。 –