【发布时间】:2011-03-09 18:32:52
【问题描述】:
我是 Ninject 的新手,所以我确定这是我做错了,我只是不确定是什么。我在我的 MVC3 Web 应用程序中使用了 Ninject 和 Ninject.MVC3。这是我正在尝试做的一个示例。
我正在使用存储库模式:
public interface IRepository<T>
{
T Get(object id);
IList<T> GetAll();
void Add(T value);
void Update(T value);
void Delete(T value);
}
对于具体类型:
public Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public Customer()
{
}
}
现在我有 2 个独立的存储库,一个需要注入数据库存储库的缓存版本:
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()
{
}
}
我这样设置了一个 NinjectModule:
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<IRepository<Customer>>().To<CustomerRepository>();
}
}
并且我在创建内核时修改了AppStart文件夹中的NinjectMVC3.cs来获取模块:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel(new ServiceModule());
RegisterServices(kernel);
return kernel;
}
在我的控制器中,我正在使用这个:
public ViewResult Index()
{
IRepository<Customer> custRepo = new CachedCustomerRepository();
return View(custRepo.GetAll());
}
它在我的CachedCustomerRepository 中的_repository.GetAll() 线上爆炸了。
我设置了一个断点以确保CreateKernel() 正在执行并获取绑定,它就是这样。我只是不确定为什么注射没有发生。另一方面,我不知道它是否重要,IRepository、Repositories 和具体类型位于单独的类库中,并在 mvc3 Web 应用程序中引用。 Web 应用程序和类库都引用了 Ninject,Web 应用程序也引用了 Ninject.MVC3。绑定和内核创建都在 Web App 中进行。
【问题讨论】:
-
+1 表示每个人在第一次学习依赖注入时所经历的挫折。
标签: c# asp.net-mvc-3 ninject