任何人都可以提供一个使用IoC(structureMap/Spring.Net)在开发中交换数据访问层中的连接字符串的示例& Production? (在C#如果可能的话)如何在Development db连接字符串和Production Db连接字符串设置中应用IoC
感谢
任何人都可以提供一个使用IoC(structureMap/Spring.Net)在开发中交换数据访问层中的连接字符串的示例& Production? (在C#如果可能的话)如何在Development db连接字符串和Production Db连接字符串设置中应用IoC
感谢
不知道Spring.Net但如何我通常做的ASP.Net,假设你有一个接受数据库连接字符串的DAL。
<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");
}
哥们我不会做,如果我是你
误入但如果你真的需要,你可能必须做这样的事情:(这可能甚至不工作)
<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>
然后有一个自定义AdoTemplate类
public class CustomAdoTemplate : Spring.Data.Generic.AdoTemplate {
public object DbProviders {
get;
set;
}
public override object DbProvider {
get {
return DbProviders[GetCurrentEnvironmentKey()];
}
}
}
为什么使用IoC?使用配置参数。 – Amy 2011-01-20 05:44:37
谢谢yodaj,我完全同意你的意见。但是有没有办法将IoC应用于此?如果是这样,我想知道它是如何完成的。 – 2011-01-20 06:32:52