Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Is this class structure method correct/desireblable?

Posted on 2011-03-16
4
Medium Priority
?
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

609 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