如何在注册时将组件实例化为Singleton? [英] How do I Instantiate component as Singleton at registration?
问题描述
我可以想象这可能在Castle中很简单,但是我是该技术的新手,并且已经在谷歌上搜索了好几个小时而没有运气!
I can imagine this might be quite straight forward to do in Castle but I'm new to the technology and have been Googling for hours with no luck!
I具有以下内容:
container.Register(
Component.For<MySpecialClass>().UsingFactoryMethod(
() => new MySpecialClass()).LifeStyle.Singleton);
现在很正确,这是延迟加载的,即传递给UsingFactoryMethod()的lambda表达式是在我真正要求Castle解析我的类实例之前,不会执行该程序。
Now quite rightly this is being lazy-loaded, i.e. the lambda expression passed in to UsingFactoryMethod() isn't being executed until I actually ask Castle to Resolve me the instance of the class.
但是我希望Castle在注册实例后立即创建它。
But I would like Castle to create the instance as soon as I have registered it. Is this possible?
推荐答案
您可以像这样使用内置的Startable工具:
You can just use the built it Startable facility like so:
container.AddFacility<StartableFacility>();
container.Register(Component.For<MySpecialClass>().LifeStyle.Singleton.Start());
您可以阅读此处
这篇关于如何在注册时将组件实例化为Singleton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!