Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

November 10, 2009

Silverlight and RIA: Ignoring a Property in Your Business Object

Filed under: ASP.NET RIA Services,C#,Silverlight,VB.NET @ 1:22 pm

There may be times when you want RIA Services to ignore a property in your business object. This may be the case if you know you won’t ever need to access the property from your Silverlight UI or if you can’t make RIA services understand the property properly.

[To begin with an overview of Silverlight, RIA, and your business objects, start here.]

My case is the second scenario. I have yet to figure out how to make RIA Services understand my Enum property. Until I do, I want my code to compile, so I need RIA to ignore any property in my business object that is of an Enum type.

[EDIT 11/10/09: Found a solution to this. Blogged it here.]

I assumed that the DataAnnotations feature would have an attribute to mark a property to be ignored. But no. There is no attribute to do this in the DataAnnotations namespace.

Rather, the property is in the System.Web.DomainServices names. Here is the code example.

NOTE: Be sure to set a reference to System.Web.DomainServices.

In C#:

public enum CustomerTypeOption
{
    Consumer,
    Corporation,
    Education,
    Government
}

private CustomerTypeOption _CustomerType;
[Exclude()]
public CustomerTypeOption CustomerType {
    get { return _CustomerType; }
    set { _CustomerType = value; }
}

In VB:

Public Enum CustomerTypeOption
    Consumer
    Corporation
    Education
    Government
End Enum

Private _CustomerType As CustomerTypeOption
<Exclude()> _
Public Property CustomerType() As CustomerTypeOption
    Get
        Return _CustomerType
    End Get
    Set(ByVal value As CustomerTypeOption)
        _CustomerType = value
    End Set
End Property

This code resides in a Customer class like the one show here. The Exclude attribute causes RIA Services to exclude this property from its generated code.

Use this attribute any time you want a property excluded from the code generated in the Silverlight project. This property is then inaccessible from your Silverlight code.

I would have never figured this out without the help of Colin Blair from the Microsoft Silverlight forums, and from additional information I found on Hatim’s Blog post.  Thanks Colin and Hatim!

Enjoy!

Edited 12/7/09: Corrected the credits in the last paragraph.

3 Comments

  1.   Colin Blair — December 7, 2009 @ 12:36 pm    Reply

    Thanks for the mention, but technically I learned about the Exclude attribute last December when I was using what was then called Alexandria. I am not sure how you conflated Hatim’s blog post with me.

  2.   DeborahK — December 8, 2009 @ 12:46 am    Reply

    Hi Colin –

    I relooked at your reply, my follow up question, and the follow up answer and just realized that it was not you that provided the follow-up answer and link that ultimately took me to Hatim’s blog. I will correct my post.

    Thanks for pointing out this error and for all of your help.

  3.   Hatim — December 8, 2009 @ 5:19 am    Reply

    Thanks for mentioning my post Deborah. I am glad I can help.

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2014 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs