private List<ValidationResult> Validate(GardenRegistration gardenRegistration) { var context = new ValidationContext(gardenRegistration, serviceProvider: null, items: null); var results = new List<ValidationResult>(); Validator.TryValidateObject(gardenRegistration, context, results); return results; }
public GardenRegisterationResult RegisterGarden(GardenRegistration gardenRegistration) { var response = new GardenRegisterationResult(); var validationResults = Validate(gardenRegistration); if (validationResults.Any()) { response.ValidationResults = validationResults; return response; } //var city = CityRepository.ByKey(c => c.Id == gardenRegistration.CityId); //var county = CountyRepository.ByKey(c => c.Id == gardenRegistration.CountyId); var gardenSize = GardenSizeRepository.ByKey(gs => gs.Id == gardenRegistration.GardenSizeId); var gardenType = GardenTypeRepository.ByKey(gt => gt.Id == gardenRegistration.GardenTypeId); if (gardenSize != null && gardenType != null) { using (var transaction = new System.Transactions.TransactionScope()) { var garden = new Garden() { Address = gardenRegistration.Address, CityName = gardenRegistration.City, Email = gardenRegistration.EmailAddress, FirstName = gardenRegistration.FirstName, GardenName = gardenRegistration.GardenName, GardenSize = gardenSize, GardenType = gardenType, GroupName = gardenRegistration.GroupName, CountyName = gardenRegistration.County, LastName = gardenRegistration.LastName, ZipCode = gardenRegistration.Zipcode, CanBeContactedByNas = gardenRegistration.CanBeContactedByNas }; foreach (var gardenReason in gardenRegistration.GardenReasons.Select(gardenReasonId => GardenReasonRepository.ByKey(gr => gr.Id == gardenReasonId))) garden.GardenReasons.Add(gardenReason); foreach (var plantType in gardenRegistration.PlantTypes.Select(plantTypeId => PlantTypeRepository.ByKey(pt => pt.Id == plantTypeId))) garden.PlantTypes.Add(plantType); garden = GardenRepository.Add(garden); response.GardenId = garden.Id; response.WasSuccessful = true; transaction.Complete(); } } return response; }