Search code examples
c#silverlight-4.0.net-4.0riadomainservices

How do I convert multiple WCF RIA Entity types to a single type for use in a ViewModel


Apologies if there's too much/not enough detail or some of it is irelevant, it's my first post on this board:

Background

I am currently developing a Silverlight 4 and WCF RIA (SP1) based application in C# using VS2010. The stack is: SQL Server -> nHibernate -> Domain Service -> Service Agent -> Silverlight 4 Client (MVVM)

I have 4 tables in the database with the same fields in them each table holding a different 'type' of the same data.

The data should really be in one table with a type column but its a legacy database that I can't change.

I have created a POCO per table and a single domain service that handles accessing and updating the four different entity sets. These types are projected to the Silverlight Client and all the CRUD stuff works as expected.

Problem

I have a viewmodel that in which contains an ObservableCollection where T is one of the 4 entity types, however I don't want to use 4 collections to hold the different entity types. The UI will allow the user to select a type and I want to use the same collection and UI bindings to edit that data.

I have tried

  • Creating a generic domain service, but they are not supported.

  • Having the server side entity types inherit from an abstract class or implement an interface but domain services don't project abstract classes or interfaces.

  • Creating client side partial classes for the 4 projected entities that implement a client side interface. However when I try and assign the ObservableCollection<MyEntity> to ObservableCollection<MyEntityInterface> I Get the following error:

Cannot implicitly convert type 'System.Collections.ObjectModel.ObservableCollection<BusinessDomain.StandardInterestRate>' to 'System.Collections.ObjectModel.ObservableCollection<BusinessDomain.IInterestRate>'

I suspect the reason is that StandardInterestRate inherits from the RIA Entity class. Any thoughts appreciated.


Solution

  • Try

    ObservableCollection<MyEntity> entities = ...
    ObservableCollection<MyEntityInterface> iEntities = new ObservableCollection(entities.Cast<MyEntityInterface>());