我试图向 StructureMap 请求一个实例并立即传递参数,以配置实例(使用我传递的参数),但我不知道该怎么做!
这是代码,很容易理解我想要做什么。
class MyRegistry : Registry {
public MyRegistry() {
// Use a new Connection for each instance
For<ISQLRepositoryCommands>().Use(x => new SQLRepositoryCommands(DatabaseConnections.NewSqlConnection()));
// WHAT I Want to do is.. remove the line above of code and let only this.
For<ISQLRepositoryCommands>()
.Use(x => {
if( x.arguments != null ) {
// Specific connection passed by parameter
return new SQLRepositoryCommands( (SqlConnection) x.arguments[0]);
}
// default connection
return new SQLRepositoryCommands(DatabaseConnections.NewSqlConnection());
});
}
}
主要的
public static class StartUseStructureMap {
public static void Main() {
// SQLRepositoryCommands with default connection associated
var def = ObjectFactory.GetInstance<ISQLRepositoryCommands>();
// SQLRepositoryCommands with custom connection associated
var personalizedConnection = new SqlConnection("cstring");
var personalized = ObjectFactory.GetInstance<ISQLRepositoryCommands>(new Arguments[] { new Argument(personalizedConnection) };
}
}