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 に答える