RepositoryHelpers 3.0.0
dotnet add package RepositoryHelpers --version 3.0.0
NuGet\Install-Package RepositoryHelpers -Version 3.0.0
<PackageReference Include="RepositoryHelpers" Version="3.0.0" />
paket add RepositoryHelpers --version 3.0.0
#r "nuget: RepositoryHelpers, 3.0.0"
// Install RepositoryHelpers as a Cake Addin #addin nuget:?package=RepositoryHelpers&version=3.0.0 // Install RepositoryHelpers as a Cake Tool #tool nuget:?package=RepositoryHelpers&version=3.0.0
RepositoryHelpers
Extensions for HttpClient and Custom Repository based on dapper
This is the component, works on .NET Core and.NET Framework
Info
Code Quality | Build | Nuget | Contributors |
---|---|---|---|
Build History
Platform Support
RepositoryHelpers is a .NET Standard 2.0 library.
Database/Dapper Extensions
Use the connection class to define the type of database and connection string
var connection = new Connection()
{
Database = RepositoryHelpers.Utils.DataBaseType.SqlServer, //RepositoryHelpers.Utils.DataBaseType.Oracle
ConnectionString = "Your string"
};
Create a CustomRepository of the type of object you want to return
var Repository = new CustomRepository<User>(conecction);
Mapping with Attributes:
[DapperIgnore] // Property will be ignored in select, insert and update
public string InternalControl { get; set; }
[PrimaryKey] // Primary key
public int MyCustomId { get; set; }
[PrimaryKey]
[Identity] //Indicates that the primary key has some identity, sequence or auto increment
public int MyBdIdIndentity { get; set; }
//You can optionally map the name of the Database table that refers to the entity
[Table("Product")]
public class Products
{
public int Id { get; set; }
}
Mapping with FluentMapper:
Install and use the Dapper.FluentMap.Dommel package to map your entities by creating the specific classes inherited from DommelEntityMap:
public class ProductMap : DommelEntityMap<Product>
{
public ProductMap()
{
Map(p => p.Id).IsKey().IsIdentity();
Map(p => p.Category).Ignore();
}
}
You can define the name of the table that will be mapped
public class ProductMap : DommelEntityMap<Product>
{
public ProductMap()
{
ToTable("Product");
Map(p => p.Id).IsKey().IsIdentity();
Map(p => p.Category).Ignore();
}
}
After that, you must configure Dapper.FluentMap.Dommel in RepositoryHelpers:
Mapper.Initialize(c =>
{
c.AddMap(new ProductMap());
});
Get Data:
To get results just use the Get method. can be syncronous or asynchronous
//Get All Users
var usersAsync = await Repository.GetAsync();
var users = Repository.Get();
//Get User by Id
var userAsync = await Repository.GetByIdAsync(1);
var user = Repository.GetById(1);
//Get by CustomQuery with parameters
var customQuery = "Select name from user where login = @userLogin";
var parameters = new Dictionary<string, object> { { "userLogin", "bertuzzi" } };
var resultASync = await Repository.GetAsync(customQuery, parameters);
var result = Repository.Get(customQuery, parameters);
//Get by CustomQuery without parameters
var customQuery = "Select * from user";
var resultASync = await Repository.GetAsync(customQuery);
var result = Repository.Get(customQuery);
//Get by multi-mapping custom query with 2 input types
var customQuery = "Select * from user inner join category on user.categoryId = category.Id where login = @userLogin";
var user = Repository.Get<User, Category, User>(
customQuery,
map: (user, category) =>
{
user.Category = category;
return user;
});
//Get by multi-mapping custom query with 2 input types (When the field that we should split and read the second object is different from "Id")
var customQuery = "Select * from user inner join state on user.stateCode = state.Code where login = @userLogin";
var user = Repository.Get<User, State, User>(
customQuery,
map: (user, state) =>
{
user.State = state;
return user;
},
parameters,
splitOn: "Code"
);
//Get by multi-mapping custom query with an arbitrary number of input types
var customQuery = "Select * from user inner join category on user.categoryId = category.Id where login = @userLogin";
var user = Repository.Get(
customQuery,
new[] { typeof(User), typeof(Category) },
map: (types) =>
{
var user = (types[0] as User);
user.Category = (types[1] as Category);
return user;
});
Insert Data :
user identity parameter to return the id if your insert needs
Repository.Insert(NewUser, true);
Update data
Repository.Update(updateUser);
Repository.UpdateAsync(updateUser);
Delete data
Repository.Delete(1);
Repository.DeleteAsync(1);
You can use ADO if you need
//Return DataSet
var customQuery = "Select name from user where login = @userLogin";
var parameters = new Dictionary<string, object> { { "userLogin", "bertuzzi" } };
Repository.GetDataSet(customQuery, parameters);
//ExecuteQuery
Repository.ExecuteQueryAsync();
Repository.ExecuteQuery();
//ExecuteScalar
Repository.ExecuteScalarAsync();
Repository.ExecuteScalar();
//ExecuteProcedure
Repository.ExecuteProcedureAsync();
Repository.ExecuteProcedure();
CustomTransaction is possible to use transaction
CustomTransaction customTransaction = new CustomTransaction(YourConnection);
customTransaction.BeginTransaction();
customTransaction.CommitTransaction();
customTransaction.RollbackTransaction();
//Sample
Repository.ExecuteQuery("yourquery", parameters, customTransaction);
DapperIgnore : if you want some property of your object to be ignored by Dapper, when inserting or updating, just use the attribute. PrimaryKey : Define your primary key. It is used for queries, updates, and deletes. IdentityIgnore: Determines that the field has identity, autoincrement ... Warns the repository to ignore it that the database will manage the field
*TIP Create a ConnectionHelper for BaseRepository and BaseTransaction to declare the connection only once :
public sealed class ConnectionHelper
{
static ConnectionHelper _instance;
public static ConnectionHelper Instance
{
get { return _instance ?? (_instance = new ConnectionHelper()); }
}
private ConnectionHelper()
{
Connection = new Connection()
{
Database = RepositoryHelpers.Utils.DataBaseType.SqlServer,
ConnectionString = "YourString"
};
}
public Connection Connection { get; }
}
public class BaseRepository<T>
{
protected readonly CustomRepository<T> Repository;
protected BaseRepository()
{
Repository = new CustomRepository<T>(ConnectionHelper.Instance.Connection);
}
}
public class BaseTransaction : CustomTransaction
{
public BaseTransaction() :
base(ConnectionHelper.Instance.Connection)
{
}
}
LiteDB Extensions
coming soon ..
HttpClient Extensions
Extensions to make using HttpClient easy.
To enable and use Follow the doc : https://github.com/TBertuzzi/HttpExtension
Samples coming soon ..
Special Thanks to project contributors
Special Thanks users who reported bugs and helped improve the package :
- Thiago Vieira
- Luis Paulo Souza
- Alexandre Harich
The RepositoryHelpers was developed by Thiago Bertuzzi under the MIT license.
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.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Dapper.Contrib (>= 2.0.78)
- Dapper.FluentMap.Dommel (>= 2.0.0)
- HttpExtension (>= 5.0.0)
- LiteDB (>= 5.0.19)
- Newtonsoft.Json (>= 13.0.3)
- Npgsql (>= 8.0.2)
- System.Data.OracleClient (>= 1.0.8)
- System.Data.SqlClient (>= 4.8.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.0.0 | 214 | 4/18/2024 |
2.8.0 | 5,046 | 8/4/2021 |
2.7.0 | 566 | 3/16/2021 |
2.6.2 | 815 | 1/18/2021 |
2.6.0 | 1,369 | 11/26/2020 |
2.5.0 | 483 | 11/9/2020 |
2.4.1 | 1,266 | 11/3/2020 |
2.4.0 | 787 | 7/20/2020 |
2.3.0 | 607 | 5/30/2020 |
2.2.0 | 557 | 5/6/2020 |
2.1.0 | 639 | 2/4/2020 |
2.0.0 | 594 | 1/9/2020 |
1.5.0 | 620 | 9/12/2019 |
1.4.2 | 609 | 6/26/2019 |
1.4.0 | 576 | 5/29/2019 |
1.3.1 | 645 | 4/25/2019 |
1.2.1 | 1,618 | 1/18/2019 |
1.1.0 | 700 | 1/15/2019 |
Update Packages