2

我使用 Structuremap 作为 IOC 容器。

在旧版本的 Structremap 中,我使用了 ObjectFactory,现在我已从以下链接更新为 Structremap.Mvc5:

https://github.com/webadvanced/Structuremap.MVC5

但我无法在 BuildRegistration 方法中创建“ISettingService”的实例。

我的 SettingConvention 课程是:

public class SettingsConvention : IRegistrationConvention
{
    static readonly MethodInfo BuildMethod = typeof(SettingsConvention).GetMethod(
    "BuildRegistration",
    BindingFlags.Static | BindingFlags.NonPublic);

    static ISettings BuildRegistration<TSettings>() where TSettings : ISettings, new()
    {
        // It's work in old version but now It's not working
        // How I should create an Instance Here...
        // var settingService = ObjectFactory.Resolve<ISettingService>();

        return settingService.LoadSetting<TSettings>();
    }

    public void ScanTypes(TypeSet types, StructureMap.Registry registry)
    {
        // Only work on concrete types
        types.FindTypes(TypeClassification.Concretes | TypeClassification.Closed).ForEach(type =>
        {
            // Register against all the interfaces implemented
            // by this concrete class
            type.GetInterfaces().ForEach(@interface =>
            {
                if (type.CanBeCastTo(typeof(ISettings)) && typeof(ISettings).IsAssignableFrom(type))
                {
                    var buildMethod = BuildMethod.MakeGenericMethod(type);

                    registry.For(type).Use(buildMethod.Invoke(null, null));
                }
            });
        });
    }
}

如何创建 'ISettingService' 的实例而不是编写 'ObjectFactory.Resolve()' ?

感谢大家花时间尝试帮助解释。

4

0 回答 0