Silverlight and RIA: Ignoring a Property in Your Business Object

Posted Tue, Nov 10 2009 11:22 by Deborah Kurata

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, who found it on Hatim's Blog post.  Thanks Colin and Hatim!

Enjoy!

Comments

# Silverlight and RIA: Enum Properties

Tuesday, November 10, 2009 2:58 PM by Deborah's Developer MindScape

I mentioned in this prior post that RIA Services did not know how to handle properties that have an Enum

Leave a Comment

(required) 
(required) 
(optional)
(required)