Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Using Business Entities with Silverlight front end and standard Class Library Business Layers.
1 replies. Latest Post by egorush on September 3, 2009.
(0)
egorush
Member
2 points
4 Posts
09-03-2009 6:17 PM |
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.
09-03-2009 11:19 PM |
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.