2
votes

We have two projects in our application:

  1. Web UI project (aspx pages).
  2. WCF project.

Both of these sections will further call the same BL and DAL layer. Here is the architecture:

Web Project:

enter image description here

WCF Project(which will be using REST):

enter image description here

Example of Business object and DTO mentioned above:

public class User
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class UserDTO
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Is it worth to have a separate data transfer object UserDTO.cs for WCF REST layer? We are already having User.cs as a Business object which is being used by Web project, BL and DAL. Further, in our WCF REST layer we are using DTO just for input prams:

  public MyResponse CreateUser(User user)
        {

and from this method we are converting the DTO to Business object (i.e UserDTO to User.cs object) via some mapper and passing it to BL layer which accepts only Business objects and not DTO. i.e from the point WCF is passing the Business object to BL and DAL, it behaves exactly the way UI passes the Business object to BL and DAL layer.

Is there any practically advantage using 2 separate data transfer objects? I have asked this question because IMO this will be redundant and we should go with one data trasnfer object i.e. Business object for both the Web project and WCF project.

1

1 Answers

3
votes

To me, yes, it's always worth doing this, but only if you're using the objects for AJAX calls.

In this scenario it comes into its own:

public class User
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class UserDTO
{ 
    // Hide the UserID
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Do you really want the user to be able to see the AJAX response and get the User ID? Probably not. This is why we use a separate DTO. It also makes sure you're only using lightweight objects, than full, potentially heavy, objects for data transfer between client and server.

Of course, you can also do this:

var query = GetYourUsers();

return query.Select(a => new { a.UserName, a.FirstName, a.LastName });

Which is more lightweight, then you can use the User object to send data from the client-side to the server-side and use a tool, such as Value Injector to inject the sent values from the client to the full object on the server.