You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Inserts/Updates/Deletes are automatically generated by Dapper FastCRUD (class should be decorated with attributes for keys/autoincrement columns)
Supports FastCRUD bulk update, bulk delete, and async methods.
Repositories can be extended with custom Queries and custom Commands (allows/promotes CQRS separation)
Queries can be defined manually (raw sql) or using Dapper FastCRUD syntax
Dynamic Queries (dynamic number of conditions) can be built using DapperQueryBuilder
There are Read-only Connection Wrappers and Read-only Repositories, so it's easy to use read-replicas (or multiple databases)
Support for ADO.NET transactions
Support for mocking Queries and Commands
Sample Insert/Update/Delete (Generic Repository - this uses Dapper FastCRUD):
varconn=new ReadWriteDbConnection(new System.Data.SqlClient.SqlConnection(connectionString));// Get a IReadWriteRepository<TEntity> which offers some helpers to Query and Write our table:varrepo= conn.GetReadWriteRepository<ContactType>();varcontactType= repo.QueryAll().First();// Updating a record
contactType.ModifiedDate = DateTime.Now;
repo.Update(contactType);// Adding a new recordvarnewContactType=new ContactType(){Name="NewType",ModifiedDate= DateTime.Now };
repo.Insert(newContactType);// FastCRUD will automatically update the auto-generated columns back (identity or guid)// Deleting a record
repo.Delete(newContactType);
[Table("ContactType", Schema ="Person")]publicclassContactType{[Key]// if column is part of primary key[DatabaseGenerated(DatabaseGeneratedOption.Identity)]// if column is auto-incrementpublicintContactTypeId{get;set;}publicDateTimeModifiedDate{get;set;}publicstringName{get;set;}}
Sample Dynamic Queries:
varconn=new ReadDbConnection(new System.Data.SqlClient.SqlConnection(connectionString));// Get a IReadRepository<TEntity> which offers some helpers to Query our table:varrepo= conn.GetReadRepository<Person>();// Custom Query (pure Dapper)varpeople= repo.Query("SELECT * FROM Person.Person WHERE PersonType = @personType ",new{ personType ="EM"});// DapperQueryBuilder allows to dynamically append conditions using string interpolation (but injection-safe)stringtype="EM";stringsearch="%Sales%";vardynamicQuery= repo.QueryBuilder();// if not specified query is initialized with "SELECT * FROM tablename"
dynamicQuery.Where($"PersonType = {type}");
dynamicQuery.Where($"ModifiedDate >= {DateTime.Now.AddDays(-1)}");
dynamicQuery.Where($"Name LIKE {search}");// Result is SELECT * FROM [Person].[Person] WHERE PersonType = @p0 AND ModifiedDate >= @p1 AND Name LIKE @p2varpeople= dynamicQuery.Query();
Extending Repositories (adding custom Queries and Commands) using Inheritance:
publicclassPersonRepository:ReadWriteDbRepository<Person>{publicPersonRepository(IReadWriteDbConnectiondb):base(db){}publicvirtualIEnumerable<Person>QueryRecentEmployees(){returnthis.Query("SELECT TOP 10 * FROM [Person].[Person] WHERE [PersonType]='EM' ORDER BY [ModifiedDate] DESC");}publicvirtualvoidUpdateCustomers(){this.Execute("UPDATE [Person].[Person] SET [FirstName]='Rick' WHERE [PersonType]='EM' ");}}publicvoidSample(){// Registers that GetReadWriteRepository<Person>() should return a derived type PersonRepository
ReadWriteDbConnection.RegisterRepositoryType<Person,PersonRepository>();varconn=new ReadWriteDbConnection(new System.Data.SqlClient.SqlConnection(connectionString));// we know exactly what subtype to expect, so we can just cast.varrepo=(PersonRepository) conn.GetReadWriteRepository<Person>();
repo.UpdateCustomers();varrecentEmployees= repo.QueryRecentEmployees();}
There are also sample scripts which extract the schema of an existing SQL Server database and generate POCO classes (with attributes [Key], [DatabaseGenerated(DatabaseGeneratedOption.Identity)], etc.) based on the schema.
The text was updated successfully, but these errors were encountered:
Hi @denniscpolley , I'm glad you liked. Would you mind asking this question in Harbin issues? I'd like to keep the technical discussion there, so it can benefit other users. Thanks!
Repository owner
locked and limited conversation to collaborators
Mar 3, 2022
Harbin.DataAccess implements Generic Repositories (Generic Repository Pattern) using "raw" Dapper, Dapper.FastCRUD, and DapperQueryBuilder.
Sample Insert/Update/Delete (Generic Repository - this uses Dapper FastCRUD):
Sample Dynamic Queries:
Extending Repositories (adding custom Queries and Commands) using Inheritance:
Full documentation here.
There are also sample scripts which extract the schema of an existing SQL Server database and generate POCO classes (with attributes
[Key]
,[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
, etc.) based on the schema.The text was updated successfully, but these errors were encountered: