结合具体实例组成部分
有什么方法可以将特定实例作为其依赖项之一进行组合(或获取导出的值)?
我有这样的事情:
public interface IEntityContext
{
IEntitySet<T> GetEntitySet<T>();
}
[Export(typeof(IEntitySet<MyEntity>))]
class MyEntitySet
{
public MyEntitySet(IEntityContext context)
{
}
}
// then through code
var container = ...;
using (var context = container.GetExportedValue<IEntityContext>())
{
var myEntitySet = context.GetEntitySet<MyEntity>();
// I wan't myEntitySet to have the above context constructor injected
}
为了可测试性,我正在尝试模拟实体框架之类的东西。但不确定我是否愿意走这条路。无论如何,我应该为此目的创建一个新容器吗?特定于模拟此一个 IEntityContext
对象的容器。
Is there any way I can compose (or get an exported value) with a specific instance as one of it's dependencies?
I have something like this:
public interface IEntityContext
{
IEntitySet<T> GetEntitySet<T>();
}
[Export(typeof(IEntitySet<MyEntity>))]
class MyEntitySet
{
public MyEntitySet(IEntityContext context)
{
}
}
// then through code
var container = ...;
using (var context = container.GetExportedValue<IEntityContext>())
{
var myEntitySet = context.GetEntitySet<MyEntity>();
// I wan't myEntitySet to have the above context constructor injected
}
I'm trying to mock something like entity framework for testability sake. Not sure though if I would want to go down this road. Anyway, should I be creating a new container for this very purpose. A container specific to the mocking of this one IEntityContext
object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,如果我的理解是正确的,您希望能够注入 MyEntitySet 实例可用的任何 IEntityContext 吗?
鉴于您想模拟 IEntityContext?如果是这样,您可以执行以下操作:(
即使用 Moq)
您可以通过添加导出的值来使用现有的 CompositionContainer 基础设施。
这有帮助吗?抱歉,似乎不太清楚您要做什么......
So, if my understanding is correct, you want to be able to inject whatever IEntityContext is available to your instance of MyEntitySet?
Given that you then want to mock the IEntityContext? If so, you could then do this:
(That's using Moq)
You can use your existing CompositionContainer infrastructure by adding an exported value.
Does that help at all? Sorry it doesn't seem exactly clear what you are trying to do...