mardi 2 octobre 2018

Any better approach to tackle this simple business use case?

I have the following interface:

 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);
    }

Now, I have bunch of EntityTypes,mentioned in the enum:

public enum EntityType
    {        
        Shop= 1,
        SmallBuisness= 2,
        Corporation = 3,
        Firm = 4,
        Partnership = 5,
        Unknown = 0
    }

All the above Entity Types have their corresponding validator classes which implements the 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}
    }

But the concern is some of the validator classes are having exact same logic/code. What I have thought of is:

  1. Instead of Interface, created abstract class and kept the common code there
  2. Validator classes, which has different implementation,are overriding the abstract class.

Now, my questions are:

  1. Although the above is working fine, is there any better approach/design pattern more suitable for the above scenario?
  2. I am using Autofac like below, its working fine, but is there any issue that you can foresee?

    builder.RegisterType < ShopValidator > ().As < IValidator >().Keyed(EntityType.Shop);

    //other validators similarly.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire