0

So I am new to MVC and have read about a couple dozen of similar questions on SO and some blogs. I can't understnd how to structure my application. Either I'm not getting it or people seem to have different opinions on how to do it. So here is my specific, simple example: login screen and account create screen.

From my understanding I should have the following:

view Simple in this case two views

model Two view models. Login has username and password. Register has username, password, email, etc. Only properties no methods.

controller Builds view model by calling service layer like CreateUser()

business/service Separate project. Has methods to interact with database and apply business logic. Called by controller which massages output into view model format. This project has its own models/classes not tied to a specific view. CreateUser() in this layer would call the stored procedure on the db

Is that the correct flow? And also when returning data from the business layer I shouldn't use the view models. So do I create another set of models on the business side that resemble the logical entities in the db?

1 Answer 1

2

This sounds reasonable. The point about separate models and view models is a general advice in order to avoid tight coupling between your service layer and your UI. It means that multiple applications can consume the services even if their UI screens are different. I do however think it in some cases makes sense to skip the view models if they become mirror images of the models. I only tend to create view models if they add some sort of value when it comes to preparing the data for the view (formatting etc).

However, some might claim that you should always create view models - even ones that mirror the model. I can understand that argument as well since it is related to avoiding coupling between the UI and the service which enables you the evolve the service without changes up the chain to the UI. You have to make a judgment call on how important that is for you since that comes at the cost of a layer that "ferries" values between identical objects.

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

4 Comments

So let's say I have several pages where I'm returning user data. In some it may just be the username, last login date and their user ID. In others it may be more detailed like a full blown profile. What should the output from the service layer be? Should it always be an all encompassing class. If I start creating case specific output classes then aren't I essentially duplicating the view models?
I would only add view models where it makes sense and adds value. In some cases it may work to bind the model object directly to the view. The point about what the service returns is up for debate. Some prefer very granular services (micro services), but others prefer more general services.
So I could conceivably have another project containing domain models like a SiteUser and an extended SiteUserProfile that inherits from the first. My service methods could return either depending on the function to the controller? What trips me up is say I have a field like LastActivityDate. Some screens need it, some don't. If I build a general service, it sounds like my SiteUser model returned by service would contain that field whether or not it's used by that specific controller?
It's ok to return fields from the service that are not used by all views. However, I think that is an argument against inheritance between models and view models. IMO it's not a big deal if a view model redefines properties that are defined on the models.

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.