Search code examples
c#asp.net-mvcentity-frameworkautomappern-tier-architecture

How to validate Business Entity in Razor view instead of Data Entity


I am working on an ASP.NET application split in to 4 layers.

  1. Business Layer
  2. Data Access Layer
  3. WCF Services Client
  4. ASP.NET MVC5 application

I am using AutoMapper to map from domain class to business class and other way around. I am using Data Annotation:

[StringLength(250)]
[Required(ErrorMessage = "Required Title")]
[Display(Name = "Function Title")]

Which is translated in view ASP.NET Razor page, i.e. creating record by passing strongly typed and validate against that. My question is how I can achieve this where I am passing business class to view as strongly typed and I don't want data dependency on business layer nor ASP.Net MVC application?

DAL Entity

[Table("Navigation_Functions")]
public class Navigation_FunctionsEntity
{
    public Navigation_FunctionsEntity()
    {

    }
    [Key]
    public int Function_ID { get; set; }

    [StringLength(250)]
    [Required(ErrorMessage = "Required Title")]
    [Display(Name = "Function Title")]
    public string FunctionName { get; set; }

    [Required(ErrorMessage = "Required Hierarchy Level")]
    [Display(Name = "Hierarchy Level")]
    public int Hierarchy_Level { get; set; }

    public ICollection<Navigation_FunctionHierarchy> Navigation_FunctionHierarchy { get; set; }
    public ICollection<Navigation_FunctionInAction> Navigation_FunctionInAction { get; set; }
    public ICollection<Navigation_FunctionInController> Navigation_FunctionInController { get; set; }

    public ICollection<Navigation_FunctionController> Navigation_FunctionController { get; set; }
 }

Business Entity

public class Navigation_Functions
{

    public int Function_ID { get; set; }

    public string FunctionName { get; set; }

    public int Hierarchy_Level { get; set; }

    public ICollection<Navigation_FunctionHierarchy> Navigation_FunctionHierarchy { get; set; }
    public ICollection<Navigation_FunctionInAction> Navigation_FunctionInAction { get; set; }
    public ICollection<Navigation_FunctionInController> Navigation_FunctionInController { get; set; }

    public ICollection<Navigation_FunctionController> Navigation_FunctionController { get; set; }
}

Solution

  • My advice is don't organize your code into layers like this. It's a lot of hoops but there's no real advantage. In fact, I found over time that a layered architecture like this actually hinders refactoring and evolution of your codebase. Layers are for cake, not software.

    https://vimeo.com/131633177

    I also question the value of WCF in your picture too, I don't know what it adds to the solution here except adding latency and making your requests slower.