Search code examples
silverlightwcf-ria-servicesdto

Initializing DTOs on the server-side when using RIA services


Say you have an domain entity with business logic for initializing its default values. E.g.

class User : IUser, Entity
{
  public User()
  {
    StartDate = DateTime.Now;
    EndDate = StartDate.AddDays(3); // This value could be user-configured.
  }
  public DateTime StartDate { get; set; }
  public DateTime EndDate { get; set; }
}

Using RIA services, the DTO that will be generated of course does not include any logic, only public properties. Which means that when a client (e.g. Silverlight application) displays a 'create new user' dialog, it will not be able to populate the fields with any default values (without, of course, duplicating the business logic on the client).

In an attempt to achieve this, I created new DTO (UserDTO) and a query in my UserDomainService:

// Construct a new domain entity and then convert to DTO
public UserDTO CreateNewUser()
{
  var user = new User(); // Business logic has now been executed.
  return new UserDTO(user);
}

This does allow the client to populate fields with default values, however when it comes time to add the newly created user, RIA has already added the UserDTO to it's internally mainted collection of entities, so you cannot call .Add on your domain context. You can still just call SubmitChanges, which will trigger the [Update] method to be called, however this seems to be going against the grain of how RIA services is supposed to be used (i.e. you shouldn't be doing an INSERT operation in an UPDATE method).

Is this scenario (i.e. server-side creation of DTOs) achievable in RIA services?


Solution

  • I don't know what your business logic looks like, but if you used a common method to save objects (whether new or modified) on the server, than you would be able to differentiate within that method, whether it is a modified object or really a new one.

    Example on the server:

    [Insert]
    public void InsertUser(UserDTO user)
    {
      this.SaveUser(user);
    }
    
    [Update]
    public void UpdateUser(UserDTO user)
    {
      this.SaveUser(user);
    }
    

    You could add a property to your User (or the base class, if you have one):

    public class UserDTO
    {
      [...]
    
      // only set this within the constructor, 
      // unfortunately it cannot be "private set", because of RIA Services
      public bool IsNewEntity { get; set; }
    }
    

    In your SaveUser method use that flag:

    private void SaveUser(UserDTO user)
    {
      if (user.IsNewEntity)
      {
        // do something with a new user
      }
      else 
      {
        // do something with an existing user
      }
    }
    

    The Constructor for the UserDTO would then be:

    public UserDTO()
    {
      this.IsNewEntity = true;
    }
    

    I know, this looks a little trivial, but I do not know of a more "elegant" way.