【发布时间】:2015-03-17 21:52:27
【问题描述】:
ViewModel 依赖于一个 MyObject 列表,该列表绑定到一个看起来永远不会被调用的 Repository 方法?
CompositionRoot
public sealed class CompositionRoot {
public CompositionRoot(IKernel kernel) {
if (kernel == null) throw new ArgumentNullException("kernel");
this.kernel = kernel;
}
public void ComposeObjectGraph() {
BindRepositoriesByConvention();
BindDomainModel();
}
private void BindDomainModel() {
kernel
.Bind<IList<MyObject>>()
.ToMethod(ctx => ctx.Kernel.Get<IMyObjectsRepository>().FindAllMyObjects())
.WhenInjectedInto<MyObjectsManagementViewModel>();
}
private void BindRepositoriesByConvention() {
kernel.Bind(s => s
.FromThisAssembly()
.SelectAllClasses()
.EndingWith("Repository")
.BindSelection((type, baseType) => type
.GetInterfaces()
.Where(iface => iface.Name.EndsWith("Repository"))));
}
private readonly IKernel kernel;
}
MyObjectsManagementViewModel
public class MyObjectsManagementViewModel {
public MyObjectsViewModel(IList<MyObject> model) {
if (model == null) throw new ArgumentNullException("model");
Model = model;
}
public MyObject Current { get; set; }
public IList<MyObject> Model { get; set; }
}
MyObjectsRepository
public class MyObjectsRepository
: NHibernateRepository<MyObject>
, IMyObjectsRepository {
public MyObjectsRepository(ISession session) : base(session) { }
public IList<MyObject> FindAllMyObjects() { return GetAll(); }
}
NHibernateRepository 是一个抽象类,它公开受保护的成员,允许人们通过诸如 IMyObjectsRepository 之类的接口自定义方法名称,以声明一个对域更友好的名称,比方说。
对象映射工作正常,因为NHibernate 已正确创建并更新底层数据库,执行Domain-Driven Design。
问题肯定是我在将 MyObject 列表绑定到 Repository 方法时对 Ninject 的理解或误用(我相信)。
我在 FindAllMyObjects 方法中设置了一个断点,但它永远不会被命中?
注入到 MyObjectsManagementViewModel 构造函数中的 MyObjects 列表始终为空?
【问题讨论】: