小编Mik*_*hva的帖子

如何在StructureMap Registy构造函数中获取实例?

如何在StructureMap Registy构造函数中获取某种类型的实例(在不同的注册表中注册)?我想使用这样的代码:

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

    }
Run Code Online (Sandbox Code Playgroud)

IApplicationSettings是应用程序设置的接口.实现此接口的具体类型(当前为ConfigFileApplicationSettings类)在另一个注册表中注册,如下所示:

    public ApplicationServicesRegistry()
    {
        ForRequestedType<IApplicationSettings>().TheDefault.IsThis(
            new ConfigFileApplicationSettings());
    }
Run Code Online (Sandbox Code Playgroud)

这两个注册表都在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>(); …
Run Code Online (Sandbox Code Playgroud)

.net structuremap

6
推荐指数
1
解决办法
3017
查看次数

标签 统计

.net ×1

structuremap ×1