为命名连接配置ConnectionFilter

时间:2013-03-25 20:48:19

标签: servicestack ormlite-servicestack

鉴于文档中的以下OrmLite配置,为每个命名连接配置ConnectionFilter的最佳方法是什么?

var dbFactory = new OrmLiteConnectionFactory(
    "Data Source=host;Initial Catalog=RobotsMaster;Integrated Security=SSPI",  //Connection String
    SqlServerDialect.Provider);

dbFactory.Run(db => db.CreateTable<MasterRecord>(overwrite:false));

NoOfShards.Times(i => {
    var namedShard = "robots-shard" + i;
    dbFactory.RegisterConnection(namedShard, 
        "~/App_Data/{0}.sqlite".Fmt(shardId).MapAbsolutePath(),                //Connection String
        SqliteDialect.Provider);

    dbFactory.OpenDbConnection(namedShard).Run(db => db.CreateTable<Robot>(overwrite:false));
});

目前,我正在使用它而不是RegisterConnection。

    OrmLiteConnectionFactory.NamedConnections[namedShard] = new OrmLiteConnectionFactory("~/App_Data/{0}.sqlite".Fmt(shardId).MapAbsolutePath(), true, SqliteDialect.Provider, true) {
        ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
    };

我们可以将RegisterConnection更改为

    public void RegisterConnection(string connectionKey, string connectionString, IOrmLiteDialectProvider dialectProvider, bool autoDisposeConnection = true)
    {
        RegisterConnection(connectionKey, new OrmLiteConnectionFactory(connectionString, autoDisposeConnection, dialectProvider, autoDisposeConnection));
    }

    public void RegisterConnection(string connectionKey, OrmLiteConnectionFactory ormLiteConnectionFactory)
    {
        NamedConnections[connectionKey] = ormLiteConnectionFactory;
    }

1 个答案:

答案 0 :(得分:3)

向ServiceStack.OrmLite提交补丁。 https://github.com/ServiceStack/ServiceStack.OrmLite/pull/212