Generic Interface inheriting Non-Generic One C#

前端 未结 3 1368
无人及你
无人及你 2020-12-09 16:18

This is class design question.

I have main abstract class

public abstract class AbstractBlockRule
{
    public long Id{get;set;}
    public abstract         


        
3条回答
  •  感情败类
    2020-12-09 16:50

    Interfaces are contracts that need to be followed by the entity that implements the contract.

    You have created two contract with the same name IRestriction

    As far as I can see, what you are basically may need is a flag for classes that can be restricted, which should implement the IRestriction non-generic interface.

    The second interface seems to be restrictable objects that also contain a limit property. Hence the definition of the second IRestriction interface can be ILimitRestriction or whatever name suits your business needs.

    Hence ILimitRestriction can inherit from IRestriction which would mark classes inheriting ILimitRestriction still objects of IRestriction

    public abstract class AbstractBlockRule
    {
        public long Id{get;set;}
        public abstract List Restrictions {get;};
    }
    
    public interface IRestriction{}
    
    public interface IRestrictionWithLimit:IRestriction where T:struct
    {
        T Limit {get;} 
    }
    
    public TimeRestriction:IRestrictionWithLimit
    {
        public TimeSpan Limit{get;set;}
    }
    
    public AgeRestriction:IRestrictionWithLimit
    {
        public int Limit{get;set;}
    }
    
    public class BlockRule:AbstractBlockRule
    {
        public virtual List Restrictions {get;set;}
    }
    

提交回复
热议问题