5

私は非常に単純な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");

どうすればそれを達成できますか?

4

2 に答える 2

3

IParameter呼び出し時に追加のを指定して、次のGet<T>ようにデータベース名を登録できます。

kernel.Get<ISessionFactory>(new Parameter("dbName", "somefile.db", false);

次に、 (sysntaxは少し冗長です)Parametersを介して提供されたコレクションにアクセスできます。IContext

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();
于 2012-11-17T19:18:10.157 に答える
0

これがNinjectModuleになったので、NinjectModule.Kernelプロパティを使用できます。

Bind<ISessionFactory>().ToMethod(x =>
    {
        return Fluently.Configure()
            .Database(SQLiteConfiguration.Standard
                .UsingFile(CreateOrGetDataFile(Kernel.Get("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();
于 2012-11-17T17:34:54.627 に答える