Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

📦 Extensions for HttpClient and Custom Repository based on dapper

NotificationsYou must be signed in to change notification settings

TBertuzzi/RepositoryHelpers

Repository files navigation

Extensions for HttpClient and Custom Repository based on dapper

This is the component, works on .NET Core and.NET Framework

Info

Code QualityBuildNugetContributors
Codacy BadgePublish package on NuGetNuGetGitHub contributors

Build History

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

varconnection=newConnection(){Database=RepositoryHelpers.Utils.DataBaseType.SqlServer,//RepositoryHelpers.Utils.DataBaseType.OracleConnectionString="Your string"};

Create a CustomRepository of the type of object you want to return

varRepository=newCustomRepository<User>(conecction);

Mapping with Attributes:

[DapperIgnore]// Property will be ignored in select, insert and updatepublicstringInternalControl{get;set;}[PrimaryKey]// Primary keypublicintMyCustomId{get;set;}[PrimaryKey][Identity]//Indicates that the primary key has some identity, sequence or auto incrementpublicintMyBdIdIndentity{get;set;}//You can optionally map the name of the Database table that refers to the entity[Table("Product")]publicclassProducts{publicintId{get;set;}}

Mapping with FluentMapper:

Install and use theDapper.FluentMap.Dommel package to map your entities by creating the specific classes inherited fromDommelEntityMap:

publicclassProductMap:DommelEntityMap<Product>{publicProductMap(){Map(p=>p.Id).IsKey().IsIdentity();Map(p=>p.Category).Ignore();}}

You can define the name of the table that will be mapped

publicclassProductMap:DommelEntityMap<Product>{publicProductMap(){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(newProductMap());});

Get Data:

To get results just use the Get method. can be syncronous or asynchronous

//Get All UsersvarusersAsync=awaitRepository.GetAsync();varusers=Repository.Get();//Get User by IdvaruserAsync=awaitRepository.GetByIdAsync(1);varuser=Repository.GetById(1);//Get by CustomQuery with parametersvarcustomQuery="Select name from user where login = @userLogin";varparameters=newDictionary<string,object>{{"userLogin","bertuzzi"}};varresultASync=awaitRepository.GetAsync(customQuery,parameters);varresult=Repository.Get(customQuery,parameters);//Get by CustomQuery without parametersvarcustomQuery="Select * from user";varresultASync=awaitRepository.GetAsync(customQuery);varresult=Repository.Get(customQuery);//Get by multi-mapping custom query with 2 input typesvarcustomQuery="Select * from user inner join category on user.categoryId = category.Id where login = @userLogin";varuser=Repository.Get<User,Category,User>(customQuery,map:(user,category)=>{user.Category=category;returnuser;});//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")varcustomQuery="Select * from user inner join state on user.stateCode = state.Code where login = @userLogin";varuser=Repository.Get<User,State,User>(customQuery,map:(user,state)=>{user.State=state;returnuser;},parameters,splitOn:"Code");//Get by multi-mapping custom query with an arbitrary number of input typesvarcustomQuery="Select * from user inner join category on user.categoryId = category.Id where login = @userLogin";varuser=Repository.Get(customQuery,new[]{typeof(User),typeof(Category)},map:(types)=>{varuser=(types[0]asUser);user.Category=(types[1]asCategory);returnuser;});

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 DataSetvarcustomQuery="Select name from user where login = @userLogin";varparameters=newDictionary<string,object>{{"userLogin","bertuzzi"}};Repository.GetDataSet(customQuery,parameters);//ExecuteQueryRepository.ExecuteQueryAsync();Repository.ExecuteQuery();//ExecuteScalarRepository.ExecuteScalarAsync();Repository.ExecuteScalar();//ExecuteProcedureRepository.ExecuteProcedureAsync();Repository.ExecuteProcedure();

CustomTransaction is possible to use transaction

CustomTransactioncustomTransaction=newCustomTransaction(YourConnection);customTransaction.BeginTransaction();customTransaction.CommitTransaction();customTransaction.RollbackTransaction();//SampleRepository.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 :

publicsealedclassConnectionHelper{staticConnectionHelper_instance;publicstaticConnectionHelperInstance{get{return_instance??(_instance=newConnectionHelper());}}privateConnectionHelper(){Connection=newConnection(){Database=RepositoryHelpers.Utils.DataBaseType.SqlServer,ConnectionString="YourString"};}publicConnectionConnection{get;}}publicclassBaseRepository<T>{protectedreadonlyCustomRepository<T>Repository;protectedBaseRepository(){Repository=newCustomRepository<T>(ConnectionHelper.Instance.Connection);}}publicclassBaseTransaction:CustomTransaction{publicBaseTransaction():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 byThiago Bertuzzi under theMIT license.

Packages

No packages published

Contributors3

  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2026 Movatter.jp