Concepts on MVP with DDD - c#

I recently started doing web dev in C# and I am confused about MVP pattern with the concept of Domain Driven Design.
I was told by one of the seniors that the application is separated into 5 different layers. The hierachical structure is like this: View, Presenter, Service, Repository, Entity.
Here are my understandings for these different layers:
View: Representation of data that is being delivered to client
Presenter: Codebehind. Handles client's request and sends info to service
Serivce: Business logic layer. Manipulates data.
Repo: Not exactly sure what it does.
Entity: An unique object in the domain, and no two objects are ever the same unless they have the same identity (attributes)
So, are my descriptions correct? Can someone elaborate on that? Also, what does the Repo layer do? Does it handle db transaction?
This might not be purely MVP pattern but it seems like this is the state of the application and I am new to all these concepts. It would be great if someone can explain it to me in simpler terms. Thanks.

There are a flew conflicts in my experience.
View - The actual page that renders data. The code-behind will implement the Interface required for the View.
Presenter - No, not the code-behind. The code-behind will instantiate a Presenter. The code-behind implements the View interface, and instantiates a Presenter (and generally passes itself along), like so:
public class CodeBehindPage : System.Web.UI.Page, IViewInterface {
private Presenter myPresenter;
public CodeBehindPage() {
myPresenter = new Presenter(this); // Presenter will require IViewInterface
}
}
Service - Correct.
Repository - Correct, it handles database transactions (loading/saving/etc).
Entity - Correct. Entities are Database objects.

I don't prefer to combine MVP design pattern with N-Tier Architecture. What you have described is more likely about N-Tier Architecture. N-Tier is an architecture that creates tier between logics.
Mainly there are 3 logics, presentation logic (UI specific logic such as calculating height or width of a control), business logic (data manipulation, validation, etc) and data access (CRUD operation).
MVP design pattern itself only consist of Model, View and Presenter. The view and presenter, in N-Tier architecture is considered as presentation logic. Model is too general in N-Tier, it can include both business logic and data access. MVP are a design where the view is communicating with the code behind via event-driven. As example is Asp.Net and C# Winform. Another good design pattern is MVC which is used in ASP.Net MVC.
Now speaking of your understandings of layers:
View is the UI. In Asp.Net it is .aspx file. It is considered as Presentation Logic Layer in N-Tier
Presenter is the code behind. In Asp.Net it is .aspx.cs file. It is considered as Presentation Logic Layer in N-Tier
Service is good, it is considered as Business Logic Layer in N-Tier
Repository is data access. Its main purpose is to communicate with the data storage. It can be Xml, Flat file, CSV or commonly database. It is considered as Data Access Layer in N-Tier
Entity is an object, which purpose is modelling the data, and will be passed between the layer. There is also a principle of POCO, where your Entity should be made as simple as you can (only consist of Properties). Its main purpose so that your Entities can be used in any kind of layers.
Domain Driven Design itself is another concept. It is a concept of designing the application regarding to the business process requirement. Not based on data which is usually be done by most programmer.

Related

Separation of Concerns in MVVM

I am working on understanding MVVM better, and from trusty Wikipedia (and lots of other research), I have gathered that the ViewModel holds presentation logic and the Model holds the business logic.
MVVM Pattern (Image courtersy of Wikipedia)
My question is, how is there a separation of concerns between logic and data when the Model is holding the business logic? Why is this a good pattern and why would I want to use it instead of using MVC where the Controller handles business and presentation logic (if I understand it correctly)? (I am using WPF, which based on my research, mainly uses MVVM and rarely uses MVC, and I still don't understand why).
Separating presentation logic from business logic allows you to change business logic without directly affecting presentation logic.
For example, if you have a RESTful web API that retrieves some data a view should render, the ViewModel should not be directly handling the HTTP operation or any translation of the incoming data. The ViewModel should ask a (separated) service class to retrieve that data and then the ViewModel can choose how to present that data, such as by pagination, sorting, filtering, and so on. You may choose to paginate at the web API level instead, but that's an implementation detail. In the event that your API contract changes, you can change the web API service without affecting the presentation logic: separation of concerns.
Let's get some things straight:
MVC = Model View Controller
MVVM = Model View ViewModel
With MVC, the controller controls the data and the business logic, the model stores the data temporarily, and the View can read the data from the model. In this model, the View handles the presentation logic. In C#, you can use Razor Pages (.cshtml) to dynamically insert values into the view.
MVVM is very similar. The Model is responsible for holding the data and business logic, the View Model holds the data temporarily, and the View controls the presentation logic. The main difference is the physical separation in your code.
With MVC, each step is separated into its own file. With MVVM, usually two or more steps are combined together into one file. This can be advantageous because it keeps you from having controllers that are hundreds or thousands of lines long if you have lots of pages. I personally still like MVC, because it keeps everything separate instead of combining it together.

Are all MVC models lightweight?

So I was first introduced to the idea of a model when I learned WPF. The MVVM concept of a model seems to be more aligned to general 'business logic' encased in a class/set of classes. When I look at MVC though, the model seems to be lightweight-classes that are "passed" to and from the user via forms. Is this assessment correct, or are there situations where a model is something heavier (does more than just hold and validate data input from the user).
You can use Models (classes) as data container (which i usually do) , look at anemic domain model (http://en.wikipedia.org/wiki/Anemic_domain_model), then you can implement business layer, service, data layer independently.
Or you can implement them within your model, which some folks wants to do that due to encapsulation, and several design priciples.
Look into domain driven design as well. (http://en.wikipedia.org/wiki/Domain-driven_design)
Well in MVC or another similar Design Pattern you have defined 3 separate layers of your application. The model with the logic of your objects, the view for showing and getting information from the user, and then a controller witch does the interaction and exchanges between the 2.
MVC is more used for web apps and MVVM with WPF. The reason is that you with WPF can do the databinding of the objects in your View with the one in the ViewModel.
For example a Twiter app: You can do have a model (tweetItem) with all the properties of a Tweet, a ViewModel that gets and stores the tweets in an collection and view that has a list, for showing them, binded to that collection.
There for MVVM or MVC only impact in your app is on how the code is organized.

The model in MVVM

Unless I misunderstand - Most of the articles I read on MVVM explains model in MVVM as the piece holding domain/business-logic, but what baffles me is that MVVM is a presentation layer pattern and presentation layer does not hold business logic in entirety. Can some please help me understand how the domain-logic in business layer maps to the model in the presentation layer, is the model in MVVM actually a DTO?
I'd appreciate if some one can help explain with an example how business layer is mapped to a MVVM model in SOA (business logic sits behind a web service). Thanks.
MVVM, like MVC, is just a form of Separated Presentation in which the intent is to achieve separation of concerns between the part of the applicaton concerned with the logic and state of the UI and the part of the application concerned with the logic and state relating to the business domain. So MVVM doesn't really dictate anything about the form that the Model part takes as long as it is separated from presentation concerns.
The Model is purposely not coupled or dependent in any way on the presentational aspects of the application but beyond that there are many different ways to implement the "M" part of the triad. In particular, it doesn't have to map to a single object: it could mean interacting with a service that returns DTOs, it could mean publishing and subscribing to messages on a message bus or it could mean retrieving domain objects that represent entities in your domain, calling methods on them and then persisting them.
What's really unique about the MVVM pattern is the ViewModel's role in it since its purpose is to represent the state of the UI in a way that can be consumed by View technologies that have rich databinding capabilities. Without rich databinding support you would use a different form of Separated Presentation such as MVC or MVP, but the "M" part could still be the same because it's independent of the UI technology by definition. That is the important factor.
The Model in MVVM is not at all the DTO. DTO is the Data Transferable Object. Its more like the entity classes. It is basically used to transfer data from one layer to other layer; such as Presentation Layer to Business Layer or Business Layer to Data Access Layer.
And the Model mainly consists the Business Logic. Presentation Layer through View Model calls the Business Logic of Model as and when required.
Very often Model is encapsulated by ViewModel itself. You have to separate Model and ViewModel when by design it could be that single ViewModel uses different Models. But really this is a rare case so ViewModel can directly work with services.
If single ViewModel can serve different kind of Models which could be substituted one by one - introduce separate layer of Models, abstract them by interfaces and inject into appropriate ViewModels otherwise View and ViewModel is enough.

Passing domain model business entity to UI layer question

Is that acceptable to pass directly domain model entity to UI layer instead of corresponding viewmodel?
In my example UI gets some user data and passes it down to presenter which interacts with domain service. Domain service performs some action on the data from the user and returns output results back to UI as domain model entity, which should be typically converted to viewmodel.
The problem is that depending on the user choice (Y or N) inspecting the returned result, it may be returned back to the service for further processing and finally saved to repository.
If we do not save returned domain entity somewhere between those 2 steps, but use viewmodel, we will not be able to pass it back for further processing.
Are there some workarounds when such user interaction takes place?
MVP is defined around the concept of View, Presenter, and Model. There's no reason you need to distinguish between a domain entity and a view Model. They can (and should) be the same thing.
Unlike MVVM, the model plays less of a role in binding to the View. It's the responsibly of the presenter to manipulate the view using the model data. There is no concept of a ViewModel as such in MVP. That term is generally reserved for MVVM where ViewModel is an entity tightly coupled to View, in addition to having a Model.
I would rather recommend separating the presentation model from domain model either in terms of view model (in MVVM) or in terms of Model in MVC / MVP, because in medium to high domain complexity you design domain model in terms of business logic and object oriented design which has a granularity level other than presentation data, when you bind or present data you tend to show part of domain model and flatten some objects to be continent for presentation.
Another aspect is when developing distributed application sometimes you need to expose your data through services (remote facade) thus it is highly preferable to expose your objects in terms of DTOs not to expose you domain model because again the aspect of DTO is different in complexity and granularity than the domain model, dont force your domain model to be corrupted to be presentation friendly or consumer friendly, and dont corrupt your presentation model and consumers to use a business oriented domain model and increase the complexity of adopting them for presentation.

Three tier architecture question

I have an ASP.NET app with a three layer architecture:
Presentation layer: ASP.NET
Bussiness Layer: C# library.
Data Access Layer: C# library with
ADO.Net Entity Framework objects.
Some methods on Bussiness layer would return ADO.NET entity objects but, data access layer is not visible at Presentation layer I can't do that.
My question is: On a design view, Is it correct to expose Entity Objects in the Presentation Layer? I think I only have to link Data Layer library with ASP.NET app.
Thank you!
It's absolutely desirable to have your entity objects available for use and consumption in your presentation tier. That's what all the work is for.
Binding collection of objects to a grid/listview/dropdown
Splashing a single object (i.e. customer) onto a form for read/update/delete
This makes your life easier by far. Otherwise you'd have to pass string after int after double after string between your presentation and business layers.
These may be Entity objects or even your own POCO objects that were hydrated from the Entity objects.
I would even go so far as to say that your Entites should be in their own assembly separate from the DAL.
I suggest that you look into the concepts of View objects...or Data Transfer Objects (DTO). You might consider using a tool like AutoMapper or similar which will create a view specific domain object out of your entities. In general you may have screens that need an entity present to perform its work. But more often than not you will need to pass several different entities. In this case you are better off creating one DTO that contains all of these entities. By doing this you are adding a layer of separation between your presentation layer and your business layer. Often times your entities have more power than you might want to expose to your presentation layer. And...vice versa. Frequently you may need to get some UI messages out to the presentation layer based on some validation flagged in your business layer. Rather than make your ui more complex than it needs to be (by passing in your full entities) you can only pass in what the UI needs in the form of the DTO. Also, there is never a need for your business objects to care about anything specific to the presentation layer. I suggest that you not databind directly to anything as far back as the data access layer. Technically your presentation layer should know as little as possible about your business layer. In the case of MVP or MVC this is very easy to achieve by disconnecting the front end and the back end by way of this additional separation!
I think no, it is not, the best way to do that is to separate data classes from behavior, and reference only data classes in presentation level.The good approach I think to use WCF see this link
See Supervising Controller and Passive View
If you pass the Entity, you are essentially Supervising controller. Otherwise you are Passive View.
Supervising controller is less work, but less testable. Supervising Controller also says databinding is OK.
Passive view is testable but a LOT more work. No databinding. Lots of properties.
Typically I stick with Supervising Controller. You typically don't need that level of testability and it isn't worth the extra trouble.

Categories

Resources