Skip to main content

Microsoft Silverlight

Answered Question Using Business Entities with Silverlight front end and standard Class Library Business Layers.RSS Feed

(0)

egorush
egorush

Member

Member

2 points

4 Posts

Using Business Entities with Silverlight front end and standard Class Library Business Layers.

I'm having a problem working with business entities with Silverlight and the Business Layer. I totally understand that the CLR's are different between Silverlight and standard .NET assemblies.

 Here is my situation. In a standard  .NET class library I have a business entity called Client, it contains things like Name, Address etc. Now I want to use the same Client object in Silverlight. So I create a Silverlight class library and simply link to the file that has the Client class, this way I can share the code. My issue comes in with meta data for validation and display. They are not equal between the two types of projects. So the following code will not compile in my standard .NET class library.

        private string _FirstName;
        [Display(Name = "First Name", Description = "The first name of the prospect.")]
        [Required(ErrorMessage="The first name is required.")]
        [StringLength(50,ErrorMessage="The first name can not be more than 50 characters.")]
        public string FirstName
        {
            get { return _FirstName; }
            set
            {
                Validator.ValidateProperty(value, new ValidationContext(this,null,null) {MemberName = "FirstName"});
                _FirstName = value;
                NotifyPropertyChanged("FirstName");
            }
        }

 The Display Attribute will not compile. Is the only work around for this to actually duplicate all my business entities  for Silverlight and Standard .Net? Or is there another way to accomplish this? I know I could just manually add display and description on the UI  elements themselves, but these objects are used on many pages, they amount of duplication and potential for error would be insane and just feels wrong.

 Any help would be appreciated.

Artin.

 

EDIT:
Forgot to mention Validator.ValidateProperty also wont compile. Not too muck work to externalize and just raise exceptions manually.
 

 

Artin
abannayan@mac.com

egorush
egorush

Member

Member

2 points

4 Posts

Answered Question

Re: Using Business Entities with Silverlight front end and standard Class Library Business Layers.

Should have done more googling. I solved the problem by using the #if SILVERLIGHT directive in my good. Not the greatest solution but it works.

Now I have the class  linked to a Silverlight class library with the following code for a property

        private string _FirstName;
        #if SILVERLIGHT
        [Display(Name = "First Name", Description = "The first name of the prospect.")]
        #endif
        [Required(ErrorMessage="The first name is required.")]
        [StringLength(50,ErrorMessage="The first name can not be more than 50 characters.")]
        public string FirstName
        {
            get { return _FirstName; }
            set
            {
                #if SILVERLIGHT
                Validator.ValidateProperty(value, new ValidationContext(this,null,null) {MemberName = "FirstName"});
                #endif
                _FirstName = value;
                NotifyPropertyChanged("FirstName");
            }
        }

 I'm still disappointed in Microsofts  solution approach. They've built really rich UI elements that look great but the code required to implement them really doesn't work well for complex solutions. You can't do validation using their controls without exception handling in properties, which is painfully in many cases. Solutions like this shouldnt be arbitrary, there should also be a way of externalising the validation from the business entitiy and still be able to take advantange of they UI elements that were built. And trying to validate all properties with the click of a button (ie saving) doesn't even exists. You have to basically rebind all the controls to force validation. Even ASP.NET  buttons had a property called CauseValidation, I can't fathom why it would not be included here.

 Anyways enough of my rant. Immediate problem solved.

Artin
abannayan@mac.com
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities