AutoMapper:
(from automapper.org)
AutoMapper is a simple little library built to solve a deceptively complex problem - getting rid of code that mapped one object to another.
https://github.com/AutoMapper/AutoMapper
http://automapper.org/
Another essential open source library...
For Microsoft Visual Studio 2013, you can install Automapper from the nuget manager.
Amazing little code needed to convert one class into another.
Typically, DTOs deserialized from data source are often less then ideal to use display data in UI or databind.
Automapper saved me 4-5hrs today and I have my code working in under 30 mins.
public class LegoCategory : INotifyPropertyChanged // ReSharper restore InconsistentNaming { private bool _inUse; [Column] public Int32 CatID { get; set; } [Column] public string CatDesc { get; set; } [Column] public bool InUse { get { return _inUse; } set { if (value == _inUse) return; _inUse = value; OnPropertyChanged("InUse"); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } } public class LegoCategoryDto // ReSharper restore InconsistentNaming { [DataMember,Column] public object CatID { get; set; } [DataMember, Column] public string CatDesc { get; set; } [DataMember, Column] public object InUse { get; set; } } //Code fragment... //fetches data from SQLite => LegoCategoryDTO converts to LegoCategory using //AUTOMAPPER var cmd = new SQLiteCommand(sql); var list = cmd.Deserialize<LegoCategoryDto>(Database.SQLITE); Mapper.CreateMap<LegoCategoryDto, LegoCategory>(); return list.Select(Mapper.Map<LegoCategory>).ToList();
No comments:
Post a Comment