In this post I teach you how can you add a computed property in WCF ria services
Customers have become very demanding these days, but thanks to the technology that we can satisfy them. I met one of our clients for the new project. The project was complex in the sense that it involved lot of calculations. The prime requirement of the client was that the end user must immediately see the output of the calculations once all the required input is provided by them. My first choice for any LOB apps is Silverlight 4 + WCF Ria services. So the big question was that how can I get this done.
After doing a bit research I found out that it was pretty straight forward with WCF Ria Services framework. So here we go I will outline the solution with sample scenario.
The scenario is pretty simple. We have customer Entity mapped to Customer table in the database. Following are its columns.
Now here is the twist we need to calculate the Age of the customer. As this property needs to be computed we don’t have it in our database but this should be calculated after user enters date of birth. So to accomplish this follow the steps mentioned below
- Add a new partial class of the same name in your client project. Please ensure that this class namespace is same as your Domain Service namespace.
- This class must be inherited from the Entity class available in System.ServiceModel.DomainServices.Client namespace.
- Add a read only property “Age” of integer type. Write your age calculation logic in the get block of this property.
- Raise the property event when the DateOfBirth property is changed.
Hope you would find this useful. Awaiting your valuable feedback.