public static TownBuildingDto ToTownBuildingDto(this IMappable building) { var buildingDto = new TownBuildingDto { Id = building.Id, TownId = building.TownId, XCoordinate = building.XCoordinate, YCoordinate = building.YCoordinate, Name = building.Name, BuildingType = building.GetType().Name }; if (building is IOwnable) { buildingDto.OwnerId = ((IOwnable)building).OwnerId; buildingDto.IsForSale = ((IOwnable)building).IsForSale; buildingDto.Price = ((IOwnable)building).Price; } if (building is WritableEntity) { buildingDto.ModifiedDate = ((WritableEntity)building).ModifiedDate; buildingDto.CreatedDate = ((WritableEntity)building).CreatedDate; } return(buildingDto); }
/// <summary> /// If <paramref name="service"/> doesn't implement <typeparamref name="T"/>, an exception is thrown. /// </summary> /// <param name="service">The service that must implement <typeparamref name="T"/>.</param> /// <typeparam name="T">The type that <paramref name="service"/> must implement.</typeparam> /// <returns></returns> /// <exception cref="FulcrumNotImplementedException">Thrown if <paramref name="service"/> doesn't implement <typeparamref name="T"/>.</exception> public static T GetImplementationOrThrow <T>(IMappable service) where T : IMappable { if (service is T implemented) { return(implemented); } throw new FulcrumNotImplementedException($"The service {service.GetType()} does not implement {typeof(T).Name}"); }
public static IMappable Map(this IMappable sourceMap, IMappable destMap) { destMap.GetType().GetFields().ToList().ForEach( f => { f.SetValue(destMap, sourceMap.GetType().GetField(f.Name)?.GetValue(sourceMap)); } ); destMap.GetType().GetProperties().ToList().ForEach( p => { p.SetValue(destMap, sourceMap.GetType().GetProperty(p.Name)?.GetValue(sourceMap)); } ); return destMap; }