Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4017334
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:50:55+00:00 2026-05-20T09:50:55+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:50:55+00:00Added an answer on May 20, 2026 at 9:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.