Dapper.NET Type Handlers

From WikiOD

Remarks[edit | edit source]

Type Handlers allow database types to be converted to .Net custom types.

Installing a TypeHandler[edit | edit source]

The above type handler can be installed into SqlMapper using the AddTypeHandler method.

SqlMapper.AddTypeHandler<IHtmlString>(new IHtmlStringTypeHandler());

Type inference allows you to omit the generic type parameter:

SqlMapper.AddTypeHandler(new IHtmlStringTypeHandler());

There's also a two-argument overload which takes an explicit Type argument:

SqlMapper.AddTypeHandler(typeof(IHtmlString), new IHtmlStringTypeHandler());

Converting varchar to IHtmlString[edit | edit source]

public class IHtmlStringTypeHandler : SqlMapper.TypeHandler<IHtmlString>
    public override void SetValue(
        IDbDataParameter parameter, 
        IHtmlString value)
        parameter.DbType = DbType.String;
        parameter.Value = value?.ToHtmlString();

    public override IHtmlString Parse(object value)
        return MvcHtmlString.Create(value?.ToString());