Solved

Is this class structure method correct/desireblable?

Posted on 2011-03-16
4
399 Views
Last Modified: 2013-12-17
Hi!

I'm building a MVC application which is a bit more complex than what I usually do and I want to try a new class structure. Basically theres a lot of reading going on. Just 5-10% of operations will be insert/update against the database.

Because of this, I'm thinking of creating base DTO classes which would be returned from a database layer. Then, business objects would inherit from the DTO class in order to extend the basic structure with all the validation and business rules.

Example:

namespace Project.DTO
{
	public class Employee
	{
		public string Name;
		public string Surname;
		...
	}
}

namespace Project
{
	public class Employee : Project.DTO.Emplotee
	{
		public bool IsValid()
		{
		...
		}
	}
}

Open in new window


Is this a good approach? What I haven't thought off yet is how to use them inside the MVC, as the "correct" way would be to implement model classes. I believe I could create model classes that inherited from the DTO objects as well... but I'm unsure.

I would also need a way to handle all validation functions with some kind of Interface, as to avoid repeating to much generic code on the GUI.

Thanks in advance!
0
Comment
Question by:Cipher17
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35154897
You might be making more work for yourself than necessary. MVC supports validation through the use of Data Annotations, which are attributes you can decorate your model with. You can then just check the IsValid property of the ModelState object in your controller to see if it passed validation checks.

This would be worth a read if you are unfamiliar:

    http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
     
0
 

Author Comment

by:Cipher17
ID: 35156532
I actually have a few applications running which have the models set up like that.

The problem is that this mixes the validation/business logic with the MVC framework too much, meaning I can't reuse the business layer on a service project or a client-server app which is part of the project.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35156689
You could implement your business objects as partial classes and then create a partial class in MVC for the data annotations. That's generally how you would do things if your models were coming from an EF4 model.
0
 

Author Closing Comment

by:Cipher17
ID: 35180893
At the end what I did was to create a DTO class and a Business Object which has a constructor that can accept a DTO class to initilize itself.

For validation, DataAnnotaions are used and a IsValid function was created on the business object that can evaluate the DataAnnotaions even outside the context of a MVC application.

I haven't finished the implementation but this seems like a well structured way for now.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now