2013-08-17 15 views
5

Dapper.net Uzantıları kullanıyorum ve tam bir özel eşleştirici yazmadan belirli özellikleri yok saymak istiyorum. Aşağıda ClassMapper'da görebileceğiniz gibi, gerçekten tek yapmak istediğim çok fazla kod var. Bunu başarmanın en iyi yolu ne olurdu?Dapper.net Uzantıları kullanarak bir sınıf özelliğini nasıl yok sayarsınız?

Burada verilen yanıtı beğendim https://stackoverflow.com/a/14649356 ancak 'Yaz' tanımlandığı ad alanını bulamıyorum. Dapper.Contrib projenin parçası -

public class Photo : CRUD, EntityElement 
{ 
    public Int32 PhotoId { get; set; } 
    public Guid ObjectKey { get; set; } 
    public Int16 Width { get; set; } 
    public Int16 Height { get; set; } 
    public EntityObjectStatus ObjectStatus { get; set; } 
    public PhotoObjectType PhotoType { get; set; } 
    public PhotoFormat2 ImageFormat { get; set; } 
    public Int32 CategoryId { get; set; } 

    public int SomePropertyIDontCareAbout { get; set; } 
} 


public class CustomMapper : DapperExtensions.Mapper.ClassMapper<Photo> 
{ 
    public CustomMapper() 
    { 
     Map(x => x.PhotoId).Column("PhotoId").Key(KeyType.Identity); 
     Map(x => x.ObjectKey).Column("ObjectKey"); 
     Map(x => x.Width).Column("Width"); 
     Map(x => x.Height).Column("Height"); 
     Map(x => x.ObjectStatus).Column("ObjectStatus"); 
     Map(x => x.PhotoType).Column("PhotoType"); 
     Map(x => x.ImageFormat).Column("ImageFormat"); 
     Map(x => x.CategoryId).Column("CategoryId"); 

     Map(f => f.SomePropertyIDontCareAbout).Ignore(); 
    } 
} 

cevap

4

WriteAttribute sınıf Dapper.Contrib.Extensions ad alanında yer almaktadır. Eğer sadece sizin ClassMapper kurucuları içindedir AutoMap(); diyoruz, Person.cs görebileceğiniz gibi Nuget aracılığıyla, paket "Dapper.Contrib"

+2

Nuget ISN'de Dapper.Contrib paketi': Sonra tekrar

[Computed] public int SomePropertyIDontCareAbout { get; set; } 

Peter Ritchie'nin cevabı büyük olasılıkla daha fazla yer-üstünde ile t görünüyor lates kodu ile güncellendi. Güncellenmesi için sahibiyle iletişim kurdum. –

+1

... ve şimdi güncellendi ve umarım gelecekte güncellenecektir. –

4

adında olduğu ekleyebilir. Örneğin,

public class CustomMapper : DapperExtensions.Mapper.ClassMapper<Photo> 
{ 
    public CustomMapper() 
    { 
     Map(x => x.PhotoId).Key(KeyType.Identity); 
     Map(f => f.SomePropertyIDontCareAbout).Ignore(); 
     AutoMap(); 
    } 
} 
+0

Bu işe yarayabilirdi. Base.AutoMap() işlevini daha iyi yapması ya da 'CustomMapper' sınıfının 'sealed' olarak işaretlenmesi daha iyidir. Teşekkürler –

3

Özelliği [Computed] ile deşifre edebilirsiniz ve özellik ekleme sırasında yoksayılır. Semantik mükemmel olmayabilir ama işi yapmak için görünüyor:

[WriteAttribute(false)] 
public int SomePropertyIDontCareAbout { get; set; } 
İlgili konular