开发者

Tracing my Data-Entity class and its MVC-ViewModel equivalent class match each other

I'm developing an MVC3 application with EntityFramework v4.1. According to most common MVC best practices, i should separate my data access layer objects from my view-model objects. I should not use entity objects as view-models for a good project design and for some possible XSS like security reasons. But since it is quite hard to trace the common properties between an entity class and its view-model equivalent while programming, i decided to use common interfaces both entity and view-model classes will be derived from. Such as :

public interface ICountry
{   
    int CountryId { get; set; }
    string CountryCode { get; set; }
    string CountryName { get; set; }
    string CountryLocalName { get; set; }
    ILanguage DefaultLanguage { get; set; }
}

...

/// entity class in my data access layer
public class Country : ICountry
{
    [Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CountryId { get; set; }

    [Required(AllowEmptyStrings=false), MaxLength(6)]
    public string CountryCode { get; set; }

    [Required(AllowEmptyStrings = false), MaxLength(64)]
    public string CountryName { get; set; }

    [Required(AllowEmptyStrings = false), MaxLength(64)]
    public string CountryLocalName { get; set; }

    public string APropertyOnlyForDataEntity_001 { get; set; }
    public string APropertyOnlyForDataEntity_002 { get; set; }


    // this "Language" type is from data entity classes, derived from ILanguage
    public virtual Language DefaultLanguage { get; set; }
}

...

/// View Model class in my MVC application
public class Country : ICountry
{
    [Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CountryId { get; set; }

    [Required(AllowEmptyStrings=false), MaxLength(6)]
    public string CountryCode { get; set; }

    [Required(AllowEmptyStrings = false), MaxLength(64)]
    public string CountryName { get; set; }

    [Required(AllowEmptyStrings = false), MaxLength(64)]
    public string CountryLocalName { get; set; }

    public string AnotherPropertyOnlyForViewModel_001 { get; set; }
    public string AnotherPropertyOnlyForViewModel_002 { get; set; }

    // this "Language" type is from view-model classes, derived from the same ILanguage
    public virtual Language DefaultLanguage { get; set; }
}

The problem here is, i can not use ILanguage DefaultLanguage { get; set; } in the ICountry interface, it gives build error. Even if my both "Country" classes have different "Language" typed objects, which are derived from same ILanguage interface, it says "it does not have a matching return type". It expects me to put "public virtual ILanguage DefaultLanguage { get; set; }" instead. But when i put it in that way, then my Database is not being cr开发者_运维知识库eated properly. Because, in my DataContext, i am using the Language and Country entities such as DbSet< Language > and DbSet< Country >...

What should i do ??


I believe this is the problem you're running into. There's some explanation there on how to fix it.

That said the Interface might be overkill. I prefer to map between database classes and view models by using AutoMapper. That will take everything between the two with the same name and handle it automatically, then you can add/remove extra stuff as needed. Much easier to set up.


You could cobble it together using an explicit implementation of the interface.

// this "Language" type is from view-model classes, derived from the same ILanguage
public Language DefaultLanguage { get; set; }

ILanguage ICountry.DefaultLanguage
{
    get { return this.DefaultLanguage; }
    set { this.DefaultLanguage = (Language)value; }
}

This would "make it work", but it seems to be a misuse of the language. Also, if any code sets ICountry.DefaultLanguage to an implementation of ILanguage other than Language, you'll get an exception. Given that this would fail fast, this is a good way of finding a bug early in your development.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜