public async Task <SettlementResultsModel> GenerateSettlement(string settlementCode, IBuildingNameGenerator buildingNameGenerator) { var settlement = await _repo.GetSettlement(settlementCode); if (settlement == null) { throw new ArgumentException($"{settlementCode} is an invalid {nameof(settlementCode)}"); } var buildingNames = await buildingNameGenerator.ListBuildingTypes(); var results = CreateResultsModel(settlement); foreach (var settlementBuilding in settlement.Buildings) { var buildingType = buildingNames.FirstOrDefault(x => x.Id == settlementBuilding.BuildingTypeId); results.Buildings.Add(buildingType, new List <BuildingNameResultModel>()); var buildingList = results.Buildings[buildingType]; var numberToMake = _randomNumber.GetRandomInteger(settlementBuilding.MinBuildings, settlementBuilding.MaxBuildings); for (int i = 0; i < numberToMake; i++) { var building = await buildingNameGenerator.GenerateBuildingName(buildingType.Code); if (buildingList.Any(x => x.BuildingName == building.BuildingName)) { i--; } else { buildingList.Add(building); } } } return(results); }