1

I am using ASP.NET MVC in my application. I have divided my application into three layer architecture. 1) Data Access Layer (using Entity Framework), 2) Application/Business Layer, 3)Presentation Layer (ASP.NET MVC).

Because I am using MVC framework on my presentation layer, I am confused about the business logic. I need to know where do I put my business logic in MVC pattern. In other words we can say from where I need to call my middle layer. From the Models or from the Controllers?

If I call my business logic from Controller then It seem like the Models are useless. Other wise if I call business logic from the model then it seems like unnecessary bidden on the system because Business Object maps with model and then model is passed to the Controller. Model does exactly what DTO is doing.

Any help will be appreciated

2 Answers 2

6

ASP.NET MVC layer or tier doesn't contains neither business logic nor business model. M in MVC stands for UI model, not the model of your application core and MVC (as well as other MV* patterns) is generally pattern for separating UI concerns. You should send messages (call) your Business Layer (BL) from Controllers, aggregate data, create or map it results into UI model and pass it to view. Your UI model should know nothing of BL model - this distinction makes loosely coupled layers of your application.

In other words we can say from where I need to call my middle layer. From the Models or from the Controllers?

Definitely from Controllers. You inject dependencies to it and call them from your Action methods. ASP.NET MVC provide lots of mechanisms of injection dependencies into controllers and integrates nicely with NInject, StructureMap and some other IoC containers.

Dependencies between components in MVC is given below

enter image description here

Picture is takes from Martin's Fowler article on GUI Architecture, which by the way, very nice reading regarding MVC and MVP.

Also, Pluralsight has set of videos on Software Practices, which covers Design Patterns. I've learned a lot from their definition of MVVM and MVP.

Reading this materials should increase your understanding not only on patterns itself, but also in how they fit into application environment and interact with it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot Ilya Ivanov. Your really helped me. one thing I forgot to ask that where do we perform presentation validation? In the view or in the controller.
Can you please explain in a little bit more detail what do you mean by presentation validation? User input? It should be triggered by controllers but it's is better to place logic near the data, on which this validation in executed. So model seems to be a good place. But this can vary depending on the context of the problem.
Yes I want to validate user input. You said Model is a good place to do but what about client side validation? If we put validation in Model it requires post back or if we put validation on both places model and view the code is isolated in two different places for the same concern can cause problem to maintain may hard to maintain
Yes, but client side is usually very different from server side both in performance and environment, so you still need to duplicate lots of validation pieces. There exist lots of different approaches and I personally recommend to use conventions for validation. At least to try them. Try to experiment - I can't say a lot about your problem, because I know nothing about it.
0

This is purely design/architecture decision you have to make based on your requirements.

If you want to scale up your application to support other services/applications, it is advised not to write any business logic in Controller/Model. You could write it in business/application layer. This will help you to scale up your architecture in future. Say if you want to create restful service for you mobile application, you could just write services as wrapper to re-use existing business/application layer.

Also just have a look at Domain Driven Design, Eric Evans book is worth reading.

http://dddsample.sourceforge.net/architecture.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.