使用Simple Injector注入web api Controller Constructor

时间:2017-06-28 17:31:50

标签: c# inversion-of-control simple-injector service-layer

目前收到以下错误

  

ChurchController类型的构造函数包含名称为' churchService'的参数。并键入未注册的IChurchService。请确保已注册IChurchService,或更改ChurchController的构造函数。

即时通讯使用Simple Injector但我不熟悉IOC不确定错误消息的含义或我做错了我已经包含了我的代码

SimpleInjectorWebApiInitializer.cs

 public static class SimpleInjectorWebApiInitializer
    {
        public static void Initialize()
        {
            var container = new Container();
            container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

            InitializeContainer(container);

            container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

            container.Verify();

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

        private static void InitializeContainer(Container container)
        {
            // For instance:
            container.Register(typeof(IRepository<>), typeof(EFRepository<>));


        }

    }
 public interface IRepository<T> where T : class
    {
        //To query using LINQ
        IQueryable<T> GetAll();

        //Returning Movie or Review by id
        T GetById(int id);

        //Adding Movie or Review
        void Add(T entity);

        //Updating Movie or Review
        void Update(T entity);

        //Deleting Moovie or Review
        void Delete(T entity);

        //Deleting Movie or Review by id
        void Delete(int id);

    }

public class EFRepository<T> : IRepository<T> where T : class
{
    public EFRepository(DbContext dbContext)
    {
        if (dbContext == null)
            throw new ArgumentNullException("dbContext");
        DbContext = dbContext;
        DbSet = DbContext.Set<T>();
    }

    protected DbContext DbContext { get; set; }
    protected DbSet<T> DbSet { get; set; }
    public virtual IQueryable<T> GetAll()
    {
        return DbSet;
    }

    public virtual T GetById(int id)
    {
        return DbSet.Find(id);
    }

    public virtual void Add(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Detached)
        {
            dbEntityEntry.State = EntityState.Added;
        }
        else
        {
            DbSet.Add(entity);
        }
    }

    public virtual void Update(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Detached)
        {
            DbSet.Attach(entity);
        }
        dbEntityEntry.State = EntityState.Modified;
    }

    public void Delete(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Deleted)
        {
            dbEntityEntry.State = EntityState.Deleted;
        }
        else
        {
            DbSet.Attach(entity);
            DbSet.Remove(entity);
        }
    }

    public void Delete(int id)
    {
        var entity = GetById(id);
        if (entity == null) return;

        Delete(entity);
    }
}

我的模特

   public class Church
    {
        public int Id { get; set; }
        public string ChurchName { get; set; }
        public virtual ICollection<User> Users { get; set; }

    }

设置我的控制器来调用我的服务

public class ChurchController : ApiController
{
    private readonly IChurchService _churchService;
    public ChurchController(IChurchService churchService)
    {

        _churchService = churchService;
    }

    public IEnumerable<Church> Get()
    {

        var result = _churchService.GetAll();

        return result;

    }
}

1 个答案:

答案 0 :(得分:2)

之前我从未使用过Simple Injector,但IoC模式总是一样的。正如史蒂文所说,你应该在容器中注册IChurchService,因此框架知道在需要时要注入的实例类型。

private static void InitializeContainer(Container container)
{
    // For instance:
    container.Register(typeof(IRepository<>), typeof(EFRepository<>));
    //Adding this should resolve it
    container.Register(typeof(IChurchService), typeof(ChurchService));
}

如果ChurchService有任何依赖关系,请务必注册这些依赖关系。注册完成对象图所需的所有依赖项