Link Search Menu Expand Document

PropertyValueAttribute


The base class for all attributes that set properties on a DbParameter object.

Attribute

Example usage:

public class Person
{
    public int Id { get; set; }

    [PropertyValue(typeof(DbParameter), nameof(DbParameter.ParameterName), "CompleteName")]
    public string Name { get; set; }
}

Fluent Mapping

To configure via FluentMapper:

FluentMapper
    .Entity<Person>()
    .PropertyValueAttributes(e => e.Name,
        new PropertyValueAttribute(typeof(DbParameter), nameof(DbParameter.ParameterName), "CompleteName"));

Multiple attributes can be mapped at once:

FluentMapper
    .Entity<Person>()
    .PropertyValueAttributes(e => e.Name, new []
    {
        new PropertyValueAttribute(typeof(DbParameter), nameof(DbParameter.ParameterName), "CompleteName")
        new PropertyValueAttribute(typeof(DbParameter), nameof(DbParameter.DbType), DbType.NVarChar)
    });

Retrieval

Retrieve the list of mapped attributes via PropertyValueAttributeCache:

var attributes = PropertyValueAttributeCache.Get<Person>(e => e.Name);

Or via PropertyValueAttributeMapper:

var attributes = PropertyValueAttributeMapper.Get<Person>(e => e.Name);

We strongly recommend using PropertyValueAttributeCache for maximum performance.

Implementation

To create a custom attribute, inherit this class:

public class CustomizedNameAttribute : PropertyValueAttribute
{
    public CustomizedNameAttribute(string parameterName)
        : base(typeof(DbParameter), nameof(DbParameter.ParameterName), parameterName)
    { }
}

The custom attribute can then be applied to a class:

public class Person
{
    public int Id { get; set; }

    [CustomizedName("CompleteName")]
    public string Name { get; set; }
}

Or via FluentMapper:

FluentMapper
    .Entity<Person>()
    .PropertyValueAttributes(e => e.Name,
        new [] { new CustomizedNameAttribute("CompleteName" })

The implementation is dynamic — any property of IDbDataParameter can be targeted, regardless of the data provider (SQL Server, PostgreSQL, MySQL, or SQLite).