.net c#我可以让 StructureMap 管理具体类型的生命周期吗?

Can I make StructureMap manage the lifecycle of a concrete type?
2021-11-25
  •  译文(汉语)
  •  原文(英语)

我有一种叫做 TypeA 的类型.

TypeA 实现了 2 个接口:IOne 和 ITwo

public class TypeA : IOne, ITwo
{
    public TypeA(IThree ithree)
    {
         ...
    }
}

我想配置 StructureMap 以便每个线程只有一个 TypeA 实例.我希望 IOne 或 ITwo 的任何分辨率返回相同的 TypeA 实例(每个线程一个).

我尝试了以下配置:

ObjectFactory.Configure(x =>
{
    var thread = Lifecycles.GetLifecycle(InstanceScope.ThreadLocal);
    x.For<TypeA>().LifecycleIs(thread).Use<TypeA>();
    x.For<IOne>().LifecycleIs(thread).Use<TypeA>();
    x.For<ITwo>().LifecycleIs(thread).Use<TypeA>();
    x.For<IThree>().LifecycleIs(thread).Use<TypeB>();
});

然后像这样解决

var test = ObjectFactory.GetInstance<IOne>();
var test2 = ObjectFactory.GetInstance<ITwo>();

但是这两个调用解析为不同的对象.

首先,我正在尝试使用 StructureMap 来实现什么?如果是这样,那么我该怎么做?

解决过程1
For<IOne>().LifecycleIs(thread).Use<Impl>();
For<ITwo>().LifecycleIs(thread).Use(x => x.GetInstance<IOne>() as Impl);

回复评论:

For<Impl>().LifecycleIs(thread).Use<Impl>();
For<IOne>().LifecycleIs(thread).Use(x => x.GetInstance<Impl>());
For<ITwo>().LifecycleIs(thread).Use(x => x.GetInstance<Impl>());

这会做你想做的.我个人的意见是,因为您正在控制 StructureMap 的配置,所以您可以假设请求对象 (ITwo) 将返回一个非空值,并且第一个配置没问题.这是一个偏好问题.

I have a type called TypeA.

TypeA implements 2 interfaces: IOne and ITwo

public class TypeA : IOne, ITwo
{
    public TypeA(IThree ithree)
    {
         ...
    }
}

I want to configure StructureMap so that there is only ever one instance of TypeA per thread. I want any resolution of EITHER IOne or ITwo to return the same instance of TypeA (one per thread).

I have tried the following config:

ObjectFactory.Configure(x =>
{
    var thread = Lifecycles.GetLifecycle(InstanceScope.ThreadLocal);
    x.For<TypeA>().LifecycleIs(thread).Use<TypeA>();
    x.For<IOne>().LifecycleIs(thread).Use<TypeA>();
    x.For<ITwo>().LifecycleIs(thread).Use<TypeA>();
    x.For<IThree>().LifecycleIs(thread).Use<TypeB>();
});

and then resolve like this

var test = ObjectFactory.GetInstance<IOne>();
var test2 = ObjectFactory.GetInstance<ITwo>();

But these two calls resolve to different objects.

Firstly, is what I'm trying to achieve possible using StructureMap? If so then how do I do it?

Solutions1
For<IOne>().LifecycleIs(thread).Use<Impl>();
For<ITwo>().LifecycleIs(thread).Use(x => x.GetInstance<IOne>() as Impl);

Answer to Comment:

For<Impl>().LifecycleIs(thread).Use<Impl>();
For<IOne>().LifecycleIs(thread).Use(x => x.GetInstance<Impl>());
For<ITwo>().LifecycleIs(thread).Use(x => x.GetInstance<Impl>());

This will do what you want. My personal opinion is that because you are controlling the configuration of StructureMap you can assume that asking for the object (ITwo) will return a non null value and the first configuration is fine. That is a matter of preference.

转载于:https://stackoverflow.com/questions/15240784/can-i-make-structuremap-manage-the-lifecycle-of-a-concrete-type

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入