Search code examples
asp.net-mvcwcfasp.net-mvc-3validationwcf-client

Validation approach on MVC3 CRUD application with EF4-based WCF as backend


I develop a simple MVC3 CRUD application - simple controllers / views, which uses WCF service for CRUD data access.

The WCF uses EF4.1 with DbContext, and simple CRUD-style methods: ListEntities, GetEntity(ID), AddEntity (entity), DeleteEntity(ID)

If I develop the MVC application directly with EF, code first, I can annotate properties in the entity classes with validation attributes, and the MVC application will automatically recognize validation errors and report them in the UI when I try to save and a validation error occurs (e.g. a required field is not set).

But in my application I don't use this approach and I face two problems:

  1. My entities in the WCF are generated from the EDMX, which in turn was also generated from the database. So I cannot actually add to them any data validation annotation attributes, because they'll vanish as soon as the entities will be regenerated from the EDMX. Is there any solution to this?

  2. Since my client (MVC app) does not share the data contract classes with WCF (for clear separation), but instead it is generated form service reference, even if I find a way to add data annotation attributes to server-side data contract classes, will they be recognized and recreated when the data contract proxy class is created on client side?
    So how could I made the MVC application to use client side validation and error message reporting for validation failures when binding to entities exposed by WCF service as data contracts?

One idea I have is, on client side, to create derived classes for all entities exposed as data contracts, and apply annotation attributes to them to desired properties. But this doesn't looks like a good solution to me, because with this I create a logic "coupling" between UI client and the WCF service / data layer (forcing UI to know about data more than it should do - by putting BL logic in client).

Can anyone give me some suggestions on how to handle those this situation?

Thanks


Solution

  • 1: Yes you can add validation using the System.ComponentModel.DataAnnotations.MetaDataType.

    I answered this question at MVC Partial Model Updates

    2a: What you can do is create a seperate Class Library Assembly that contains all the interfaces (with or without additional MetaDataTypes) and use that on both the WCF service and the MVC application. After you add the reference to your MVC application, when adding the WCF Service reference, you can match the WCF Service DataContacts directly to the interfaces in the Assembly. One Caveat is that both the WCF service and MVC application are dependant on the Assembly (some might consider this tightly coupled) but this should be ok because you are only tightly coupling at the interface level, and whether or not you choose to allow VS to recreate it's own interfaces/classes or reuse what you already created in the Assembly it boils down to the same thing in my opinion.

    2b: If you decide not to use a Class Library, I'm pretty sure that the service reference classes are partial, and you can simply create another .cs file with partial classes and add the interfaces as I described in part 1 to the partial classes.

    Update

    I am currently using Entity Framework to access my database. Entity Framework, like WCF References, classes are Auto-Generated classes will look something similar to:

    [EdmEntityTypeAttribute(NamespaceName="MyNameSpace", Name="Info ")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class Info : EntityObject
    {
        public static Info CreateInfo (global::System.Int32 id)
        {
            Info info= new Info ();
            info.Id = id;
            return info;
        }
    
        public string Name { get; set; }
        public string FavoriteColor { get; set; }       
    
        // etc etc
    
    }
    

    In a separate file with the same namespace as the previous partial class, I have created:

    [SomeAttribute1]
    [AnotherAttribute2]
    public partial class Info: IInfo
    {
    }
    

    So now my auto-generated class is not only based on an Interface I created IInfo so the actual methods are not exposed (because my datatier in MVC returns interfaces), but it also has Attributes (for Data Annotations or whatever).

    What I would suggest is instead of putting your data annotations directly on your WCF Service reference class is to use the MetedataType DataAnnotations. This allows you to separate the actual data object with the data annotations validations. Especially helpful if you want to use the same data class with different validations based on whatever (maybe administrators don't have to have a valid favorite color).

    For example:

    public interface NormalUser
    {
      [Required]
      string Name { get; set; }
      [Required]
      string FavoriteColor { get; set; }
    }
    
    public interface AdminUser
    {
      [Required]
      string Name { get; set; }
      string FavoriteColor { get; set; }
    }
    
    [MetadataType(typeof(INormalUser))
    public class NormalUserInfo : Info { }
    
    [MetadataType(typeof(IAdminUser))
    public class AdminUserInfo : Info { }
    

    In this example we have two different classes NormaUserInfo and AdminUserInfo which both have different validations. Each of them have inherited from Info so they are valid models that can be passed into the WCF Service.