How can I get an instance in the StructureMap Registy constructor?

How can I get an instance of some type (registered in another registry) inside the StructureMap Registy constructor? I want to use a code like this:

    public RepositoriesRegistry()
    {
        IApplicationSettings lApplicationSettings =
            ObjectFactory.GetInstance<IApplicationSettings>();
        Debug.Assert(lApplicationSettings != null);

        const string cSupportedDevicesConnectionString =
            "metadata=res://*/Models.SupportedDevices.Database.SupportedDevicesModel.csdl|res://*/Models.SupportedDevices.Database.SupportedDevicesModel.ssdl|res://*/Models.SupportedDevices.Database.SupportedDevicesModel.msl;provider=System.Data.SqlClient;provider connection string=\"{0}\"";
        string lSupportedDevicesConnectionString =
            string.Format(cSupportedDevicesConnectionString, lDatabaseConnectionString);
        SupportedDevicesEntities lSupportedDevicesEntities =
            new SupportedDevicesEntities(lSupportedDevicesConnectionString);
        ForRequestedType<SupportedDevicesEntities>().TheDefault.IsThis(
            lSupportedDevicesEntities);
        ForRequestedType<ISupportedDevicesRepository>().TheDefault.IsThis(
            new SupportedDevicesRepository(lSupportedDevicesEntities));

    }

IApplicationSettings is an interface to application settings. The specific type that implements this interface (currently the ConfigFileApplicationSettings class) is registered in another registry as follows:

    public ApplicationServicesRegistry()
    {
        ForRequestedType<IApplicationSettings>().TheDefault.IsThis(
            new ConfigFileApplicationSettings());
    }

And both registers are registered in Bootstrapper:

        #region IBootstrapper Members

    public void BootstrapStructureMap()
    {
        ObjectFactory.Initialize(InitalizeStructureMapContainer);
    }

    #endregion

    #region Private properties

    private static bool HasStarted { get; set; }

    #endregion

    #region Private methods

    private void InitalizeStructureMapContainer(IInitializationExpression x)
    {
        x.IgnoreStructureMapConfig = true;
        x.AddRegistry<ViewModelRegistry>();
        x.AddRegistry<ApplicationServicesRegistry>();
        x.AddRegistry<RepositoriesRegistry>();
        x.AddRegistry<DataOperationsRegistry>();
    }

    #endregion

When I try to get an instance of IApplicationRegisty in the registry constructor, I have an error (of course). I do not understand how to use StructureMap correctly. Maybe I should do something different. But in any case, can I get an instance of some type that was previously registered in the registry constructor?

+5
1

. ( :)) , StructureMap .

, , , - . .

public class SettingsRegistry : Registry
{
    public SettingsRegistry()
    {
        ForRequestedType<ISettingsProvider>().TheDefault.Is.OfConcreteType<AppSettingsProvider>();

        Scan(s =>
        {
            s.TheCallingAssembly();
            s.With<SettingsScanner>();
        });
    }
}

public class RegistryNeedingSettings : Registry
{
    public RegistryNeedingSettings()
    {
        var settingsContainer = new Container(new SettingsRegistry());
        var coreSettings = settingsContainer.GetInstance<CoreSettings>();

        //configuration needing access to the settings.
    }
}

, .

, .

+7

All Articles