开发者

Encapsulate Use Cases in a software

开发者 https://www.devze.com 2022-12-16 21:59 出处:网络
I usually write use cas开发者_如何学运维es 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 usually write use cas开发者_如何学运维es 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.


I think having a fat model and skinny controller is generally a good practice in any language and not specifically .NET MVC. Checkout this nice article that goes through a sample scenario showing the advantages of a fat mode in Ruby on Rails (but the ideas apply to any language).

For representing the use-cases in your code, I think a much better place for them is in test-cases rather than the controller.


Push as much business logic to your models and helper classes as possible, and use controllers mainly for handling URL calls and instantiating the relevant models, retrieving data from them, and pushing data to the views. Views and controllers should have as few decisions to make as possible.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号