c# – 将参数传递给方法绑定
发布时间:2020-12-15 05:36:50  所属栏目:百科  来源:网络整理 
            导读:我有一个非常简单的Ninject绑定: BindISessionFactory().ToMethod(x = { return Fluently.Configure() .Database(SQLiteConfiguration.Standard .UsingFile(CreateOrGetDataFile("somefile.db")).AdoNetBatchSize(128)) .Mappings( m = m.FluentMappings.Ad
                
                
                
            | 
                         我有一个非常简单的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();
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
