I have this simple entity.
public class Foo
{
public int Id { get; private set; }
public string Name{ get; private set; }
public string Code { get; private set; }
private Foo() { }
public Foo(string Name, string Code)
{
GuardClauses.IsNullOrWhiteSpace(Name,nameof(Name), "cannot be null or empty");
GuardClauses.IsNullOrWhiteSpace(Code, nameof(Code), "cannot be null or empty");
this.Nom = Nom;
this.Code = Code;
}
}
In my DbContext I have this code that ensure the Code is unique from a persistence point of view.
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<User>()
.HasIndex(u => u.Email)
.IsUnique();
}
I want in my service class the addNewFoo method ensure that for all the Foos I have in my Application the property code is unique.
I try as much as I can to respect persistence ignorance principle, but I'm not as skilled as I wish to do that.
First Is that Role of a Builder to Determine if Code is Unique? Second In my validation layer I can of course determine if there already exist one foo with same Code that the foo I try to add. But the approach is nor thread safe neither transactional. The fact is I don't want to wait the moment I add my foo too have a SqlException, just to know it cannot be done.
What is the best approach to ensure unicity in my application with the Fail Fast principle in mind.
Aucun commentaire:
Enregistrer un commentaire