Last active
April 20, 2020 17:08
-
-
Save vkhorikov/72f5dc4038373bd86259 to your computer and use it in GitHub Desktop.
Representing data as code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Industry : Entity | |
{ | |
public const string CarsIndustry = "Cars"; | |
public const string PharmacyIndustry = "Pharmacy"; | |
public const string MediaIndustry = "Media"; | |
public string Name { get; private set; } | |
} | |
public class Customer : Entity | |
{ | |
public Industry Industry { get; private set; } | |
public EmailCampaign EmailCampaign { get; private set; } | |
public Customer(Industry industry) | |
{ | |
UpdateIndustry(industry); | |
} | |
public void UpdateIndustry(Industry industry) | |
{ | |
EmailCampaign = GetEmailCampaign(industry); | |
Industry = industry; | |
} | |
private EmailCampaign GetEmailCampaign(Industry industry) | |
{ | |
if (industry.Name == Industry.CarsIndustry) | |
return EmailCampaign.LatestCarModels; | |
if (industry.Name == Industry.PharmacyIndustry) | |
return EmailCampaign.PharmacyTips; | |
if (industry.Name == Industry.MediaIndustry) | |
return EmailCampaign.MediaNews; | |
throw new ArgumentException(); | |
} | |
} | |
public enum EmailCampaign | |
{ | |
LatestCarModels, | |
PharmacyTips, | |
MediaNews | |
} | |
public class CustomerController | |
{ | |
public void CreatePharmacy(string name) | |
{ | |
Industry industry = _industryRepository.GetByName(Industry.PharmacyIndustry); | |
var customer = new Customer(industry); | |
_customerRepository.Save(customer); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Industry : Entity | |
{ | |
public static readonly Industry Cars = new Industry(1, "Cars"); | |
public static readonly Industry Pharmacy = new Industry(2, "Pharmacy"); | |
public static readonly Industry Media = new Industry(3, "Media"); | |
public string Name { get; private set; } | |
private Industry(int id, string name) | |
{ | |
Id = id; | |
Name = name; | |
} | |
} | |
public class Customer : Entity | |
{ | |
public Industry Industry { get; private set; } | |
public EmailCampaign EmailCampaign { get; private set; } | |
public void UpdateIndustry(Industry industry) | |
{ | |
EmailCampaign = GetEmailCampaign(industry); | |
Industry = industry; | |
} | |
private EmailCampaign GetEmailCampaign(Industry industry) | |
{ | |
if (industry == Industry.Cars) | |
return EmailCampaign.LatestCarModels; | |
if (industry == Industry.Pharmacy) | |
return EmailCampaign.PharmacyTips; | |
if (industry == Industry.Media) | |
return EmailCampaign.MediaNews; | |
throw new ArgumentException(); | |
} | |
} | |
public class CustomerController | |
{ | |
public void CreatePharmacy(string name) | |
{ | |
var customer = new Customer(Industry.Pharmacy); | |
_customerReposoitory.Save(customer); | |
} | |
} | |
[Fact] | |
public void Updating_industry_updates_email_campaign() | |
{ | |
var customer = new Customer(Industry.Pharmacy); | |
customer.UpdateIndustry(Industry.Cars); | |
Assert.Equal(EmailCampaign.PharmacyTips, customer.EmailCampaign); | |
} | |
public class IndustryTests | |
{ | |
[Fact] | |
public void All_requires_values_are_in_DB() | |
{ | |
Verify(1, Industry.Cars); | |
Verify(2, Industry.Pharmacy); | |
Verify(3, Industry.Media); | |
} | |
private void Verify(int industryId, Industry hardCodedInstustry) | |
{ | |
using (var unitOfWork = new UnitOfWork()) | |
{ | |
var repository = new IndustryRepository(unitOfWork); | |
Industry industry = repository.GetById(industryId); | |
Assert.Equal(industry.Id, hardCodedInstustry.Id); | |
Assert.Equal(industry.Name, hardCodedInstustry.Name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment