MVC - Service Layer Question
-
I was reading some articles about how to implement a service layer in MVC applications yesterday. One thought that came to mind was: what about data annotations used for model state validation? When you implement a service layer, do you get rid of those data annotations (like [Required] or [MaxLength]) for the model classes? This[^] is the article I was reading. MVC development is mostly new to me, aside from the fact that I have a fundamental knowledge of the core concepts. I switched employers recently and our work here is done primarily in MVC, which I'm happy about because I've been wanting to get into it for a while now. My two coworkers are more knowledgeable than I am, for sure, but they're open to new ideas and new techniques that may help improve the applications. I figured a service layer would be beneficial not only to remove business/validation logic from controllers, but also because we're starting to write unit tests.
djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.
-
I was reading some articles about how to implement a service layer in MVC applications yesterday. One thought that came to mind was: what about data annotations used for model state validation? When you implement a service layer, do you get rid of those data annotations (like [Required] or [MaxLength]) for the model classes? This[^] is the article I was reading. MVC development is mostly new to me, aside from the fact that I have a fundamental knowledge of the core concepts. I switched employers recently and our work here is done primarily in MVC, which I'm happy about because I've been wanting to get into it for a while now. My two coworkers are more knowledgeable than I am, for sure, but they're open to new ideas and new techniques that may help improve the applications. I figured a service layer would be beneficial not only to remove business/validation logic from controllers, but also because we're starting to write unit tests.
djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.
Hi Matt, If I get you correctly then are pointing towards that if you have BL or service layer then you would do away with Model? Correct me if I'm wrong. But, please note that model is the manifestation of your view i.e. it is tightly coupled to your view. You should have that as well as Business layer where you would get the data (from DAL), do some processing and then pass the data onto the model. Also, note that model does not do away DTO as well which would be used to communicate between your various layers. A lot of the time DTO and models are pretty much the same but can differ based on the requirements.
Hope this helps, if it does then mark it as answer Rohit Aanstaande blog sweet blog
-
Hi Matt, If I get you correctly then are pointing towards that if you have BL or service layer then you would do away with Model? Correct me if I'm wrong. But, please note that model is the manifestation of your view i.e. it is tightly coupled to your view. You should have that as well as Business layer where you would get the data (from DAL), do some processing and then pass the data onto the model. Also, note that model does not do away DTO as well which would be used to communicate between your various layers. A lot of the time DTO and models are pretty much the same but can differ based on the requirements.
Hope this helps, if it does then mark it as answer Rohit Aanstaande blog sweet blog
Thank you, Rohit. But no, I know the model is necessary. What I'm referring to is: are data annotation attributes used for validation (like "[Required]") irrelevant when using a service layer?
djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.
-
Thank you, Rohit. But no, I know the model is necessary. What I'm referring to is: are data annotation attributes used for validation (like "[Required]") irrelevant when using a service layer?
djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.
But, Matt data annotations are for validations on the view side and nothing to do with your BL/service layer. Basically in MVC, models are responsible data validations. I would still do the required validations on my BL/service side (required, null, etc.)
Hope this helps, if it does then mark it as answer Rohit Aanstaande blog sweet blog
-
Thank you, Rohit. But no, I know the model is necessary. What I'm referring to is: are data annotation attributes used for validation (like "[Required]") irrelevant when using a service layer?
djj55: Nice but may have a permission problem Pete O'Hanlon: He has my permission to run it.
How do annotations pass through serialisation (JSON)? Caveat: I am even less knowledgeable about MVC! MVC, being server based can have the DAL/Models etc in the same project, eliminating the service component. I don't like that design and will need to look at a service component so this is relevant to me. So the service would supply data based on the Table/View structure from the database via json and the MVC would then deserialise them into UI models. This to me reduces the benefits of using EF spit as the DAL as it is bloody useless in a service.
Never underestimate the power of human stupidity RAH