例如我有这个类:
public class UnitOfWork : IUnitOfWork
{
private readonly ApplicationDbContext _context;
public IProductRepository Products { get; private set; }
public ICategoryRepository Categories { get; private set; }
public IPhotoRepository Photos { get; private set; }
public UnitOfWork(ApplicationDbContext context, IProductRepository productRepository,
ICategoryRepository categoryRepository, IPhotoRepository photoRepository)
{
_context = context;
Products = productRepository;
Categories = categoryRepository;
Photos = photoRepository;
}
public int Complete()
{
return _context.SaveChanges();
}
public Task<int> CompleteAsync()
{
return _context.SaveChangesAsync();
}
public void Dispose()
{
_context.Dispose();
}
public async …Run Code Online (Sandbox Code Playgroud) c# dependency-injection idisposable unit-of-work asp.net-core
我正在努力为一个类实现一个验证器,其中应该只设置一个属性。
假设我们有以下类:
public class SomeClass
{
public DateTime SomeDate {get; set;}
public IEnumerable<int> FirstOptionalProperty {get; set;}
public IEnumerable<int> SecondOptionalProperty {get; set;}
public IEnumerable<int> ThirdOptionalProperty {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
这个类有一个强制性属性 - SomeDate。其他属性是可选的,只能设置一个,例如如果FirstOptionalProperty设置了 -SecondOptionalProperty并且ThirdOptionalProperty应该为空,如果SecondOptionalProperty设置了 -FirstOptionalProperty并且ThirdOptionalProperty应该为空等等。
换句话说:如果设置了 IEnumerable 道具之一 - 其他 IEnumerables 应该为空。
关于为此类类实现验证器的任何提示/想法?我唯一想到的是编写When规则块,但是这种编写代码的方式很容易出错,而且结果看起来很丑陋。