我有问题。在我第一次通过IRepository模式引入依赖注入(IoC)时,一切都很好。那时我用于每个存储库的个别界面。现在我尝试将所有接口合并到一个通用类型接口中,现在我遇到了ninject控制器factrory的问题。 GetControllerInstance方法中的“在转换时出现异常”
public interface IRepositoryEF<TAble> where TAble: class
{
IQueryable<TAble> GetAll();
void Add(TAble);
TAble GetById(long id);
TAble DeleteById(long id);
}
public interface MealsRepositoryEF: IRepositoryEF<Meal>
{
IQueryable<Meal> GetAllAsQueryable()
{
}
void Add(Meal meal)
{
}
Meal GetById(long id)
{
}
Meal DeleteById(long id)
{
}
}
public interface GoalsRepositoryEF: IRepositoryEF<Goal>
{
IQueryable<Goal> GetAllAsQueryable()
{
}
void Add(Goal goal)
{
}
Patient GetById(long id)
{
}
Patient DeleteById(long id)
{
}
}
public class NinjectControllerFactory: DefaultControllerFactory {
private IKernel ninjectKernel;
public NinjectControllerFactory() {
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext,
Type controllerType)
{
return controllerType == null?
null :
(IController)ninjectKernel.Get(controllerType);
}
private void AddBindings() {
// put additional bindings here
ninjectKernel.Bind<IRepositoryEF<Meal>>().To(typeof(MealsRepositoryEF));
ninjectKernel.Bind<IRepositoryEF<Goal>>().To(typeof(GoalsRepositoryEF));
}
}
public class MealsController : Controller
{
private IRepositoryEF<Meal> mealsRepository;
public PatientController(IRepositoryEF<Meal> repo)
{
mealsRepository= repo;
}
}