- 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
- .NET 5
- FastMember >=1.5.0
- System.Data.SqlClient >=4.8.3
- System.Linq.Expressions >=4.3.0
- Humanizer >= 2.13.14
You can either clone this repository, build it & use the compiled dlls or simply install the NuGet Package:
PM> Install-Package NickX.TinyORM
- Get your SQL ConnectionString
var conString = "Database=myDatabase;Server=myServer,1433;User Id=myUser;Password=mySuperSecretPassword123"
- 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();
- Initialize a Connection Factory
var conFactory = new SqlConnectionFactory(
connectionString: conString,
mapping: fluentMapping,
createDb: true
);
- 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);
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);
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);
var foos = new SqlRepository<Foo>(sqlConnectionFactory);
var newFoo = new Foo()
{
Firstname = "Incontinentia",
Lastname = "Buttocks"
};
int insertedId = (int)foos.Insert(newFoo);
var foos = new SqlRepository<Foo>(sqlConnectionFactory);
var existingFoo = foos.Single(14);
existingFoo.Firstname = "AnotherFirstname";
foos.Update(existingFoo);
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);
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
}
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.
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);
Following QueryOperators are available.
public enum QueryOperators
{
Equals,
NotEquals,
Contains,
NotContains,
StartsWith,
NotStartsWith,
EndsWith,
NotEndsWith,
GreaterThan,
LessThan,
GreaterThanOrEqual,
LessThanOrEqual
}
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);
}
Please consider creating an issue if you encounter any problems, bugs, errors or inconveniences. I will happily continue supporting this project.
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#.