简体   繁体   中英

How Can I Extend A Entity When Using RIA Services With Silverlight?

On the server side of my Silverlight solution, I have 2 projects.

  1. Website that serves up the Silverlight page.
  2. A Entity Framework data access layer.

I have a entity with FirstName and LastName properties on it. I want to add a FullName property that will be available from the Silverlight client side.

I have added the property:

namespace Server.DAL.Model
{
    public partial class Contact
    {
        public string FullName
        {
            get
            {
                return string.Format("{0} {1}", this.FirstName, this.LastName);
            }
        }
    }
}

When tested from the server side, this new property is present and working correctly. The property is NOT present on the Silverlight client side. I tried adding a metadata class with the Include attribute but since string is a primitive type, I get the following error on compilation:

The property 'FullName' in entity type 'Contact' cannot be marked with the IncludeAttribute because 'String' is not a valid entity type. Entity types cannot be a primitive type or a simple type like string or Guid.

How can I make this property available to the Silverlight client?

Add [DataMember] to your FullName property. Here are some instructions on adding methods/properties to ComplexTypes. They might apply to entities as well. Maybe using a buddy class, I haven't tried this for entities.

namespace Server.DAL.Model
{
    public partial class Contact
    {
        [DataMember]
        public string FullName
        {
            get
            {
                return string.Format("{0} {1}", this.FirstName, this.LastName);
            }
        }
    }
}

You should put the code you have shared into a file called Contact.shared.cs . The WCF RIA tooling takes this code exactly and creates a file in the Silverlight project with that code. The client-side code then has access to this member and a duplication of the code compiled in the server project.

Here is more information on shared code in the MSDN docs .

I assume you're using RIA Services? If so, the problem is that RIA Services will only copy the structure of your server side classes to the client, not any custom code you've written within properties or methods.

The good news is, the solution is simple and you're almost there. RIA Services generates the client-side classes as partial classes, just like Entity Framework does on the server. That means you can extend those classes in the Silverlight project using partial classes in exactly the manner you did on the server project.

Simply move your class from the server project to the Silverlight project, make sure your namespace matches the namespace of the class RIA Services generated for you, and you'll be good to go.

Good luck!

IMHO I think adding the property to the datamodel isn't the best approach as I like to keep the model clean (and not implementation specific so my entities work across various projects without clutter from other projects). The way I address this issue is using Extension Methods . Here is the EXACT same scenario (from my code) using extension methods instead of adding it to the datamember.

namespace <MyAppName>.Services.Entities
{
  public static class UserExtension
  {

    public static String FullName(this User user)
    {
        return String.Format("{0} {1}", user.First, user.Last);

    }
  }
}

Note the parameter of the method and its definition (ie this isn't a normal method definition). I have a "Common.dll" that holds the base routines for my application that is always referenced...so I place the extension methods in that DLL. I also ensure that the Namespace of the extension methods match exactly the namespace of the entities. If you do that, the method will appear on the entity as if it was part of the entity.

The only downside to this is you have to implement it as a method and cannot define it as a property ....which means no direct data binding. But your ViewModel or an IValueConverter can handle that pretty trivially.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM