Is ASP.NET MVC normal to have side effect presentation logic?

In most of the documentation that you read about ASP.NET MVC, the whole "Problem Separation" is very much pushed. Dependency injection / control inversion, unit testing, preserving "logic" from views, etc.

But how far is it meant to push? Is this bad practice if a particular task requires additional logic beyond the “three-layer” View / Model / Persistence approach?

For example, I have a solution configured with four separate projects.

Project.Web (ASP.NET MVC) [ References Data.dll, Models.dll ] Project.Data (Fluent nHibernate Mapping) [ References Models.dll ] Project.Models (POCO, Helpers, Factories, etc) Project.Tests (Unit Testing) 

So far it has served me well. But for some of my MVC views, I need a very abstract logic, so I need to get involved in Models and arrange the View Model , which is stored in the database.

This cannot happen in my Data section, as this will eliminate the need for reuse, as well as the business logic included in this process. This cannot happen in my Models section, because that would require knowing the Data section, which is not. And I do not want to put it in the Web section, because I do not need a data access code.

Question

Is it a massive violation for me to add, say, a Project.Presentation project that references Data.dll and Models.dll to build what I need? In addition, the project is on the sidelines: is this a bad approach overall or is it something that many of you find it necessary to do sometimes? Part of me feels that if I have to resort to this, I just created my software incorrectly, but at the same time I’m very sure that I did a pretty good job, it’s too abstract to make crude HTML interpretation without agreement the average person.

+4
source share
4 answers

In my experience, the only responsibility is a great way to write code that you expect to change early and often. Like Jan, I also have a solid line between who does what, when. The more you do this, the easier it is to replace a piece of your system. I recently uninstalled linq2sql with EF4.1 and because of SRP, as soon as I got tests running around my new EF4 layer, everything else just worked.

However, I usually let unit testing dictate where these things live - is it my SRP driver, and also ask the main question "must = class / service = know about = something else = do this job"? If the answer is no, it goes somewhere else - if so, then it is a dependency. Now, if it gets painful, his way of telling me “is stupid” ( look at this question for stupid ), and I tried to force something instead of letting it fit the way it should be.

On your main quest: you have clearly defined a gap in your code - he SHOULD know about two things (data and model), and I agree, it should be his own business and pull it out. I would call it a "DomainService" or maybe just a DTO, but the presentation seems like it will be more than just preparing the data. (I would argue that the presentation is processing the presentation ... maybe you're a pretty average presenter?). I also object to your perception that you did “something is wrong” - no, you learn to write code in different ways and allow it to develop, EXACTLY , as it should be. :-)

+2
source

I use the following structure, which more or less solves all the problems that we have regarding MVC structures.

  • Models: contains all ViewModels. Just clean, only getters and setters.
  • Implementation of business logic: a set of projects containing database logic, DAL objects, etc. But it has only one interface (in our case, it is the WCF API).
  • ServiceLayer: The bridge between Business Logic and ViewModels. Doesn't know anything about the web interface.
  • Display layer: Translation between business objects and ViewModels.
  • Web project: contains very subtle controllers and views, and everything related to the website.

The webpage stream stream will follow this stream:

  • URL maps for a specific action
  • Actions are really clean, they only refer to a method in ServiceLayer
  • ServiceLayer performs one or more actions in the implementation of business logic (receive data, store data, etc.).
  • ServiceLayer transfers business objects to the mapping level.
  • The display layer converts business objects to ViewModels

In the code, it would look like this:

 // action public ActionResult ListOfObjects () { var model = new ServiceLayer.ObjectsClient().GetListOfObjects(); return View(model); } // Service Layer public ListOfObjectsModel GetListOfObjects () { var businessEntities = BusinessDao.GetThingysFromDb(); var model = Mapper.TranslateToViewModel(businessEntities); return model; } // Mapping Layer public ListOfObjectsModel TranslateToViewModel(List<BusinessEntity> entities) { // do mapping } 

In POST processing, you will follow the same thread, but the display layer should translate your ViewModel into data objects that are provided to the business logic.

+2
source

"take part in models and organize a viewing model, which is stored in the database"

Then its not a viewmodel.

Does it make it easier?

+1
source

What is a "ViewModel":

More often, the "ViewModel" gets confused with persistence. If you look closely at this word, this is a combination of "View" + "Model", which essentially means that it is a single block of data necessary to satisfy all the needs of your presentation. ViewModel can output multiple objects or sources to create what the View requires.

Now let's get to your question:

Is it a massive violation for me to add, say, a Project.Presentation project that references Data.dll and Models.dll to build what I need?

Re: If I had to do this, I would create a separate namespace (folder) in my MVC project with the name "ViewModels" and place all my view models there. In my opinion, if you want your ViewModels to be in a separate namespace, this would not actually violate MVC. You just enter the separation or make it more friendly unit test.

Just my 2 cents !!!

0
source

All Articles