Skip to content
Scott Offen edited this page Sep 30, 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
  • DateTime
  • DateTimeOffset
  • TimeSpan
  • TimeZoneInfo
  • DataTable
  • IEnumerable<IDataRecord>

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

Table-Valued Parameters

If your model includes a DataTable or IEnumerable<IDataRecord> property, it will be automatically added as a Table-Valued Paramater using the built in extension method AsTableValuedParameter() provided by Dapper.

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.AsTableValuedParameter());

// 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, decorate 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; }

// OR
[Parakeet(propertyName: "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(direction: ParameterDirection.Output)]
public string SomeName { get; set; }

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

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

Additional Attribute Properties

You can also specify tableName (for table-valued parameters), dbType, size, precision and scale as optional named parameters on the attribute.

Removing Unused Properties and Ignoring Other Properties

You can indicate unused properties should not be included by passing a true value as the second parameter 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 but 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; }