我有一个非常简单的Ninject绑定:
Bind<ISessionFactory>().ToMethod(x => { return Fluently.Configure() .Database(sqliteConfiguration.Standard .UsingFile(CreateOrGetDataFile("somefile.db")).AdoNetBatchSize(128)) .Mappings( m => m.FluentMappings.AddFromAssembly(Assembly.Load("Sauron.Core")) .Conventions.Add(PrimaryKey.Name.Is(p => "Id"),ForeignKey.EndsWith("Id"))) .BuildSessionFactory(); }).InSingletonScope();
我需要的是用参数替换“somefile.db”.类似的东西
kernel.Get<ISessionFactory>("somefile.db");
我如何实现这一目标?
解决方法
调用Get< T>时,您可以提供其他IParameters.所以你可以这样注册你的数据库名称:
kernel.Get<ISessionFactory>(new Parameter("dbName","somefile.db",false);
然后你可以通过IContext访问提供的Parameters集合(sysntax有点冗长):
kernel.Bind<ISessionFactory>().ToMethod(x => { var parameter = x.Parameters.SingleOrDefault(p => p.Name == "dbName"); var dbName = "someDefault.db"; if (parameter != null) { dbName = (string) parameter.GetValue(x,x.Request.Target); } return Fluently.Configure() .Database(sqliteConfiguration.Standard .UsingFile(CreateOrGetDataFile(dbName))) //... .BuildSessionFactory(); }).InSingletonScope();