Ale*_*oun 5 c# ninject asp.net-mvc-3
我是Ninject的新手,所以我确信这是我做错了,我只是不确定是什么.我在我的MVC3 Web应用程序中使用Ninject和Ninject.MVC3.这是我正在尝试做的一个例子.
我正在使用Repository模式:
public interface IRepository<T>
{
T Get(object id);
IList<T> GetAll();
void Add(T value);
void Update(T value);
void Delete(T value);
}
Run Code Online (Sandbox Code Playgroud)
对于具体类型:
public Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public Customer()
{
}
}
Run Code Online (Sandbox Code Playgroud)
现在我有两个独立的存储库,一个需要注入数据库存储库的缓存版本:
public CachedCustomerRepository : IRepository<Customer>
{
private IRepository<Customer> _repository;
public Customer Get(object id)
{
Customer cust = new Customer();
IList<Customer> custs = GetAll();
if (custs != null && custs.Count > 0)
cust = custs.FirstOrDefault(c => c.Id == int.Parse(id.ToString()));
return cust;
}
public IList<Customer> GetAll()
{
IList<Customer> custs = HttpRuntime.Cache["Customers"] as IList<Customer>;
if (custs == null)
{
custs = _repository.GetAll();
if (custs != null && custs.Count() > 0)
{
double timeout = 600000d;
HttpRuntime.Cache.Insert("Customers", custs, null, DateTime.UtcNow.AddMilliseconds(timeout), System.Web.Caching.Cache.NoSlidingExpiration);
}
else
{
throw new NullReferenceException();
}
}
return custs;
}
public void Add(Customer value)
{
throw new NotImplementedException();
}
public void Update(Customer value)
{
throw new NotImplementedException();
}
public void Delete(Customer value)
{
throw new NotImplementedException();
}
public CachedCustomerRepository()
{
}
[Inject]
public CachedCustomerRepository(IRepository<Customer> repository)
{
_repository = repository;
}
}
public class CustomerRepository : IRepository<Customer>
{
public Customer Get(object id)
{
Customer cust = new Customer();
IList<Customer> custs = GetAll();
if (custs != null && custs.Count > 0)
cust = custs.FirstOrDefault(c => c.Id == int.Parse(id.ToString()));
return cust;
}
public IList<Customer> GetAll()
{
//Customer retrieval code
}
public void Add(Customer value)
{
throw new NotImplementedException();
}
public void Update(Customer value)
{
throw new NotImplementedException();
}
public void Delete(Customer value)
{
throw new NotImplementedException();
}
public CachedCustomerRepository()
{
}
}
Run Code Online (Sandbox Code Playgroud)
我像这样设置了一个NinjectModule:
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<IRepository<Customer>>().To<CustomerRepository>();
}
}
Run Code Online (Sandbox Code Playgroud)
我修改了AppStart文件夹中的NinjectMVC3.cs以在创建内核时获取模块:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel(new ServiceModule());
RegisterServices(kernel);
return kernel;
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我使用这个:
public ViewResult Index()
{
IRepository<Customer> custRepo = new CachedCustomerRepository();
return View(custRepo.GetAll());
}
Run Code Online (Sandbox Code Playgroud)
它爆炸了_repository.GetAll()我的线CachedCustomerRepository.
我设置了一个断点来确保CreateKernel()正在执行并获取绑定,它就是这样.我只是不确定注射为什么没有发生.另一方面,我不知道它是否重要,IRepository,Repositories和具体类型都在一个单独的类库中,并在mvc3 Web应用程序中引用.Web应用程序和类库都引用了Ninject,Web应用程序也引用了Ninject.MVC3.绑定和内核创建都在Web App中进行.
首先,您在控制器中调用了错误的构造函数。此无参数构造函数不会调用任何其他内容,这就是您收到 null 异常的原因。其次,您想要重构控制器,以便不存在直接依赖关系。
您想要执行如下操作:
public class SomeController
{
private readonly IRepository<Customer> repo;
public SomeController(IRepository<Customer> repo)
{
this.repo = repo;
}
public ViewResult Index()
{
return View(this.repo.GetAll());
}
}
Run Code Online (Sandbox Code Playgroud)
这样,Ninject 就会帮你解决依赖问题!IRepository<Customer>MVC会要求 Ninject 的内核创建一个控制器。由于 Ninject 有这个“绑定”,它会尝试CustomerRepository为您实例化。
另外,为什么要创建“CachedRepository”?我真的真的认为你过早地优化了这一点。老实说,您只需要一个CustomerRepository,然后将其连接到 Ninject 模块中。