Solved

Is this class structure method correct/desireblable?

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PowerShell:  Where-Object not working as a filter 2 72
reading excel file in .net 2 33
C# Formatting DateTime String Value 5 52
How can I do Unit Testing with Session ? 1 23
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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.
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.

730 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