The ViewModel depends on a list of MyObject which is bound to a Repository method that looks like it never gets called?
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(); }
}
The NHibernateRepository
is an abstract class which exposes protected members allowing one to customize the method names through interfaces like IMyObjectsRepository
to state a more domain friendly name, let's say.
The objects mappings work fine as NHibernate has properly created and updated the underlying database doing Domain-Driven Design.
The problem is definitely around my understanding or misuse (I believe) of Ninject while binding the list of MyObject to the Repository method.
I've put a breakpoint to the FindAllMyObjects method, and it never gets hit?
and the list of MyObjects being injected into the MyObjectsManagementViewModel constructor is always an empty one?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…