public virtual void Create(TEntity entity) { if (entity == null) { throw new ArgumentNullException(); } DomainModelValidator <TDbContext> .Validate(entity, _dbContext, ValidationType.OnCreate); _dbContext.Set <TEntity>().Add(entity); }
public virtual void Update(TEntity entity) { if (entity == null) { throw new ArgumentNullException(); } DomainModelValidator <TDbContext> .Validate(entity, _dbContext, ValidationType.OnUpdate); _dbContext.Set <TEntity>().Attach(entity); _dbContext.Entry <TEntity>(entity).State = EntityState.Modified; }
static void InitValidator() { DomainModelValidator <DataSource> .Configure(cfg => { cfg.ConfigureOnCreate(ruleSet => { ruleSet.UsingExceptionApproach() .AddRule <ClassUnderValidation>((c, ds) => ds.Data[0].Property == c.Property) //.ThrowingException(new Exception("The first is the same")) .Including((c, ds) => ds.Data[0].Property > c.Property) .ThrowingException(new Exception("bla")); ruleSet.UsingNotificationApproach() .AddRule <Other>((c, ds) => ds.Data[0].Property == c.Property) .NotifiedAs(new Notification() { Message = "1" }) .Including((c, ds) => ds.Data[1].Property > c.Property) .NotifiedAs(new Notification() { Message = "2" }) .WithValidationExceptionMessage("Failed Other"); }); cfg.ConfigureOnDelete(ruleSet => { ruleSet.UsingExceptionApproach() .AddRule <ClassUnderValidation>((c, ds) => ds.Data[0].Property != c.Property) .ThrowingException(new Exception("DELETE The first is the same")) .Including((c, ds) => ds.Data[0].Property > c.Property) .ThrowingException(new Exception("DELETE bla")); ruleSet.UsingNotificationApproach() .AddRule <Other>((c, ds) => ds.Data[0].Property != c.Property) .NotifiedAs(new Notification() { Message = "DELETE 1" }) .Including((c, ds) => ds.Data[1].Property > c.Property) .NotifiedAs(new Notification() { Message = "DELETE 2" }) .WithValidationExceptionMessage("DELETE Failed Other"); }); }); }
public virtual void Delete(TEntity entity) { if (entity == null) { throw new ArgumentNullException(); } DomainModelValidator <TDbContext> .Validate(entity, _dbContext, ValidationType.OnDelete); if (_dbContext.Entry <TEntity>(entity).State == EntityState.Detached) { _dbContext.Set <TEntity>().Attach(entity); } _dbContext.Set <TEntity>().Remove(entity); }
static void Main(string[] args) { DataSource dataSource = InitDataSource(); InitValidator(); ClassUnderValidation test = new ClassUnderValidation(1); Other other = new Other(0); //DomainModelValidator<DataSource>.Configure(cfg => { }); //DomainModelValidator<string>.Validate(test, "", ValidationType.OnCreate); try { DomainModelValidator <DataSource> .Validate(other, dataSource, ValidationType.OnCreate); } catch (Exception ex) { int i = 10; //throw; } Console.ReadLine(); }