开发者

How to apply IoC in Development db connection string and Production Db connection string settings

Can anyone kindly provide me an example of using IoC (structureMap / Spring.Net) for swapping connection string in Data Access Layer in Deve开发者_StackOverflow中文版lopment & Production? (In C# if possible)

Thanks


Don't know about Spring.Net but this is how I usually do it in ASP.Net, assuming that you have a DAL that accept a db connection string.

<connectionStrings>
    <add name="Development" connectionString="Enlist=false;MultipleActiveResultSets=True;Data Source=MYPC\SQLEXPRESS;Initial Catalog=Development;Integrated Security=True" providerName="System.Data.SqlClient"/>
    <add name="Production" connectionString="Enlist=false;MultipleActiveResultSets=True;Data Source=MYPC\SQLEXPRESS;Initial Catalog=Production;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

public class MySession : ISession
{
    public MySession(string connectionName)
    {
        // ...
    }
}

ObjectFactory.Initialize(
    x =>
    {
         x.For<ISession>()
          .Use<MySession>().Ctor<string>("connectionName").Is("Development");
          //.Use<MySession>().Ctor<string>("connectionName").Is("Production");
    }


Dude i wouldn't do that if i was you

  1. When you deploy, your connection strings for all your environments will go out to all your environments (security issue)
  2. You are straying from the standard implementation, which means pain in the long run

But if you really needed to, you would probably have to do something like this: (this might not even work)

<db:provider id="PRODDbProvider" provider="SqlServer-2.0" connectionString="whateveritis" />

<db:provider id="DEVDbProvider" provider="SqlServer-2.0" connectionString="whateveritis" />

<object id="genericAdoTemplate" type="CustomAdoTemplate">
<property name="DbProviders">
  <dictionary>
    <entry key="PROD" value="PRODDbProvider" />
    <entry key="DEV" value="DEVDbProvider" />
  </dictionary>
</property>
</object>

Then have a custom AdoTemplate

public class CustomAdoTemplate : Spring.Data.Generic.AdoTemplate {

    public object DbProviders {
        get;
        set;
    }

    public override object DbProvider {
        get {
            return DbProviders[GetCurrentEnvironmentKey()];
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜