// Maps a ViewModel to DataModel public static T MapToDataModel <T>(this BaseViewModel src) where T : class, BaseDataModel { var mapType = typeof(T); switch (mapType.Name) { case "BusinessItem": { var srcObj = new BusinessItemIModel(); srcObj.CopyPropertiesFrom(src); var mapObj = new BusinessItem { Id = srcObj.Id, ItemId = srcObj.ItemId, Item = new Item { Id = srcObj.ItemId, Name = srcObj.Name, Description = srcObj.Description, GroupId = srcObj.GroupId, Group = null }, Quantity = srcObj.Quantity, Prices = new List <Price> { new Price { Type = PriceType.Cost, Amount = srcObj.PriceCost, BusinessItemId = srcObj.Id, BusinessItem = null } }, DateOfProduction = DateTime.ParseExact(srcObj.DateOfProduction, "dd/MM/yyyy", new CultureInfo("en-US")), DateOfLastSold = DateTime.ParseExact(srcObj.DateOfLastSold, "dd/MM/yyyy", new CultureInfo("en-US")), Catalogues = null, }; var map = (T)Activator.CreateInstance(mapType); map.CopyPropertiesFrom(mapObj); return(map); } case "BusinessEntity": { var srcObj = new BusinessEntityIModel(); srcObj.CopyPropertiesFrom(src); var mapObj = new BusinessEntity { }; var map = (T)Activator.CreateInstance(mapType); map.CopyPropertiesFrom(mapObj); return(map); } default: throw new Exception($"No implementation to map {mapType.Name}!"); } }