Ninject绑定到方法时不会触发方法吗? [英] Ninject does not trigger method when binding to method?
问题描述
ViewModel 取决于绑定的MyObject列表到看起来像它的 存储库 方法永远不会被叫到?
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(); }
}
NHibernateRepository
是一个抽象类,它公开受保护的成员,从而使人们可以通过诸如IMyObjectsRepository
之类的接口自定义方法名称,以声明更域名友好的名称.
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.
对象映射工作正常,因为 NHibernate 已正确创建并更新了基础数据库, 域驱动设计 .
The objects mappings work fine as NHibernate has properly created and updated the underlying database doing Domain-Driven Design.
问题肯定与我对 否注入 ,同时将MyObject列表绑定到 Repository 方法.
The problem is definitely around my understanding or misuse (I believe) of Ninject while binding the list of MyObject to the Repository method.
-
我在 FindAllMyObjects 方法上设置了一个断点,但它从未被命中?
I've put a breakpoint to the FindAllMyObjects method, and it never gets hit?
,注入到MyObjectsManagementViewModel构造函数中的MyObjects列表始终是空的吗?
and the list of MyObjects being injected into the MyObjectsManagementViewModel constructor is always an empty one?
推荐答案
我认为这里的问题是您绑定到了Ninject不会解析的类型(IList<T>
).最简单的解决方案是绑定到Func,该Func将返回所需的对象.
I think the issue here is you are binding to a type (IList<T>
) that Ninject isn't expecting to resolve. the simplest solution is to instead bind to a Func that will return the object(s) you want.
例如:
kernel
.Bind<Func<IList<MyObject>>>()
.ToMethod(ctx => () => ctx.Kernel.Get<IMyObjectsRepository>().FindAllMyObjects())
.WhenInjectedInto<MyObjectsManagementViewModel>();
然后:
public MyObjectsViewModel(Func<IList<MyObject>> model) {
if (model == null) throw new ArgumentNullException("model");
Model = model();
}
在构造MyObjectsViewModel对象时,这可以有效地从存储库中延迟加载项目.
this effectively does a lazy-load of the items from the repository when the MyObjectsViewModel object is constructed.
另一种选择(可能更清晰,更好的设计)是创建一个像IMyObjectProvider
这样的新接口,该接口仅负责查找并返回正确的数据.那么该接口将被注入到您的视图模型中,而不是实际的模型对象中.
another alternative (which may be more clear and better design) would be to create a new interface like IMyObjectProvider
, which is responsible only for finding and returning the correct data. then that interface would be injected into your viewmodel instead of the actual model objects.
这篇关于Ninject绑定到方法时不会触发方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!