webadvanced / Structuremap.MVC5

Apache License 2.0
21 stars 9 forks source link

How to Resolve an Instance in Structuremap RegistrationConvention #18

Open SaMirzaei opened 6 years ago

SaMirzaei commented 6 years ago

I'm using Structuremap as IOC container.

In old version of Structremap I used ObjectFactory, now I have updated to Structremap.Mvc5 But I can't create an instance of 'ISettingService' in my BuildRegistration method.

My SettingConvention class is: `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));
            }
        });
    });
}

}`

How can I create an instance of 'ISettingService' instead of writing 'ObjectFactory.Resolve()' ?

Thanks everyone for taking the time to try and help explain.