GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - codeapologist/DataAbstractions.DapperParameters: Easily build Dapper dynamic parameters using a Fluent API

BranchesTags
Open more actions menu

Latest commit

 

History

16 Commits
16 Commits

Folders and files

NameName
Last commit message
Last commit date
src/DataAbstractions.DapperParameters
src/DataAbstractions.DapperParameters
 
 
tests/DataAbstractions.DapperParameters.Tests
tests/DataAbstractions.DapperParameters.Tests
 
 
.gitignore
.gitignore
 
 
DataAbstractions.DapperParameters.sln
DataAbstractions.DapperParameters.sln
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 

Repository files navigation

DataAbstractions.DapperParameters

A library for building Dapper dynamic parameters with a simple a fluent API.

Extension Methods

CreateParameters() extension method will turn each property into a dynamic parameter using the property name and value.

public class MyObject
{
    public int Id {get; set; }
    public string Name { get; set; }
    public DateTime ModifiedDate { get; set; }
}
var parameters = myObject.CreateParameters();

To customize parameters, use Parameterize() to add, remove, and replace with a fluent syntax.

var parameters = myObject.Parameterize()
                         .Add("NewId", myIdentifier)
                         .Remove(x => x.Name)
                         .Update(x => x.ModifiedDate, DateTime.Now)
                         .Create();

connection.Execute("myStoredProcedure", parameters, commandType: CommandType.StoredProcedure);                         

IParameterFactory Interface

An IParameterFactory interface is available for projects leveraging dependency injection. This interface has the same API method signatures as the extension methods above.

IParameterFactory parameterFactory = new ParameterFactory();

var parameters = parameterFactory.Parameterize(myObject)
                         .Add("NewId", myIdentifier)
                         .Remove(x => x.Name)
                         .Update(x => x.ModifiedDate, DateTime.Now)
                         .Create();

Add Your Own Fluent Methods

You can define your own fluent api with extension methods. The following example adds a LastModified parameter.

    public static class MyParameterBuilderExtensions
    {
        public static IParameterBuilder<T> AddLastModified<T>(this IParameterBuilder<T> parameterBuilder)
        {
            //Cast to ParameterBuilder<T>
            var builder = (ParameterBuilder<T>)parameterBuilder; 

            //"Add" tells the builder to add this parameter key and value
            builder.Add("LastModified", DateTime.Now);

            //Return the builder
            return builder;
        }
    }

Use the new fluent method and create the dynamic parameter:

IParameterFactory parameterFactory = new ParameterFactory();

var parameters = parameterFactory.Parameterize(myObject)
                         .AddLastModified()
                         .Create();

TODO:

  • Add Rename fluent method
  • Create fluent API for customizing and creating Table Valued Parameters
  • Document all the built-in fluent methods

About

Easily build Dapper dynamic parameters using a Fluent API

Resources

Readme
MIT license
Activity

Stars

2 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.