依赖项注入在Web API调用中不起作用 [英] Dependency injection not working in web api call

查看:108
本文介绍了依赖项注入在Web API调用中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WebAPI构建Angular 2 Web应用程序,它是使用依赖项注入松散耦合的Entityframework.我正在为依赖注入使用unity.我在一个解决方案中创建了多个项目来解决分离问题.

Hi I am trying to build angular 2 web application using WebAPI, Entityframework that is loosely coupled using dependency injection. I am using unity for dependency injection. I have created multiple projects in one solution to address the separation concerns.

我已经在unity.config中配置了依赖性,但是当我执行webapi应用程序并键入以下URL http://localhost:8702/api/allcustomers ,我收到消息说客户控制器没有无参数构造函数.我已经在unity.config中设置了我的断点,但从未被击中

I have configured the dependency in unity.config however when i execute the webapi application and type the following url http://localhost:8702/api/allcustomers , I get message saying the customer controller doesn't have parameter-less constructor. I have set my break points in unity.config which never get hit

我想了解我的实现是否正确

I would like to to understand if my implementation is correct as well

下面是我的解决方案的结构

Below is the structure of my solution

CustomerOrder.Business.ObjectsCustomerOrder.Data.Objects(引用业务对象)CustomerOrder.Service.Api(引用业务对象和服务实现)CustomerOrder.Service.Implementation(引用业务对象和数据对象)CustomerOrder.Web(尚未实施)

CustomerOrder.Business.Objects CustomerOrder.Data.Objects (references the business object) CustomerOrder.Service.Api (references business object and service implementation) CustomerOrder.Service.Implementation (references business objects and data objects) CustomerOrder.Web (Yet to implement)

下面是代码

CustomerOrder.Business.Objects

 public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }

        public int? Zip { get; set; }

    }

CustomerOrder.Data.Objects

    public class CustomerDao : ICustomerDao
        {

            IEnumerable<CustomerOrder.BusinessObjects.Customer> ICustomerDao.GetAllCustomers()
            {
                using (var customerOrderContext = new Entities())
                {
                    return (from customer in customerOrderContext.Customers

                            select new CustomerOrder.BusinessObjects.Customer
                            {
                                Id = customer.Id,
                                FirstName = customer.FirstName,
                                LastName = customer.LastName,
                                Address = customer.Address,
                                City = customer.City,
                                Email = customer.Email,
                                Gender = customer.Gender,
                                State = customer.State,
                                Zip = customer.Zip
                            }).ToList();
                }
            }
      }


    public interface ICustomerDao
        {

            /// <summary>
            /// Get All Customers
            /// </summary>
            /// <returns></returns>
            IEnumerable<Customer> GetAllCustomers();

        }

        public interface IDaoFactory
        {
            ICustomerDao CustomerDao { get; }

        }
    }

  public class DaoFactory : IDaoFactory
    {
        public DaoFactory(ICustomerDao CustomerDao, IProductDao ProductDao, IOrderDao OrderDao)
        {
            this.CustomerDao = CustomerDao;
        }

        public ICustomerDao CustomerDao { set; get; }





    }

CustomerOrder.Service.Api

Unity.Config 

    public static void RegisterComponents()
            {
                var container = new UnityContainer();

                // register all your components with the container here
                // it is NOT necessary to register your controllers

                // e.g. container.RegisterType<ITestService, TestService>();

                container.RegisterType<ICustomerProvider, CustomerProvider>();
                container.RegisterType<IOrderProvider, OrderProvider>();
                container.RegisterType<IProductProvider, ProductProvider>();

                GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
            }

CustomerController.cs

    public class CustomerController : ApiController
        {
            private ICustomerProvider customerProvider;


            public CustomerController(ICustomerProvider customerProvider)
            {
                this.customerProvider = customerProvider;
            }

            [Route("api/allcustomers")]
            public IEnumerable<Customer> GetAllCustomers()
            {
                return customerProvider.GetAllCustomers();
            }

CustomerOrder.Service.Implementation

public interface ICustomerProvider
{

    IEnumerable<BusinessObjects.Customer> GetAllCustomers();

}

 public class CustomerProvider : ICustomerProvider
    {

        private readonly IDaoFactory dataAccess;

        public CustomerProvider(IDaoFactory dalFactory)
        {
            this.dataAccess = dalFactory;
        }

        public IEnumerable<BusinessObjects.Customer> GetAllCustomers()
        {
            IList<BusinessObjects.Customer> customerCollection = new List<BusinessObjects.Customer>();
            dataAccess.CustomerDao.GetAllCustomers();
            return customerCollection;
        }
}

上下文类

namespace CustomerOrderData.EF
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class Entities : DbContext
    {
        public Entities()
            : base("name=Entities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
         //   throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Customer> Customers { get; set; }
        public virtual DbSet<OrderDetail> OrderDetails { get; set; }
        public virtual DbSet<Order> Orders { get; set; }
        public virtual DbSet<Product> Products { get; set; }
    }
}

推荐答案

CustomerProvider 中, IDaoFactory 可能由于未注册而无法解决.将此添加到 Unity.Config :

In CustomerProvider, the IDaoFactory is probably not getting resolved because it's not registered. Add this to the Unity.Config:

container.RegisterType<IDaoFactory , DaoFactory >();

这篇关于依赖项注入在Web API调用中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆