Solved

Is this class structure method correct/desireblable?

Posted on 2011-03-16
4
398 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
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.

763 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

13 Experts available now in Live!

Get 1:1 Help Now