Tiny ORM with support for multiple databases in the same project?
Are there any lig开发者_JAVA百科htweight ORMs (like PetaPoco) that have support for multiple databases in the same project?
It would be great if the ORM was typed and could generate all entities.
Dapper supports this fine out of the box.
public bool Equals(Identity other)
{
return
other != null &&
gridIndex == other.gridIndex &&
type == other.type &&
sql == other.sql &&
commandType == other.commandType &&
connectionString == other.connectionString &&
parametersType == other.parametersType;
}
The identity cache takes the connection string as part of the identity. Meaning you get excellent performance for all your queries, cause the materializer and param extractor is cached, but the caching is done per-db. The cache is also resilient to db schema changes.
Dapper works with POCOs and/or dynamics, you get to choose. Dapper.Contrib contains a few helpers that can make UPDATE
, INSERT
and so on, simpler.
Only caveat here is that it would not generate entities for you. You would need your own code to do that.
To give a basic example:
DbConnection db1 = ...
DbConnection db2 = ...
var dataFrom1 = db1.Query<Order>(sql, args);
var dataFrom2 = db2.Query<Order>(sql, args);
Because the origin is the connection, this is automatically database-centric (note that it will cope fine with different column orders etc between databases).
Or to put it another way: StackExchange is multi-tenancy, meaning we talk to lots of databases inside the same AppDomain
. StackExchange uses dapper, and dapper was fully designed to handle this scenario.
Disclamer I am a contributor on the dapper project, I imagine plenty other Micro ORMs support this
精彩评论