Formula.SimpleRepo
1.0.0
See the version list below for details.
dotnet add package Formula.SimpleRepo --version 1.0.0
NuGet\Install-Package Formula.SimpleRepo -Version 1.0.0
<PackageReference Include="Formula.SimpleRepo" Version="1.0.0" />
paket add Formula.SimpleRepo --version 1.0.0
#r "nuget: Formula.SimpleRepo, 1.0.0"
// Install Formula.SimpleRepo as a Cake Addin #addin nuget:?package=Formula.SimpleRepo&version=1.0.0 // Install Formula.SimpleRepo as a Cake Tool #tool nuget:?package=Formula.SimpleRepo&version=1.0.0
Formula.SimpleRepo
Easy repositories for .Net built on Dapper.
Creating a repository
Step 1 - Create a model
The model represents a single record mapping between a table or view on your datasource and a POCO (plain old c# object) for use within your application.
Annotate your model with the the connection to use from your appSettings, the Table or view to use.
Various other attributes can be used to provide additional mapping assistance (such as Key, Column, GUID). See Dapper.SimpleCRUD for details
using System;
using Dapper;
using Formula.SimpleRepo;
namespace MyApi.Data.Models
{
[ConnectionDetails("DefaultConnection")]
[Table ("Todos")]
public class Todo
{
[Key]
public int Id { get; set; }
public String Details { get; set; }
public Boolean Completed { get; set; }
public int? CategoryId { get; set; }
}
}
Step 2 - Create a Repository
The repository provides simple CRUD operations ( provided by Dapper.SimpleCRUD ), simple constrainable operations (query by JSON), as well as a single place to wrap business concepts into data fetch / store operations by custom function you provide.
The simplest implementation of a repository to take advantage of simple CRUD and constrainables can be implemented as follows.
using System;
using Microsoft.Extensions.Configuration;
using Formula.SimpleRepo;
using MyApi.Data.Models;
namespace MyApi.Data.Repositories
{
[Repo]
public class TodoRepository : RepositoryBase<Todo, Todo>
{
public TodoRepository(IConfiguration config) : base (config)
{
}
}
}
(Optional) Step 3 - Expose via API
The Formula.SimpleAPI project provides utilities to expose your repository as a RESTful API.
Registering Repositories
Repositories can be registered into the depencey injection system by implementing a couple steps. In the ConfigureServices section of Startup.cs make sure to make a call to AddRepositories. Failing to do so will result in controllers depending on these respositories bing unable to resolve service for these repository types.
using Formula.SimpleRepo;
services.AddRepositories();
All repositories decorated with the [Repo] attribute will be injected.
Custom Contraints
A constraint is anything you want to be able to expose querying for resources by. By default, you can simply provide the POCO model as the constrainable definition, however, you can also provide custom / dynamic fields to allow your resource models to be constrained by.
Example..
Suppose we wanted to allow our todo list to be able to be queried by a particular keyword found in the details of the todo.
We can create a class extending the Todo model defining a new constraint called DetailsLike. We first must define this new constraint (by extending Contraint), and we must implement the Bind method that handles building the query for values.
public class DetailsLike : Constraint
{
public override Dictionary<String, Object> Bind(Dapper.SqlBuilder builder)
{
var parameters = new Dictionary<String, Object>();
builder.Where("UPPER(Details) like @DetailsLike");
parameters.Add("DetailsLike", "%" + this.Value.ToString().ToUpper() + "%");
return parameters;
}
}
Since we want to allow constraint binding against all of the current properties on our model, we can extend our Todo model and add an aditional field called DetailsLike.
public class TodoConstraints : Todo
{
public DetailsLike DetailsLike { get; set; }
}
We can now pass our custom constraints (which include our dynamic custom constraint DetailsLike) to our repository as the second template supplied to the RepositoryBase.
public class TodoRepository : RepositoryBase<Todo, TodoConstraints>
Packages / Projects Used
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Dapper (>= 2.0.30)
- Dapper.SimpleCRUD (>= 2.2.0)
- Dapper.SqlBuilder (>= 2.0.30)
- Microsoft.Extensions.Configuration (>= 3.1.0)
- Microsoft.Extensions.Configuration.Binder (>= 3.1.0)
- Microsoft.Extensions.DependencyInjection (>= 3.1.0)
- Newtonsoft.Json (>= 12.0.3)
- System.Data.SqlClient (>= 4.8.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Formula.SimpleRepo:
Package | Downloads |
---|---|
Formula.SimpleAPI
Easy API's for .Net |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.9.0-beta | 104 | 8/15/2024 |
1.8.1 | 130 | 8/16/2024 |
1.8.0 | 125 | 3/27/2024 |
1.6.7 | 4,236 | 3/15/2024 |
1.6.6 | 976 | 3/8/2024 |
1.6.5 | 16,848 | 7/20/2023 |
1.6.4 | 11,557 | 2/24/2023 |
1.6.3 | 4,441 | 1/23/2023 |
1.6.1 | 1,078 | 1/17/2023 |
1.6.0 | 34,900 | 8/22/2022 |
1.1.4 | 3,121 | 7/27/2022 |
1.1.3 | 13,633 | 10/25/2021 |
1.1.2 | 364 | 10/22/2021 |
1.1.1 | 548 | 9/28/2021 |
1.0.7 | 26,945 | 4/10/2021 |
1.0.6 | 564 | 3/15/2021 |
1.0.5 | 469 | 3/4/2021 |
1.0.4 | 428 | 2/25/2021 |
1.0.3 | 3,250 | 8/26/2020 |
1.0.2 | 956 | 5/23/2020 |
1.0.1 | 656 | 4/3/2020 |
1.0.0 | 761 | 2/22/2020 |