【发布时间】:2013-11-13 13:19:12
【问题描述】:
在我的界面包中,当用户想要更改有关艺术家的某些信息时,我会运行以下代码:
IArtist artistToChange = ContainerHelper.Container.GetExportedValue<IArtist>();
artistToChange.Load(new Guid("provided guid"));
artistToChange.SomeProperty = newValue;
artistToChange.Update();
作为艺术家是我领域中的一个实体,它由必须加载的 IUser CreatedBy 和 IUser LastAlteredBy 属性组成(想想多对一关系)。每个实体也有自己的存储库。 IArtist 拥有一个 IArtistRepository,就像 IUser 拥有 IUserRepository 一样。
我的问题如下:如何在维护 IoC 的同时在 IArtist.Load() 中获取 IUser 的具体实现实例(没有 IArtist 的具体实现,不知道具体实现IUser)?
(为方便起见,我们将CArtist 称为IArtist 的具体实现,将CUser 称为IUser 的实现。)
考虑到这一点,我想将容器传递给 Entities 以便他们也可以请求部件,但我不知道这是一个好主意还是反模式,主要是因为我使用构造函数注入和我的“CArtist”构造函数看起来像这样:
[ImportingConstructor]
public CArtist(IArtistRepository repository)
{
this.repository = repository;
}
但我不能让容器自己注入类似的东西
[ImportingConstructor]
public CArtist(IArtistRepository repository, CompositionContainer container)
{
this.container = container
this.repository = repository;
}
所以基本上就是这样......我在这里很迷茫......结果证明这是一个寻求帮助/指导的呼声,而不是一个问题......
PS:如果需要任何其他信息,请询问!
【问题讨论】:
-
您打算如何使用 CArtist 类中的容器?容器会比使用 ImportAttribute 修饰的属性更灵活吗?
标签: c# inversion-of-control mef ioc-container