问题描述
我的类结构是:
public class PhotoService {
private IRepository _repo;
private DTConfig _config;
public PhotoService(IRepository repo, DTConfig config)
{
_repo = repo;
_config = config;
}
}
public class DTConfig
{
private int _accountId;
public DTConfig(int accountId)
{
_accountId = accountId;
}
}
我的Ninject绑定如下:
My Ninject Bindings are like:
var kernel = new StandardKernel();
kernel.Bind<IPhotoService>().To<PhotoService>();
kernel.Bind<IRepository>().To<Repository>();
kernel.Bind<DTConfig>().ToSelf();
现在我想要的是传递 accountId
作为参数,同时创建 PhotoService 的实例
Now what I want, is to pass the accountId
as a parameter while creating an instance of PhotoService
var photo = kernel.Get<IPhotoService>();
如何在创建PhotoService的实例时将参数传递给DTConfig。 accountId是从服务获取的,并且在编译时不可用。
How can I pass the parameter to DTConfig while creating an instance of PhotoService. The accountId is fetched from a service and is not available in compile time.
如果需要任何其他信息,请随意评论。
Feel free to comment if any other information is required.
推荐答案
Yannick的答案是好的,但是,这里有一个替代方法:创建 IAccountService
具有查找和返回AccountId的逻辑。然后将该服务注入到您的 IPhotoService
。
Yannick's answer is a good, however, here's an alternative: create an IAccountService
that has the logic to find and return the AccountId. Then inject that service into your IPhotoService
.
这篇关于如何使用Ninject将参数传递到依赖关系链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!