简体   繁体   English

Castle Windsor 更新实例属性

[英]Castle Windsor Update Instance Property

I'm trying to use Castle Windsor to reuse a single instance of my WebApp settings class ( MySettings ).我正在尝试使用 Castle Windsor 来重用我的 WebApp 设置类 ( MySettings ) 的单个实例。 This settings rarely changes, but when it changes, I need to update the instance in the container.这个设置很少改变,但是当它改变时,我需要更新容器中的实例。 I can easily track when the Settings changes, but I can't figure out the right way to do it, can anybody help me?我可以轻松跟踪设置更改的时间,但我想不出正确的方法,有人可以帮助我吗?

The Windsor Installer class is bellow: Windsor 安装程序类如下:

public class SettingsInstaller : IWindsorInstaller
{
    private MySettings Settings { get; set; }

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        UpdateSettings();

        container.Register(
            Component.For<MySettings>()
            .Instance(this.Settings));
    }

    public MySettings UpdateSettings()
    {
        using (DbContext db = new DbContext())
        {
            this.Settings = db.Settings.FirstOrDefault();
        }

        return this.Settings;
    }
}

How can I call the UpdateSettings() and make sure that the container will use the updated Settings in the next Dependency Injection resolution?如何调用UpdateSettings()并确保容器将在下一个依赖注入解决方案中使用更新的Settings

I had no answer yet.我还没有答案。 I have done this, and it is working:我已经这样做了,它正在工作:

public class SettingsInstaller : IWindsorInstaller
{
    private static MySettings Settings { get; set; }

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        UpdateSettings();

        container.Register(
            Component.For<MySettings>()
            .UsingFactoryMethod(() =>
            {
                return SettingsInstaller.Settings;
            })
            .LifestyleTransient());
    }

    public static MySettings UpdateSettings()
    {
        using (DbContext db = new DbContext())
        {
            SettingsInstaller.Settings = db.Settings
                .AsNoTracking()
                .FirstOrDefault();
        }

        return SettingsInstaller.Settings;
    }
}

I think what you want is for MySettings to be a singleton whose state you can then update in other parts of the application.我认为您想要的是 MySettings 成为一个单例,然后您可以在应用程序的其他部分更新其状态。 If you register MySettings as a singleton, then whenever the container resolves a MySettings for you, it will return the same instance.如果您将 MySettings 注册为单例,那么每当容器为您解析 MySettings 时,它都会返回相同的实例。 This is essentially what you are doing in your answer, but you're just storing the instance in a local variable (instead of letting the container do it for you).这基本上就是您在答案中所做的,但您只是将实例存储在局部变量中(而不是让容器为您完成)。

Your registration code can then get very simple:您的注册码可以变得非常简单:

public class SettingsInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
         container.Register(Component.For<MySettings>().LifestyleSingleton());
    }
}

Whenever you need to access the settings, you can request a MySettings from the container using constructor injection (or any other method of getting an instance from a container):每当您需要访问设置时,您都可以使用构造函数注入(或从容器获取实例的任何其他方法)从容器请求 MySettings:

public class ClientClass
{
    private readonly MySettings _settings;

    public ClientClass(MySettings settings)
    {
         _settings = settings;
    }

    public void UpdateSettings()
    {
         _settings.SomeSetting = myNewSetting; // reset them however you need to here
    }
}

After you've called UpdateSettings() in your application somewhere, then the instance the container is holding on to (a singleton) will be updated with myNewSetting, and when the next class requests a MySettings you'll get the same instance with the updated value.在您的应用程序中某处调用 UpdateSettings() 之后,容器所持有的实例(单例)将使用 myNewSetting 进行更新,并且当下一个类请求 MySettings 时,您将获得具有更新的相同实例价值。

If you actually need a new instance of MySettings, then in your UpdateSettions() method you can create an entirely new one using _settings = new MySettings() and the container will serve up the new instance for you from then on.如果您确实需要 MySettings 的新实例,那么在您的 UpdateSettions() 方法中,您可以使用_settings = new MySettings()创建一个全新的实例,然后容器将为您提供新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM