locked
How do you implement chain of responsibility pattern in Entity Framework? RRS feed

  • Question

  • User-1095454647 posted

    If you are using entity framework would you create separate classes/tables for different Employees and Managers? Or a single table with a flag ?

    I am a bit confused as to what would be the better approach. Let's suppose you have Employees and Managers which are basically two different kinds of employees in the end. You could also add HOD(Head of the Department) to this mix. 

    Manager and HOD have some privileges of course but all the attributes / columns remain almost identical to regular employees. Would you create one table/class or different tables/class? 

    If I create one table I will have one class or vice versa. 

    Consider his scenario when a certain person places an expense  claim which can be approved by manager if its below $100 else it can be approved only by HOD and so on.

    In either case, how does one implement chain of responsibility here? 

    The following is my understanding of Chain of Responsibility:

    	abstract class Approver  //can be interface  
    	{  
    	  protected Approver _successor;  
    	  public void SetSuccessor(Approver successor)  
    	  {  
    	    this._successor=successor;  
    	  }  
    	  
    	  public abstract void ProcessRequest(Purchase purchase)   
    	}  
    	class Manager : Approver  
    	{  
    	    public override void ProcessRequest(Purchase purchase)  
    	        {  
    	       if(purchase.Amount<10000)  
    	       {  
    	        console.wireline('approved');  
    	           }  
    	       else if(successor !=null)  
    	       {  
    	         _successor.ProcessRequest(purchase);          
    	       }  
    	    }  
    	}  
    	class HOD: Approver  
    	{  
    	    public override void ProcessRequest(Purchase purchase)  
    	        {  
    	       if(purchase.Amount<20000)  
    	       {  
    	        console.wireline('approved');  
    	           }  
    	       else if(successor !=null)  
    	       {  
    	         successor.ProcessRequest(purchase);          
    	       }  
    	    }  
    	}  
    
    
    	Approver ronny=new Manager();  
    	Approver bobby=new HOD();  
    	Approver ricky=new President();  
    	  
    	ronny.SetSuccessor(bobby);   //setting the successors
    	bobby.SetSuccessor(ricky);  
    	  
    	Purchase p =new Purchase(8880,350,"Assets");  
    	ronny.ProcessRequest(p);  
    

    How exactly does this work in entity framework? 



    Saturday, March 23, 2019 5:03 PM

All replies

  • User475983607 posted

    Commonly, roles or claims are used to define what users can do in an application. 

    How exactly does this work in entity framework? 

    Use ASP Identity to manage user accounts.

    Use claim based authorization to house the approver logic.

    https://docs.microsoft.com/en-us/aspnet/core/security/authorization/claims?view=aspnetcore-2.2

    There's also policy based.

    https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies?view=aspnetcore-2.2

    Saturday, March 23, 2019 5:36 PM
  • User-1095454647 posted

    Identity role based policies are something I have been meaning to look into and thanks for reminding me. But until quite recently there was no identity framework. 
    If you are working on a MVC 4 project with entity framework 5 (which i believe does not support identity?) How will you tackle it? 

    Saturday, March 23, 2019 5:53 PM
  • User475983607 posted

    Identity role based policies are something I have been meaning to look into and thanks for reminding me. But until quite recently there was no identity framework. 

    I Identity is the newest Membership API.  The first was named Membership Provider. Then came Simple Membership which works in MVC 4 and EF5.

    https://docs.microsoft.com/en-us/aspnet/identity/overview/getting-started/introduction-to-aspnet-identity

    All the APIs have roles.

    If you are working on a MVC 4 project with entity framework 5 (which i believe does not support identity?) How will you tackle it? 

    I don't get the inheritance structure shown above.  But if I had to build this then I would simply assign each user to a role.  Then it's just a matter of managing the target object's state which can be table records.  If there are rules for each approver, then these can be stored in a table joined by the role.  

    Saturday, March 23, 2019 6:34 PM