Search code examples
c#design-patternsooad

Encapsulate Use Cases in a software


I usually write use cases for all the software that I develop. For each use case I generally write a controller which directs the flow (implements a use case).

I have recently started developing web apps using Asp.net MVC. One of the best practices of Asp.net MVC is to keep very less logic in the controllers. I am not able to figure out how will I change my design to reflect this.

I basically want a way to encapsulate my use cases.


Solution

  • Create a business component to encapsulate use cases. For instance if you have a leave management system you would have use cases like apply for a leave, approve a leave request, reject a leave request, etc. For this you can create a business component (class) called Leave Manager with methods (functions/operations) like "Apply", "Approve", "Reject", etc. These methods will encapsulate your use cases. These methods would take your business entities and data store classes as input and execute the use case.

    class LeaveManager{
         int Apply(from, to);
    
         bool Approve(leaveApplicationId, approverId);
    
         bool Reject(leaveApplicationId, approverId);
    }
    

    You can then use this business component in your controllers to execute the use case by supplying the required parameters.