繁体   English   中英

TinyIOC寄存器类,其构造函数使用注册的类型和指定的类型

[英]TinyIOC register class with constructor using registered types and specified type

我想在其构造函数中注册一个使用先前注册类型的类,但是它也有一个需要指定的构造函数参数。

需要使用新的BackgroundWorkerPoll(1000)创建ConnectionEngine。 我该怎么做呢?

        FreshIOC.Container.Register<IFormsDevice, DeviceWrapper>();
        FreshIOC.Container.Register<IBluetoothQuery, BluetoothQuery>();
        FreshIOC.Container.Register<ISuperConnectionManager, SuperConnectionManager>();

        ....................

        var superConnectionManager = FreshIOC.Container.Resolve<ISuperConnectionManager>();
        var bluetoothQuery = FreshIOC.Container.Resolve<IBluetoothQuery>();
        var formsDevice = FreshIOC.Container.Resolve<IFormsDevice>();

        var connectionEngine = new ConnectionEngine(superConnectionManager,
            bluetoothQuery, new BackgroundWorkerPoll(1000), formsDevice);

我可以这样做,但看起来很麻烦:

        FreshIOC.Container.Register<ConnectionEngine>().UsingConstructor(() => 
        new ConnectionEngine(
            FreshIOC.Container.Resolve<ISuperConnectionManager>() as ISuperConnectionManager,
            FreshIOC.Container.Resolve<IBluetoothQuery>() as IBluetoothQuery,
            new BackgroundWorkerPoll(1000), 
            FreshIOC.Container.Resolve<IFormsDevice>() as IFormsDevice));

好的,这是我能做的最好的事情:

            FreshIOC.Container.Register<IFormsDevice, DeviceWrapper>();
            FreshIOC.Container.Register<IBluetoothQuery, BluetoothQuery>();
            FreshIOC.Container.Register<ISuperConnectionManager, SuperConnectionManager>();
            FreshIOC.Container.Register<DeviceConnectionTracker>().AsSingleton();
            FreshIOC.Container.Register<MasterParameterContainer>().AsSingleton();
            FreshIOC.Container.Register<ParsingEngine>().AsSingleton();

            var connectionEngine = FreshIOC.Container.Resolve<ConnectionEngine>(
                new NamedParameterOverloads() { { "poller", new BackgroundWorkerPoll(1000) } });

            FreshIOC.Container.Register<ConnectionEngine>(connectionEngine); // register it for FreshMVVM

暂无
暂无

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

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