Search code examples
c#silverlightentity-frameworkwcf-ria-services

Alternative to Interface w/ Ria Services


All of my Entities will have the following properties:

Employee CreatedBy { get; set; }
Employee ModifiedBy { get; set; }
DateTime CreatedDate { get; set; }
DateTime ModifiedDate { get; set; }

This is for a very large application and the entities are all pulled from different databases and, therefore, are in different .edmx files.

Mostly, these will be displayed in a DataGrid and I want to have a tooltip display all of that information. Employee is a View that is in every database.

What I would normally do, would be to create an interface IEmployee that the Employee entity from each namespace would implement. Then I would create an IToolTipEnabled interface with the properties listed above, and I would implement that wherever needed.

Then, I would be able to use a single converter on the Silverlight side for the tooltip content. As it is, I would have to basically create a new converter for each entity type, in order to get the cast right.

Is there a good way to do this?

Thanks for taking the time to read this and for any help/insight you might be able to provide!

EDIT: ken2k's solution is definitely the correct one, simply place the interfaces in a '.shared.cs' file and in that same shared file (or another shared file) put partial class definitions of the class implementing the interface. That's all it takes.


Solution

  • If some of your entities share common properties and you need to get those properties without having to know the type of the entity, then interfaces are indeed a good idea.

    For example:

    public interface IDatedEntity
    {
        DateTime CreationDate { get; set; }
        DateTime UpdateDate { get; set; }
    }
    
    public partial class User : IDatedEntity
    {
        public DateTime CreationDate { get; set; }
        public DateTime UpdateDate { get; set; }
        ...
    }
    ...
    public partial class Customer : IDatedEntity
    {
        public DateTime CreationDate { get; set; }
        public DateTime UpdateDate { get; set; }
        ...
    }
    

    So you can use a single converter without having to know the actual entity:

    // Returns the number of days since last update of an entity
    public class DateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {       
            if (value is IDatedEntity)
            {
                return DateTime.UtcNow.Substract(((IDatedEntity)value).UpdateDate).TotalDays;
            }
    
            return 0;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }