I was wondering what is the best practice for enterprise level architecture based on MVC5. I mean selection between multiple layer or multiple project in one solution? and or maybe more than one solution? any good example project?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Since my question has been visited a lot in the last year and there is no solid answer as I am aware of that, I decided to provide a comprehensive answer as much as possible. This answer is based on some actual projects experience and with few expert consultations:
- First of all, it is important to note that in software design
process, there is nothing like solid right and wrong. As long as an
approach works for your project and fits well, it is
right
and if it doesn’t, it iswrong
. There are no rigid principals in software design. There areProject needs and specifications
. But generally, it has been accepted usingDesign Patterns and Principles
makes project morerobust
,reliable
andeasy to maintain
and make your codeloosely coupled and highly cohesive
. - The whole story of
Software Design and Architecture
is about how you could manage your project easily and how you could maintain your future changes. Think about which approach gives you best answer on them. That will be the best for you. Don't think too much aboutProfessionalism
! .Your project grows by time and gets more mature. So just think about your project! - As a first step and for Enterprise level application architecture,
always try to follow
Separation of Concerns
orSoC
. It means you should have different tiers for different layers of your project. It is highly recommended to use different project in your solution forData Access Layer
,Domain Entities
,Business Layer
andPresentation Layer
. In MVC5 project, it is better to useClass Library Project
forData Access Layer
,Domain Entities
,Business Layer
and a MVC project forPresentation Layer
. Data Access Layer
is the project that faces to database and database interactions. You could have all yourEntity Framework
or similar entities in this project. Having separated layer for database layer means in the case of changing your project data warehouse, the only thing you need to change is changing this project and some minor changes on yourBusiness Layer
. All other projects in your solution remain intact. So you could easily move from MS Sql to Oracle or fromEntity Framework
toNHibernate
.Domain Entities
is the project I use to define all my solution level interfaces, classes, enums and variables. This project keeps integrity throughout my solution on my classes and my methods. My all classes in whole solution are inherited from interfaces in this project. So I have one place to change my classes or global variables and it meansEasy to Maintain
for future in my solution and easy to understand for newly joined developers to the project.Business Layer
is the place I put my all business logic includingBusiness Entities
andBusiness Services
. The whole idea about this layer is having one place to keep your all business methods and interactions. All calculations, object modification and all logic about data including saving, retrieving, changing and so on should happen in this section. By having this layer in your project, you could have different consumers at the same time, for example one nativeMVC
and oneWeb API
layer. Or you could provide different feeding based on different business services consumers specifications. It is highly recommended to avoid putting any business logic into controller section of MVC layer. Having any business logic inside controllers means you using your presentation layer as business logic layer and it violatesSeparation of Concerns
. Then it won’t be easy to change from one presentationlayer to other or having different type of consumers for your solution. It is better to keep controller section in MVC as slim as possible. The controllers should only have logic and methods directly related toView Models
. For more information aboutView Models
refer to section7
. One thing to remember, It is better to have differentBusiness Services
classes based on your solution objects orBusiness Entities
.Presentation Layer
in MVC solution will be an MVC project. But solution could have other type or more than one Presentation Layers for different consumers or technology. For example you could have one MVC layer and oneWeb API
in one solution. Generally Use Presentation Layer to keep all presentation logic in it. Presentation logic shouldn’t have anything related to business logic or data logic. So question is what isPresentation logic
?Presentation logic
is logic related to view models. View models are objects customized for views or pages. In most cases, business objects are not suitable to use in views. On the other hand, presentation views usually need some validation logic or presentation logic, for example display name different than original object names. In these cases it is better keep presentation logic separated than business logic to make it easy to change presentation logic or business logic independently and even easy to switch presentation layer for different UI design or changing business logic for having more functionality without fear of any interruption with presentation logic. In the case of using MVC project as presentation layer for solution, all view models should be places inModels
section of MVC project and all presentation logic should be placed inControllers
section of project.- The last thing to say is for every multi-tier solution, you need
frameworks for object to object mapping, for example to convert your
business entity to view model. There are some tools for this
purposes like
AutoMapper
,BLToolkit
, andEmitMapper
.
Last word: please comment and score question
and my answer
to make it better!
回答2:
Take a look at Contoso University. Excellent example of Enterprise level application.
Sample Web Application Demonstration