Search code examples
c#asp.netentity-frameworkmvp

In MVP pattern, how to create a Property of type List<T> in the View, where T is a Data Type generated by Entity Framework?


Premise: I'm using Entity Framework, Repository Pattern and MVP for an ASP.NET project.

Question: In MVP pattern, how to create a Property of type List<T> in the View, where T is a Data Type generated by Entity Framework?

In the code below, "TeamMember" in List<TeamMember> is a class created by Entity Framework based on the SQL Table TeamMembersTable. The issue here is I'm creating a List<T> property in my View, where T is a Data Type from my Entity Framework model. This seems to be violating separation of concerns, since the view is now dependent on Data Types from my Model layer. How to accomplish clean separation of View and Model in this scenario?

Note: In case of a simple TextBox, its easy since the data type is a .NET type like String or DataTime. However, if the DataType of property to be created in View is from the model, it seems to make View more tightly coupled with Model as far as data types are concerned.

//***************Simplified Code Sample*******************//
using Model;

namespace View
{
    public interface IEmployeeView
    {
        List<TeamMember> TeamMembers { set; }
    }
}

Solution

  • Introduce a view model which will represent your TeamMember class and pass that into your view instead e.g.

    public class TeamMemberViewModel
    {
        ...
    }
    
    public interface IEmployee
    {
        public List<TeamMemberViewModel> TeamMembers { get; set; }
    }