结合具体实例组成部分

发布于 2024-09-09 10:28:02 字数 661 浏览 8 评论 0原文

有什么方法可以将特定实例作为其依赖项之一进行组合(或获取导出的值)?

我有这样的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-09-16 10:28:02

那么,如果我的理解是正确的,您希望能够注入 MyEntitySet 实例可用的任何 IEntityContext 吗?

[Export(typeof(IEntitySet<MyEntity>))]
public class MyEntitySet : IEntitySet<MyEntity>
{
    [ImportingConstructor]
    public MyEntitySet(IEntityContext context)
    {

    }
}

鉴于您想模拟 IEntityContext?如果是这样,您可以执行以下操作:(

var contextMock = new Mock<IEntityContext>();
var setMock = new Mock<IEntitySet<MyEntity>>();

contextMock
    .Setup(m => m.GetEntitySet<MyEntity>())
    .Returns(setMock.Object);

Container.ComposeExportedValue<IEntityContext>(contextMock.Object);

var context = Container.GetExportedValue<IEntityContext>();
var entitySet = context.GetEntitySet<MyEntity>();

即使用 Moq)

您可以通过添加导出的值来使用现有的 CompositionContainer 基础设施。

这有帮助吗?抱歉,似乎不太清楚您要做什么......

So, if my understanding is correct, you want to be able to inject whatever IEntityContext is available to your instance of MyEntitySet?

[Export(typeof(IEntitySet<MyEntity>))]
public class MyEntitySet : IEntitySet<MyEntity>
{
    [ImportingConstructor]
    public MyEntitySet(IEntityContext context)
    {

    }
}

Given that you then want to mock the IEntityContext? If so, you could then do this:

var contextMock = new Mock<IEntityContext>();
var setMock = new Mock<IEntitySet<MyEntity>>();

contextMock
    .Setup(m => m.GetEntitySet<MyEntity>())
    .Returns(setMock.Object);

Container.ComposeExportedValue<IEntityContext>(contextMock.Object);

var context = Container.GetExportedValue<IEntityContext>();
var entitySet = context.GetEntitySet<MyEntity>();

(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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文