Structuremap支持Lazy开箱即用? [英] Does Structuremap support Lazy out of the box?

查看:150
本文介绍了Structuremap支持Lazy开箱即用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结构图允许您以懒惰的方式进行构造函数注入?
意思是不要创建被注入的对象,直到它被使用?

Does structuremap allow you to do constructor injection in a lazy fashion? Meaning not creating the object which is injected until it is used?

推荐答案

更新: StructureMap v3实现了这个开箱即用,所以这个技巧已经不再需要了。

UPDATE: StructureMap v3 implements this out of the box, so this trick is no longer necessary.

StructureMap版本2不,但有一些技巧,你可以让它做我相信你正在寻找的东西。首先,您可以手动如此连接 Lazy< T> 实例:

StructureMap version 2 doesn't, but with a few tricks you can get it to do what I believe you are looking for. First of all, you can already wire up Lazy<T> instances manually like this:

container = new Container(x =>
{
    x.Scan(y =>
    {
        y.TheCallingAssembly();
        y.WithDefaultConventions();
    });

    x.For<Lazy<IFoo>>().Use(y => new Lazy<IFoo>(y.GetInstance<Foo>));
    x.For<Lazy<IBar>>().Use(y => new Lazy<IBar>(y.GetInstance<Bar>));
    x.For<Lazy<IBaz>>().Use(y => new Lazy<IBaz>(y.GetInstance<Baz>));
});

这个功能很好,但你必须单独注册每个类型。如果您可以利用更加基于会议的方法,那将更好。理想情况下,以下语法会很好。

This works just fine, but you have to register each and every type individually. It would be nicer if you could take advantage of a more convention-based approach. Ideally, the following syntax would be nice.

x.For(typeof(Lazy<>)).Use(typeof(Lazy<>));

这种语法实际上是有用的。不幸的是,在运行时,StructureMap将尝试找到 Lazy< T> 的greediest构造函数,并且定位在 public Lazy(Func< T> valueFactory ,bool isThreadSafe)。由于我们没有告诉它如何处理布尔值isThreadSafe参数,它会在尝试解析懒惰时抛出异常。

This syntax actually works... somewhat. Unfortunately, at runtime, StructureMap will attempt to find the "greediest" constructor for Lazy<T> and settle on public Lazy(Func<T> valueFactory, bool isThreadSafe). Since we didn't tell it what to do with the boolean isThreadSafe parameter, it will throw an exception when it tries to resolve `Lazy'.

Lazy状态的文档默认 Lazy(Func< T> valueFactory)构造函数的线程安全模式是 LazyThreadSafetyMode.ExecutionAndPublication ,这只是通过将true传递到上面的构造函数的isThreadSafe参数来实现。所以,如果我们可以告诉StructureMap,为 isThreadSafe 传递 true ,我们会得到与我们调用的相同的行为我们实际上想要使用的构造函数(例如 Lazy(Func< T> valueFactory))。

The documentation for Lazy states that the "thread safety mode" of the default Lazy(Func<T> valueFactory) constructor is LazyThreadSafetyMode.ExecutionAndPublication, which just so happens to be what you get by passing true into the isThreadSafe parameter of the constructor above. So, if we could just tell StructureMap to pass true for isThreadSafe, we would get the same behavior as if we called the constructor we actually wanted to use in the first place (e.g. Lazy(Func<T> valueFactory)).

只需注册 x.For(typeof(bool))。使用(y => true)非常鲁莽和危险,因为我们将告诉StructureMap继续使用任何地方的任何布尔值 true 。相反,我们需要告诉StructureMap为这个一个布尔参数使用的值,我们可以这样做。

Simply registering x.For(typeof(bool)).Use(y => true) would be very reckless and dangerous since we would be telling StructureMap to go ahead and use the value true for any boolean anywhere. Instead, we need to tell StructureMap what value to use for just this one boolean parameter, which we can do like this.

x.For(typeof(Lazy<>)).Use(typeof(Lazy<>))
 .CtorDependency<bool>("isThreadSafe").Is(true);

当解析<$ c $时,StructureMap现在知道使用isThreadSafe参数的true值C>懒< T> 。我们现在可以在构造函数中使用 Lazy< T> ,并获得我相信您正在寻找的行为。

StructureMap now knows to use the value of "true" for the isThreadSafe parameter when resolving Lazy<T>. We can now use Lazy<T> in constructor parameters, and get the behavior I believe you were looking for.

您可以更详细地阅读懒惰课程 here

You can read about the Lazy class in more detail here.

这篇关于Structuremap支持Lazy开箱即用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆