Where to write Database and Business logic in MVC?

喜夏-厌秋 提交于 2019-12-30 03:58:19

问题


As I am learning and working on Asp.Net MVC application, I want to know that what is the better place to write Business Logic and Data Access logic in MVC.

Where should I write DataAccess and Business Logic among three layers (Model, View and Controller) ??

Could anybody please tell me the correct way to write the code for this.

Scenario: I want to retrieve all the employees where employee name like 'Mi%' ( I have SQL procedure to execute and retrieve the data.)

PS: Want to know that where I should create instance of Business Logic class and where I should create instance of Data Access layers class?

Thanks in advance.


回答1:


Business logic should be in the Model.

Data Access can either be its own later your Controllers call, or automated in an ORM that your Controller will call through repositories.

A walk-through covering this can be found in Nerd Dinner, look for the free download section.




回答2:


Business logic (BL) and data access (DAO) should be in separate layers. Models should only keep data and contain no logic. Controller should only receive data from view and send it to BL layer (or send from BL to view).
It's not a strict rules, but most recently used approach




回答3:


The view is where you put your interface code.

The Controller is the place that connects the view with the model.

The model stores business logic and possibly database access. (Some ORM Layer can be used aswell)




回答4:


You could skip the three tier thinking completely and go for another way of thinking;

User Acts; that sends Command or Event to Background Service. Background Service you can run in process, for starters. Background Service publishes events, again, in memory. You create multiple Views that subscribe the Event from the Background Service. Each View has a 'last seen' Event; it's an integer. Each Start it reads all later Events from the "global log".

Views are throw-away, because you have a global log of Event/Command from GUI and from Background Service, and they'll read up all missed events when you start, anyhow.

This is the equivalent of "do something and I expect to see that 'change' in this other view". You also have:

An Interaction is when you have a single or multiple Events from either GUI or Background Service and you let some actor, most likely a Saga, perform some action, possibly with compensations based on other Events, should the action fail.

Also, I can recommend browsing this presentation



来源:https://stackoverflow.com/questions/3679263/where-to-write-database-and-business-logic-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!