Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Design Data Transfer Objects in Business Logic Layer

DTO

I'm building a Web application I would like to scale to many users. Also, I need to expose functionality to trusted third parties via Web Services.

I'm using LLBLGen to generate the data access layer (using SQL Server 2008). The goal is to build a business logic layer that shields the Web App from the details of DAL and, of course, to provide an extra level of validation beyond the DAL. Also, as far as I can tell right now, the Web Service will essentially be a thin wrapper over the BLL.

The DAL, of course, has its own set of entity objects, for instance, CustomerEntity, ProductEntity, and so forth. However, I don't want the presentation layer to have access to these objects directly, as they contain DAL specific methods and the assembly is specific to the DAL and so on. So, the idea is to create Data Transfer Objects (DTO). The idea is that these will be, essentially, plain old C#/.NET objects that have all the fields of, say, a CustomerEntity that are actually the database table Customer but none of the other stuff, except maybe some IsChanged/IsDirty properties. So, there would be CustomerDTO, ProductDTO, etc. I assume these would inherit from a base DTO class. I believe I can generate these with some template for LLBLGen, but I'm not sure about it yet.

So, the idea is that the BLL will expose its functionality by accepting and returning these DTO objects. I think the Web Service will handle converting these objects to XML for the third parties using it, many may not be using .NET (also, some things will be script callable from AJAX calls on the Web App, using JSON).

I'm not sure the best way to design this and exactly how to go forward. Here are some issues:

1) How should this be exposed to the clients (The presentation tier and to the Web Service code)

I was thinking that there would be one public class that has these methods, every call would be be an atomic operation:

InsertDTO, UpdateDTO, DeleteDTO, GetProducts, GetProductByCustomer, and so forth ...

Then the clients would just call these methods and pass in the appropriate arguments, typically a DTO.

Is this a good, workable approach?

2) What to return from these methods? Obviously, the Get/Fetch sort of methods will return DTO. But what about Inserts? Part of the signature could be:

InsertDTO(DTO dto)

However, when inserting what should be returned? I want to be notified of errors. However, I use autoincrementing primary keys for some tables (However, a few tables have natural keys, particularly many-to-many ones).

One option I thought about was a Result class:

class Result
{
    public Exception Error {get; set;}
    public DTO AffectedObject {get; set;}
}

So, on an insert, the DTO would get its get ID (like CustomerDTO.CustomerID) property set and then put in this result object. The client will know if there is an error if Result.Error != null and then it would know the ID from the Result.AffectedObject property.

Is this a good approach? One problem is that it seems like it is passing a lot of data back and forth that is redundant (when it's just the ID). I don't think adding a "int NewID" property would be clean because some inserts will not have a autoincrementing key like that. Another issue is that I don't think Web Services would handle this well? I believe they would just return the base DTO for AffectedObject in the Result class, rather than the derived DTO. I suppose I could solve this by having a LOT of the different kinds of Result objects (maybe derived from a base Result and inherit the Error property) but that doesn't seem very clean.

All right, I hope this isn't too wordy but I want to be clear.

like image 522
JustAProgrammer Avatar asked Feb 22 '09 07:02

JustAProgrammer


2 Answers

1: That is a pretty standard approach, that lends itself well to a "repository" implementation for the best unit-testable approach.

2: Exceptions (which should be declared as "faults" on the WCF boundary, btw) will get raised automatically. You don't need to handle that directly. For data - there are three common approaches:

  • use ref on the contract (not very pretty)
  • return the (updated) object - i.e. public DTO SomeOperation(DTO item);
  • return just the updated identity information (primary-key / timestamp / etc)

One thing about all of these is that it doesn't necessitate a different type per operation (contrast your Result class, which would need to be duplicated per DTO).

like image 84
Marc Gravell Avatar answered Nov 15 '22 19:11

Marc Gravell


Q1: You can think of your WCF Data Contract composite types as DTOs to solve this problem. This way your UI layer only has access to the DataContract's DataMember properties. Your atomic operations would be the methods exposed by your WCF Interface.

Q2: Configure your Response data contracts to return a new custom type with your primary keys etc... WCF can also be configured to bubble exceptions back to the UI.

like image 28
grenade Avatar answered Nov 15 '22 20:11

grenade