OrmLite's goal is to provide a convenient, DRY, config-free, RDBMS-agnostic typed wrapper that retains a high affinity with SQL, exposing intuitive APIs that generate predictable SQL and maps cleanly to (DTO-friendly) disconnected POCO's. This approach makes easier to reason-about your data access making it obvious what SQL is getting executed at what time, whilst mitigating unexpected behavior, implicit N+1 queries and leaky data access prevalent in Heavy ORMs.
OrmLite was designed with a focus on the core objectives:
- Provide a set of light-weight C# extension methods around .NET's impl-agnostic
System.Data.*
interfaces - Map a POCO class 1:1 to an RDBMS table, cleanly by conventions, without any attributes required.
- Create/Drop DB Table schemas using nothing but POCO class definitions (IOTW a true code-first ORM)
- Simplicity - typed, wrist friendly API for common data access patterns.
- High performance - with support for indexes, text blobs, etc.
- Amongst the fastest Micro ORMs for .NET.
- Expressive power and flexibility - with access to IDbCommand and raw SQL
- Cross platform - supports multiple dbs (currently: Sql Server, Sqlite, MySql, PostgreSQL, Firebird) running on both .NET and Mono platforms.
Step1:
Step2:
namespace YourNameSpace
{
public interface IYourAppConnectionFactory : IDbConnectionFactory
{
}
public class YourAppConnectionFactory : OrmLiteConnectionFactory, IYourAppConnectionFactory
{
public YourAppConnectionFactory(string s) : base(s) { }
public YourAppConnectionFactory(string s, SqlServer2014OrmLiteDialectProvider provider) : base(s, provider) { }
}
}
If you're using an IOC you can register OrmLiteConnectionFactory
as a singleton, e.g: OrmLiteConfig.CommandTimeout = 360;
//Insert and Update unicode with serviceStack
OrmLiteConfig.DialectProvider = SqlServer2014Dialect.Provider;
OrmLiteConfig.DialectProvider.GetStringConverter().UseUnicode = true;
container.Register<IYourAppConnectionFactory>(c =>
new YourAppConnectionFactory(ConfigurationManager.ConnectionStrings["YourDBConnect"].ConnectionString, SqlServer2014OrmLiteDialectProvider.Instance);
Step 3: How to use
public class BusinessClass
{
private readonly IYourAppConnectionFactory _yourApConnectionFactory;
public BusinessClass(IYourAppConnectionFactory yourApConnectionFactory){
_yourApConnectionFactory = yourApConnectionFactory;
}
public void Create(){
using (var db = _yourApConnectionFactory.Open())
{
using (var transaction = db.OpenTransaction()){
db.Insert(new Poco { Id = 1, Name = "Seed Data"});
}
}
}
}
Source: https://github.com/ServiceStack/ServiceStack.OrmLite
No comments:
Write comments