Solved

Is this class structure method correct/desireblable?

Posted on 2011-03-16
4
403 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help deploying my first MVC.Net app with a SQL Server backend 3 25
parsing JSON help 1 20
JSON Deserialize issue 6 26
linq, c# 8 24
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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 custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

828 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