Code Monkey home page Code Monkey logo

postulate.lite's Introduction

Postulate.Lite ORM

Build status

Note: I am moving all development to the Postulate repo in order to target .NET Standard 2.0.

Postulate.Lite is a code-first ORM built around Dapper that performs CRUD operations on your model types. It's an evolution of my Postulate.Orm project that is

Postulate.Lite is not a Linq replacement. In my applications, I use inline SQL with Postulate.Lite's Query<T> type. Please see the wiki page on this.

Check out my Medium.com post, which has more background.

Nuget

  • SQL Server package: Postulate.Lite.SqlServer
  • MySql package: Postulate.Lite.MySql

How to Use

  • Create any number of model classes that correspond to your database tables. They can be POCO, but added functionality is available if you inherit from Postulate.Lite.Core.Record. The only design requirement for Postulate.Lite model classes is that either they have an [Identity] attribute that defines the primary key property, or they have a property called Id with no particular attribute decoration. You can also use the [PrimaryKey] attribute on select properties to define an explicit primary key -- of either one or multiple properties.

  • Use the [References] attribute to define foreign keys on properties.

  • For Sql Server, Postulate.Lite supports int, Guid, and long identity types. MySql currently supports int. When creating your model classes, decide on an identity type and be consistent across all your model classes.

  • Open your IDbConnection object in whatever way is appropriate for your application, normally within a using block.

  • Use any of the Postulate.Lite Crud extension methods of IDbConnection: Find, FindWhere, Save, Insert, Update, Delete, Exists, and ExistsWhere. They all accept a TModel generic argument corresponding to your model class. In the SQL Server package, there are three different namespaces with a static ConnectionExtensions class that provides the crud methods: Postulate.Lite.SqlServer.IntKey, LongKey, and GuidKey, so use the namespace appropriate to the identity type you chose above.

  • All of the Crud methods accept an IUser optional argument you can use to pass the current user name and access to the user's local time. This argument takes effect if your model class is based on Record (see above), which offers a number of overrides for checking permissions and executing row-level events, looking up foreign keys, among other things.

Code-first Migration

There are two ways to merge your model class code to a database:

  • Use the free command line tool available here. This tool merges only from C# to SQL Server, not from database to database.

  • Use my commercial GUI app SQL Model Merge. There's more product info and a demo video at that link.

Examples

A simple find using the Find method:

using (var cn = GetConnection())
{
  var e = cn.Find<Employee>(2322);
  Console.WriteLine(e.FirstName);
}

Find using criteria with the FindWhere method:

using (var cn = GetConnection())
{
  var e = cn.FindWhere(new { OrganizationId = 12, Number = 3988 });
  Console.WriteLine(e.FirstName);
}

Create and save a record with the Save method.

using (var cn = GetConnection())
{
  var e = new Employee()
  {
    FirstName = "Thomas",
    LastName = "Whoever",
    HireDate = new DateTime(2012, 1, 1)
  };
  cn.Save<Employee>(e);
  Console.WriteLine(e.Id.ToString());
}

Extending Postulate.Lite

To implement Postulate.Lite for a particular database, inherit from abstract class CommandProvider<TKey> and implement its various abstract methods that generate SQL for Crud actions. The TKey generic argument specifies the identity (primary key) type used with your model classes. The default MySQL implementation assumes an int primary key type. To implement long or Guid primary key types, you'd need to derive a new class from CommandProvider with your desired key type.

postulate.lite's People

Contributors

adamfoneil avatar

Watchers

 avatar

postulate.lite's Issues

Remove obsolete schema merge functionality

Originally, Postulate.Lite was going to have schema sync/merge capability more or less ported from Postulate.Orm. This fell off the roadmap after starting the SchemaSync project and its enclosing SQL Model Merge tool.

The merge functionality from Postulate.Lite therefore needs to be removed. We might keep the CreateTable method however since it's used in some integration tests.

Make it so SqlServerCommandProvider accepts TKey argument

This way, the SQL syntax and key type isn't coupled -- there's no reason for them to be.
Then add extension classes from there that target different key type flavors: SqlServerIntProvider, SqlServerGuidProvider, SqlServerLongProvider, etc.

Identity attribute position argument

Create optional argument on [Identity] attribute
so you can place identity column at beginning or end of class.
This is for Becky, really, so she can put Id property first

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.