3

All,

I have read up on the way SS uses Func to wire registrations. My current issue is that I am still not seeing how to call a specific instance from runtime.

What I would like to do is set up two different repository classes representing two different database systems, say SQLDB and MongoDB, both of which inherit from IDB, then be able to determine which database to use based on an app setting in the config file.

What I have right now in my Configure method is just

container.Register("SQLDB", new TestSQLDB("connectionName"));
container.Register("MongoDB", new TestMongoDB("mongoURL"));

If anyone can help me fill in the blanks I'd appreciate it. I already managed this with Ninject, but I would prefer not to have to add it if I don't need to.

Thanks, Bs

4

2 回答 2

3

通过具体类型(或如果您愿意,可以使用接口)注册:

container.Register<SQLDB>(new TestSQLDB("connectionName"));
container.Register<MongoDB>(new TestMongoDB("mongoURL"));

当你想要一个回来时:

var mySqlDB = container.Resolve<SQLDB>();
var myMongoDB = container.Resolve<MongoDB>();
于 2013-09-18T14:26:18.047 回答
2

感谢您的帮助加文。你让我走上正轨。这是有效的。

1)在配置中:

container.Register<ITest>("SQLDB", new TestSQLDB("xxxx"));
container.Register<ITest>("MongoDB", new TestMongoDB("mongo://127.0.0.1"));

2)在服务中:

IAppHost appHost = base.GetAppHost();
var repository = appHost.GetContainer().ResolveNamed<ITest>("MongoDB");
List<Test> testlist = repository.LoadAll();

这样我就可以用配置文件中的东西替换“MongoDB”,我不需要重新编译来更改数据源。

于 2013-09-18T16:49:18.207 回答