C# ASP.NETWebAPI:如何通过UnityDI注入存储库
请与代码示例讨论如何通过Unity DI在控制器中动态注入存储库 现在我这样做,没有团结C# ASP.NETWebAPI:如何通过UnityDI注入存储库,c#,asp.net-web-api,dependency-injection,unitydi,C#,Asp.net Web Api,Dependency Injection,Unitydi,请与代码示例讨论如何通过Unity DI在控制器中动态注入存储库 现在我这样做,没有团结 public class CustomerController : ApiController { static readonly ICustomerRepository repository = new CustomerRepository(); public IEnumerable<Customer> GetAllCustomers() { retu
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);
}
}
公共类CustomerController:ApicController
{
static readonly icustomerepository repository=新建CustomerRepository();
公共IEnumerable GetAllCustomers()
{
返回repository.GetAll();
}
公共客户GetCustomer(字符串customerID)
{
Customer=repository.Get(customerID);
如果(客户==null)
{
抛出新的HttpResponseException(HttpStatusCode.NotFound);
}
退货客户;
}
公共IEnumerable GetCustomerByCountry(字符串国家)
{
返回repository.GetAll()。其中(
c=>string.Equals(c.Country,Country,StringComparison.OrdinalIgnoreCase));
}
公共HttpResponseMessage后客户(客户)
{
customer=repository.Add(客户);
var response=Request.CreateResponse(HttpStatusCode.Created,客户);
字符串uri=Url.Link(“DefaultApi”,新的{customerID=customer.customerID});
response.Headers.Location=新Uri(Uri);
返回响应;
}
公共产品(字符串customerID,客户)
{
customer.CustomerID=CustomerID;
如果(!repository.Update(客户))
{
抛出新的HttpResponseException(HttpStatusCode.NotFound);
}
}
public void DeleteProduct(字符串customerID)
{
Customer=repository.Get(customerID);
如果(客户==null)
{
抛出新的HttpResponseException(HttpStatusCode.NotFound);
}
删除(customerID);
}
}
静态只读ICCustomerRepository存储库=新建
CustomerRepository()
这里我的存储库是硬代码。我怎样才能通过统一注入它
请给我指路。谢谢
构造函数注入
public class CustomerController : ApiController {
readonly ICustomerRepository repository;
public CustomerController(ICustomerRepository repository) {
this.repository = repository;
}
//...other code removed for brevity
}
为Web api配置Unity
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...
}
}
嘿@Nkosi,我已经用同样的方法实现了,但是在UnityResolver.cs类中获取GetService方法,错误是-->“解析失败,有错误:没有公共构造函数可用于System.Web.Http.Metadata.ModelMetadataProvider类型。”,你知道吗?
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();
}
}