I was wondering if we want to check for adding new item if projectId exist in db, shell we somehow in AddItem
method to insert Guard.Agains.NotFound(???)
or not?
I'm asking because if create some entity:
public class Country : BaseEntity<int>, IAggregateRoot
{
public string Name { get; private set; }
private readonly List<District> _districts = new List<District>();
public IEnumerable<District> Districts => _districts.AsReadOnly();
public Country(string name)
{
Name = Guard.Against.NullOrEmpty(name, nameof(name));
}
public void AddDistrict(District newDistrict)
{
Guard.Against.Null(newDistrict, nameof(newDistrict));
Guard.Against.NegativeOrZero(newDistrict.CountryId, nameof(newDistrict.CountryId));
_districts.Add(newDistrict);
}
}
public class District : BaseEntity<int>, IAggregateRoot
{
public string Name { get; set; }
public int CountryId { get; set; }
public List<Municipality> Municipalities { get; set; }
}
How we can validate if countryId sent by request exist in DB ? Example if create integration test like :
[Fact]
public async Task AddDistrict()
{
var districtName = "District";
var countryRepository = GetCountryRepository();
var country = new Country("Country");
await countryRepository.AddAsync(country);
var district = new District
{
CountryId = 2,
Name = districtName
};
country.AddDistrict(district);
await countryRepository.UpdateAsync(country);
Assert.Equal(1, district.Id);
}
No matter with intenger value I put as CountryId test will pass till is not 0 or negative integer, but I want to check if that id of country entity exist in DB. Where would be best place to manage this check ? Regards,