TinyIOC注册类,包含使用已注册类型和指定类型

时间:2015-12-04 15:15:14

标签: c# inversion-of-control ioc-container tinyioc freshmvvm

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

需要使用新的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);

2 个答案:

答案 0 :(得分:1)

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

        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));

答案 1 :(得分:0)

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

            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
相关问题