2

使用 Dapper 将字符串列映射到 Uri 属性的最简洁方法是什么?

这是迄今为止我能想到的最干净的(使用ITypeMap 功能):

询问:

SELECT * FROM TableWithAStringAddressColumn

POCO:

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

扩展:

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;
        };
}

自定义属性:

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

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

[编辑]我正在寻找一个我可以使用的解决方案,而无需在我的所有查询中调用所有列(我想找到一个我可以使用的解决方案SELECT *)。

4

1 回答 1

2

好像工作量很大...

这不就好了吗?

public class MyPoco
{
    private string _uriMapper;

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

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

编辑:

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);
        }
    }
}
于 2012-12-27T21:37:13.817 回答