Map a Uri field using Dapper

dapper

Question

What is the cleanest way to map a string column to a Uri property using Dapper?

Here's the cleanest I've been able to come up with so far (using the ITypeMap functionality):

Query:

SELECT * FROM TableWithAStringAddressColumn

POCO:

public class MyPoco
{   
    [ColumnSetter("DapperAddress")]
    public Uri Address { get; set; }
    private string DapperAddress { set { this.Address = new Uri(value); } }
}

Extensions:

partial class SqlMapper
{
    public static void InitializeTypeMaps()
    {
        SqlMapper.SetTypeMap(
            typeof(MyPoco),
            new CustomPropertyTypeMap(typeof(MyPoco), SqlMapper.CustomSetterMapper));

        // call out every other class that needs this kind of mapping
    }

    public static Func<Type, string, PropertyInfo> CustomSetterMapper =
        (type, columnName) =>
        {
            PropertyInfo prop = type
                .GetProperties()
                .FirstOrDefault(p => string.Equals(columnName, p.Name, StringComparison.OrdinalIgnoreCase));

            if (prop != null)
            {
                // find out if we need to use a different setter
                ColumnSetterAttribute setterAttribute = prop.GetCustomAttributes(false).OfType<ColumnSetterAttribute>().LastOrDefault();
                if (setterAttribute != null)
                {
                    PropertyInfo setterProp = type
                        .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                        .FirstOrDefault(p => string.Equals(setterAttribute.Setter, p.Name, StringComparison.OrdinalIgnoreCase));
                    if (setterProp == null)
                    {
                        throw new InvalidOperationException(string.Format("Setter property misconfigured (Property={0}, Setter={1})", prop.Name, setterAttribute.Setter));
                    }
                    else
                    {
                        prop = setterProp;
                    }
                }
            }
            return prop;
        };
}

Custom Attribute:

public class ColumnSetterAttribute : Attribute
{
    public string Setter { get; set; }

    public ColumnSetterAttribute(string setter)
    {
        this.Setter = setter;
    }
}

[edit] I'm looking for a solution I can use without needing to call out all columns in all my queries (I'd like to find a solution where I can use SELECT *).

Accepted Answer

Seems like a lot of work...

Wouldn't this be ok?

public class MyPoco
{
    private string _uriMapper;

    public Uri SomeUri
    {
        get { return new Uri(_uriMapper); }
    }

    public string Mapper { set { _uriMapper = value; } }
}

Edit:

public class UriContainer
{
    private string _uriMapper;

    public string UriMapper { set { _uriMapper = value; } }

    public int Id { get; set; }

    public Uri SomeUri { get {return new Uri(_uriMapper);} }
}



public class DbTests
{
    [Test]
    public void Can_Get_A_Uri()
    {
        using (var c = new SqlConnection("hello"))
        {
            c.Open();

            var uri = c.Query<UriContainer>("select *, someuri as urimapper from uris where id = 3").Single();

            Console.WriteLine(uri.SomeUri);
        }
    }
}


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why