16

I tried to structure my last sizeable MVC project following a best practice approach, but didn't quite understand what I was doing.

It has a Data, Business and Web (MVC) project, but the controllers contain most of the code, the Data layer uses NHibernate and has a few repositories responsible for too many things, and the Business layer is a dumping ground for anything that doesn't belong in the other two projects. It works, but I feel it could have been setup better - the main things I'm unhappy with are the fat controllers and the repositories.

I'm starting a new project that might grow to be a decent size, so I'm spending a bit more time trying to get my design right up front. Having read a bit more, I'm trying to have a repository per aggregate-root, and then have a service in the Business layer for each controller in the presentation layer.

My initial hopes were that the bulk of the code would go in the services and this coupled with the smaller repositories would keep my controllers and data layer thin. So far though, this isn't happening.

Everything I've read suggests that the View Models should not be returned from the Business layer, and should be populated in the presentation layer, so at the moment my service layer is mainly passing models from my data layer through to the presentation layer which then does what it needs to prepare the view models. So I still have fat controllers, plus a thin business and data layer.

My presentation layer also knows about both my business and data layer, but I thought part of the point of this separation was to reduce coupling?

Have I got this all wrong? Should I stop trying to blindly follow what I read on the internet and simply prepare view models in the business layer so I can move the bulk of my code in there? Should I just go back to classic ASP? :)

3
  • Try following frameworks: 1) Serenity.is - the best rapid application development framework available, and best rated template in VSGallery. Serenity is built on popular and high quality open source libraries, including Bootstrap, SlickGrid, Dapper and JSON.NET. By combining them with the power of ASP.NET MVC and TypeScript, it provides a robust and stable application platform. http://serenity.is/ 2) Asp Net Boiler Plate - ASP.NET Boilerplate is a general purpose application framework especially designed for new modern web applications. It uses already familiar t Commented Jul 10, 2017 at 11:35
  • 1
    @mijaved Please be objective here. Your statements about these frameworks seem to be copy-pasted from advertisements. They're clearly not the result of neutral, objective assessments of these tools. Commented Jul 10, 2017 at 19:51
  • Related post - ASP.NET MVC (Domain Model, Repository, Fluent, Services - Structure for my Project) Commented Jul 2, 2018 at 9:03

1 Answer 1

13

the main guide that I use when I am setting up a project structure is to ensure that I can add some operationcontract attributes to the business logic layer, and then host it as a wcf service.

If i can do this, that means the business logic layer has isolated my data layer, and is interacting with its client only by passing simple structures and entities. the datalayer is completely hidden.

So my usual structure looks like:

Solution
    Business.Contracts (interfaces for bll layer in here)
    Business.Logic     (concrete implementations of contracts in here)
    Business.Entities  (Pocos that bll uses)
    Data.Contracts     (interfaces for dal)
    Data.Sql           (Concrete Sql implementation of contracts)
    Common.Enums       (Enums needed by all projects)
    FrontEnd           (Main mvc app)

So in this structure, my mvc project only ever deals with the business namespace and the common namespace.

When interacting with the entities however, I tend to use my own models in the mvc project to allow me to add annotations and front end specific functionality, then i provide implicit conversions for these models to be able to be used interchangeably with the business entities.

HTH

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

2 Comments

So your FrontEnd knows about your Business.Contracts and your Business.Entities, your Business.Logic knows about your Data.Contracts and your Data.Sql knows about your Business.Entities? Does that acurately describe the coupling in your layout? Also, what benefits does this structure give you?
yes, pretty much, the business logic layer interacts with the data layer. With this structure, I can isolate the front end away from the data layer, which means that should I need to split the load of the web server, I can move the business logic layer behind a wcf service and host it on a different server.

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.