arvenyon / NickX.TinyORM

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TinyORM - Simple, lightweight .NET MicroORM for SQL-Server

Features

  • Fluent or Attribute Mapping between classes and SQL tables
  • Generic repository pattern as persistence layer, already implemented
  • Attach to existing databases
  • Database creation based on your mapping & definitions
  • Events for CRUD operations which you can hook into

Dependencies

Installation

You can either clone this repository, build it & use the compiled dlls or simply install the NuGet Package:

PM> Install-Package NickX.TinyORM

Super Quick Start

  1. Get your SQL ConnectionString
var conString = "Database=myDatabase;Server=myServer,1433;User Id=myUser;Password=mySuperSecretPassword123"
  1. Define your Mapping via Fluent API
var fluentMapping = new FluentMapping()
    .MapTable<Foo>("tblFoos")
		.MapColumn(c => c.Id, allowsNull: false, length: default, DefaultValues.AutoIncrement)
		.MapColumn(c => c.Firstname, "AnotherNameForYourColumn")
		.MapColumn(c => c.DateCreate, allowsNull: false, length: default, DefaultValues.Timestamp)
		.SetPrimaryKey(c => c.Id)
		.PackUp()
	.MapTable<Bar>()
		.MapColumn(c => c.Id, allowsNull: false, length: default, DefaultValues.AutoIncrement)
		.MapColumn(c => c.FooId, allowsNull: false, length: default, DefaultValues.None);
		.SetPrimaryKey(c => c.Id)
		.AddForeignKey<Foo>(b => b.FooId, f => f.Id)
		.PackUp();
  1. Initialize a Connection Factory
var conFactory = new SqlConnectionFactory(
	connectionString: conString,
	mapping: fluentMapping,
	createDb: true
);
  1. Grab your repositories and start manipulating data
var foos = new SqlRepository<Foo>(conFactory);

var newFoo = new Foo()
{
	Firstname = "Biggus",
	Lastname = "Dickus"
};

int insertedKey = (int)foos.Insert(newFoo);

Simplified Overview

CRUD Operations

All well known CRUD functionality is packed into repositories. You can instantiate a repository for any type that you've mapped via your FluentMapping:

var foos = new SqlRepository<Foo>(sqlConnectionFactory);

Select

var foos = new SqlRepository<Foo>(sqlConnectionFactory);

// Get All
var allFoos = foos.All();

// Get an object by Key
var fooByKey = foos.Single(14);

// Get a single object by condition
var condition = foos.CreateQueryConditionBuilder()
	.Start(f => f.Firstname, QueryOperators.Equals, "Biggus")
	.And(f => f.Lastname, QueryOperators.Equals, "Dickus");
var fooByCondition = foos.Single(condition);

// Get multiple by condition
var condition = foos.CreateQueryConditionBuilder()
	.Start(f => f.DateCreate, QueryOperators.LowerThanOrEquals, DateTime.Now.AddYears(-1))
	.Or(f => f.Status, QueryOperators.Equals, Status.Inactive);
var foosByCondition = foos.Multiple(condition);
	

Insert

var foos = new SqlRepository<Foo>(sqlConnectionFactory);

var newFoo = new Foo()
{
	Firstname = "Incontinentia",
	Lastname = "Buttocks"
};
int insertedId = (int)foos.Insert(newFoo);

Update

var foos = new SqlRepository<Foo>(sqlConnectionFactory);

var existingFoo = foos.Single(14);
existingFoo.Firstname = "AnotherFirstname";

foos.Update(existingFoo);

Delete

var foos = new SqlRepository<Foo>(sqlConnectionFactory);

var condition = foos.CreateQueryConditionBuilder()
	.Start(f => f.Status, QueryOperators.Equals, Status.Inactive);
var toDelete = foos.Single(condition);

foos.Delete(toDelete);

Exists

var foos = new SqlRepository<Foo>(sqlConnectionFactory);

var condition = foos.CreateQueryConditionBuilder()
	.Start(f => f.Status, QueryOperators.Equals, Status.Inactive);
	
if (foos.Exists(condition))
{
	// go on and do stuff
}

Query Condition

Queries often depend on conditions. In order to not need to use any strong typed SQL statements anywhere, repositories provide a QueryConditionBuilder for their mapped Type.

Usage

At this point you can define your conditions via (again) a Fluent API.

var fooRepository = new SqlRepository<Foo>(sqlConnectionFactory);

var condition = foos.CreateQueryConditionBuilder()
	.Start(f => f.Status, QueryOperators.Equals, Status.Inactive);

QueryOperators

Following QueryOperators are available.

public enum QueryOperators
{
	Equals,
	NotEquals,
	Contains,
	NotContains,
	StartsWith,
	NotStartsWith,
	EndsWith,
	NotEndsWith,
	GreaterThan,
	LessThan,
	GreaterThanOrEqual,
	LessThanOrEqual
}

Events

Insert, Update & Delete operations all have events attached to them. They're provided by the repository.

Inserted & Updated will always be the Type you've gotten the repository for

var foos = new SqlRepository<Foo>(conFactory);

// after insert
foos.OnInsert += (sender, inserted) =>
{
	Console.WriteLine("Inserted Foo:");
	Console.WriteLine(inserted);
}

// after update
foos.OnUpdate += (sender, inserted, deleted) => 
{
	Console.WriteLine("Updated Foo:");
	Console.WriteLine(inserted);
	Console.WriteLine("Old Foo:");
	Console.WriteLine(deleted);
}

// after delete
foos.OnDelete += (sender, deleted) =>
{
	Console.WriteLine("Deleted Foo:");
	Console.WriteLine(deleted);
}

Issues, Bugs & Requests

Please consider creating an issue if you encounter any problems, bugs, errors or inconveniences. I will happily continue supporting this project.

Disclaimer

This is a personal project which initially started out as a project for learning purposes. Take this information for whatever you want it to. :)
Also, despite the fact that GitHub sais, this repository would consist of 98% JavaScript, nothing, I repeat, not a single ducking thing here, is written in JavaScript. It's all C#.

About

License:GNU General Public License v3.0


Languages

Language:JavaScript 97.9%Language:C# 1.4%Language:CSS 0.7%