【发布时间】:2023-03-07 04:18:01
【问题描述】:
我有如下界面:
public interface IValidator
{
// Checks whether the selected roles are Valid based on Buisness rules for the
// specific EntityValidator
bool HasCompleteValidSelection(
ICollection<Role> availableRoles, ICollection<Role> selectedRoles);
//Checks whether the available roles are Valid for the specific entity
bool HasValidRoles(ICollection<Role> availableRolesList);
//Computes the Remaining Roles that needs to be selected to make it a Valid selection
ICollection<Role> GetRemainingRoles(
ICollection<Role> availableRoles, ICollection<Role> selectedRoles);
}
现在,我有一堆 EntityType,在枚举中提到:
public enum EntityType
{
Shop= 1,
SmallBuisness= 2,
Corporation = 3,
Firm = 4,
Partnership = 5,
Unknown = 0
}
上述所有实体类型都有其对应的验证器类,这些验证器类实现了 IValidator。
public class ShopValidator : IValidator
{
public bool HasCompleteValidSelection(
ICollection<Role> availableRoles, ICollection<Role> selectedRoles)
{ /*implementation */ }
public bool HasValidRoles(ICollection<Role> availableRolesList)
{ /*implementation */ }
public ICollection<Role> GetRemainingRoles(
ICollection<Role> availableRoles, ICollection<Role> selectedRoles)
{ /*implementation */ }
}
但令人担忧的是,一些验证器类具有完全相同的逻辑/代码。 我想到的是:
- 代替接口,创建抽象类并保持通用 代码在那里
- 具有不同实现的验证器类是 覆盖抽象类。
现在,我的问题是:
- 虽然上述工作正常,有没有更好的 更适合上述场景的方法/设计模式?
-
我正在使用如下所示的 Autofac,它工作正常,但是有没有 您可以预见的问题?
builder.RegisterType().As().Keyed(EntityType.Shop);
// 其他验证器类似。
【问题讨论】:
标签: c# design-patterns dependency-injection autofac