Skip to content
Scott Offen edited this page Sep 11, 2019 · 10 revisions

Default Usage

Give the following model:

public class MyModel
{
    public Guid Id { get; set; } = Guid.NewGuid();

    public DateTimeOffset DateOccured { get; set; } = DateTimeOffset.Now;

    public string Description { get; set; }
}

The following code snippets are equivalent:

var model = new MyModel{ Description = "Describe this thing" };

// Using Dapper Method 1
var dp1 = new DynamicParameters();
dp1.Add("@Id", model.Id);
dp1.Add("@DateOccured", model.DateOccured);
dp1.Add("@Description", model.Description);

// Using Dapper Method 2
var dp2 = new DynamicParameters(model)

// Using Parakeet (returns a DynamicParameters object)
var dp3 = Parakeet<MyModel>.Generate(model);

In this simple use case, there is not much difference. Parakeet will add all public instance properties on the model to the DynamicParameters instance - as long as it deems the property "eligible".

Automatic Eligibility

Automatically eligible property types include:

  • all primitive types
  • enum
  • string
  • decimal
  • Guid
  • DataTable
  • DateTime
  • DateTimeOffset
  • TimeSpan
  • TimeZoneInfo

Includes the nullable versions of the types listed above, as applicable.

DataTable and TableValueParameter

If your model includes a DataTable property, it will be automatically added as a TableValueParamater.

public class MyModel
{
   public Guid Id { get; set; } = Guid.NewGuid();

   public DateTimeOffset DateOccured { get; set; } = DateTimeOffset.Now;

   public string Description { get; set; }

   public DataTable MyDataTable { get; set;} = new DataTable();
}

var model = new MyModel();

// Using Dapper
var dp1 = new DynamicParameters();
dp1.Add("@Id", model.Id);
dp1.Add("@DateOccured", model.DateOccured);
dp1.Add("@Description", model.Description);
dp1.Add("@MyDataTable", model.MyDataTable.AsTableValueParameter());

// Using Parakeet (returns a DynamicParameters object)
var dp3 = Parakeet<MyModel>.Generate(model);

The Parakeet Attribute

If you want Parakeet to include properties that don't meet the default eligibility criteria, decoreate the property with the [Parakeet] attribute and it will be inlcuded.

You can also add modifiers to the attribute that will change how the property is added to the DynamicParameters object. These modifiers correspond to the arguments to the DynamicParameters.Add() method.

Use a different Name

Give the parameter a different name than the property.

[Parakeet("DifferentName")]
public string SomeName { get; set; }

When using Parakeet, the above property will be added like this:

dp.Add("@DifferentName", model.SomeName);

Change Parameter Direction

Parameter direction is, by default ParameterDirection.Input, unless otherwise specified in the attribute.

[Parakeet(ParameterDirection.Output)]
public string SomeName { get; set; }

When using Parakeet, the above property will be added like this:

dp.Add("@SomeName", model.SomeName, ParameterDirection.Output);

Additional Attribute Properties

You can also specify the DbType, Size, Precision and Scale properties on the attribute.

Removing Unused Properties and Ignoring Other Properties

You can indicate unused properties should not be included, you can pass a true value to the Generate method. This is equivalent to setting the RemoveUnused property of the returned DynamicParameters object to true.

var pb = Parakeet<MyModel>.Generate(model, true);

If you want Parakeet to ignore a property that would otherwise be included (but can't use the RemoveUnused property because, for example, the parameters are needed by a stored procedure by don't show up in the query command) you can mark that property as ignored by using the [ParakeetIgnore] attribute, and it will not be added to the DynamicParameters.

[ParakeetIgnore]
public string DoNotSendMe { get; set; }
Clone this wiki locally