MVC pattern differences

前端 未结 3 990
情深已故
情深已故 2021-02-04 11:26

I just need a few links to articles I can read up on or some basic explanations regarding the different patterns used in MVC (C#).

At present I tend to build my web apps

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 11:42

    Controller:

    In theory your controller should be only handling "data". Moving pieces of information from one place to another.

    Little example:

    1. Controller receives request "GetMessage" with some parameter.
    2. Sends this data to service layer. In service layer you are accessing repository returning message.
    3. Cntroller receives this message (or if there were none null) and decides if it shoudl send received message back or maybe there was an error and user should be notified somehow.

    All the business logic "in theory" should be behind some service layer. That way you can easilly test everything. Logic in controller makes some tests more difficult.

    Interfaces:

    Interface based design is very popular now. Especially with all the IOC Containers handling dependancy injection. But if you are starting with this concept don't bother about these keywords. If you know the Repository pattern, then try at first with IRepository interface and instead of accesing repository by concrete class, use IRepository. (Just change field in controller from Repository to IRepository).

    Generaly about interfaces

    You will see the benefit of interfaces in more complex scenarios, but there is one technique that will show you all the glory of this approach. Unit Testing + Mocking.

提交回复
热议问题