Wednesday, March 28, 2012

Validating a DTO (Data Transfer Object)

I am building a home groan DAL (yes I meant to write groan instead of grown). I know there are many tools out there that do the same thing. I am doing this purely for educational purposes (I am curious about how to implement a DAL). I have decided that I will transfer data between layers via a DTO. For each database table in my application I am going to create a DTO that has public properties that map directly to a database table.

For the purposes of this question I have the following:

Database Tables DTO's

Employee EmployeeDTO

Department DepartmentDTO

I also have a EmployeeCRUD class and a DepartmentCRUD class that handle all of the CRUD operations for these tables/DTO's. These CRUD classes will take a DTO object as a parameter (ie. EmployeeCRUD.Insert(employeeDTO)

Before I perform an insert or update I would like to validate the fields within the DTO.

Here are my questions:

1. Would I be better off putting the validation method in the DTO class or the CRUD class (the call to the validation method will be done in the CRUD class before the insert and update methods)?

2. One of the fields in my Employee table and my EmployeeDTO is departmentCode. I am not sure of the best method to validate the department code (which is a foreign key in the department table).

Should I validate the departmentCode like I would any field in my DTO ( i.e. if (departmentCode.IsNullOrEmpty)...) or should I create a DepartmentDTO object within my EmployeeDTO object and then call the Validate Method on the DeparmentDTO object?

Thanks.

I would definitely valid my business logic as close to my business objects as possible. I believe that the latest version of the Enterprise Library includes a Validation Block, that uses reflection and attributes on your properties to do some simple validation (Someone correct me if this is wrong, but I believe that this is what I saw in it)


No comments:

Post a Comment