0

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

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

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

2 回答 2

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));
于 2015-12-04T15:22:52.773 回答
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
于 2015-12-04T19:46:16.853 回答