【发布时间】:2014-03-11 09:27:02
【问题描述】:
在我的应用程序中,我使用存储库进行 CRUD 操作。在所有实体中,有些根本不能删除,有些实体(实现ISoftDeletable)只能软删除,有些实体(实现IPermanentDeletable)可以永久删除。 (我正在关注这个帖子Generic Repository with Soft Delete Feature的答案)
我有 3 个相互继承的存储库,如下所示
BaseRepository : IRepository
SoftDeleteRepository : BaseRepository, ISoftDeleteRepository
PermanentDeleteRepository : BaseRepository, IPermanentDeleteRepository
我的问题是,我不想为每个不同类型的实体做单独的绑定,比如
kernel.Bind(typeof(IRepository<Company>)).To(typeof(BaseRepository<Company>));
kernel.Bind(typeof(IRepository<Country>)).To(typeof(PermanentDeleteRepository<Country>));
kernel.Bind(typeof(IRepository<Contact>)).To(typeof(SoftDeleteRepository<Contact>));
但是,相反,我想以某种方式使IRepository 的所有绑定都进入工厂并根据其通用参数所采用的实体类型实例化存储库,并将其注入控制器。
有可能实现吗?
【问题讨论】:
标签: c# entity-framework asp.net-mvc-4 ninject