Skip to content

Whiteknight/CastIron

Repository files navigation

CastIron

A micro-ORM with the Query Object Pattern for C#

Overview

CastIron is a micro-ORM which focuses on power, flexibility, modularity, usability, and the Query Object pattern. These are the central design goals, the relative prioritization of which separates it from other Micro-ORMs.

CastIron is written in a provider-agnostic way using abstractions from System.Data instead of concrete classes. The CastIron project offers providers for multiple database types:

  1. CastIron.Sql targets Microsoft SQL Server
  2. CastIron.Sqlite targets SQLite.
  3. CastIron.Postgres targets PostgreSQL

Get Started

Start by creating an ISqlRunner:

var runner = RunnerFactory.Create(connectionString);

At this point you will need to create a class to hold your SQL queries or command. Implement one of the following interfaces:

  1. ISqlQuery<T> for a simple string of SQL which returns result sets
  2. ISqlQueryRawCommand<T> you get the raw IDbCommand to which you can add query text and parameters, and get back query results.
  3. ISqlQueryRawConnection<T> you get the raw IDbConnection on which you can do any operation you want
  4. ISqlCommand for a simple string of SQL which does not return a result set
  5. ISqlCommandRaw you get the raw IDbCommand to which you can add command text and parameters. This will return no results.
  6. ISqlCommandRaw<T> like ISqlCommandRaw except this class will have an opportunity to read query results and output parameters.

As an example of a simple query:

public class MyTestQuery : ISqlQuerySimple<MyObject>
{
    public string GetSql() {
        return "SELECT TOP 1 Col1, Col2 FROM dbo.MyTable;";
    }

    public MyObject Read(IDataResults result) {
        return result.AsEnumerable<MyObject>().SingleOrDefault();
    }
}

And it's use:

var runner = RunnerFactory.Create(connectionString);
var result = runner.Query(new MyTestQuery());

The IDataResults object contains the raw IDataReader object, any output parameters, and utilities for automatically mapping rows to objects and values. Experimenting with the possibilities of IDataReader is key to finding happiness and success with CastIron.

Please see the existing unit tests for additional, in-depth examples of using CastIron.Sql.

Status

CastIron.Sql is stable, in active development, and used in production deployments. v2.0.0 is released to nuget and is preferred over all previous versions.

Providers are available for SQL Server, SQLite and PostgreSQL. Providers for other databases such as MySql are in development.

About

Modular micro-ORM with a focus on the Query Object pattern

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages