Skip to main content
 首页 » 编程设计

c#之Entity Framework之MVC之将从模型中的数据库请求的数据类型(POCO 对象)转换为 View 中使用的业务对象

2024年02月13日12java哥

我正在使用 MVC 模式和 Entity Framework 4.1。我有三个项目:模型 (M)、 View (V) 和 Controller (C)。

                         M <-------- V --------> C 
                         ^                       | 
                         |_______________________| 

查看项目引用 Controller 和模式项目。 Controller 项目引用模型项目。

在模型和 View 之间还有另一个项目,比方说项目 MV,它包含一个接口(interface)和一些转换器(我将在下面解释)。模型项目引用 MV。也可以查看引用 MV。但是MV是独立的,它不引用任何项目,既不引用Model也不引用view。

在模型中,我有我的 POCO 对象(实体),它们定义了物理数据库设计。 因为我想将模型与 View 隔离,也就是说,我不希望模型依赖于 View ,模型有自己的实体, View 也有自己的实体。

例如在 View 命名空间中我有以下对象:

          public class EntityA : InterfaceMV 
          { 
             public string Property1 { get; set; } 
             public int Property2 { get; set; } 
             EntityB entityB;   <--- refrences an entity in the same namespace               
          } 

在 Model 命名空间中,我有以下对象,实际上它包含相同的属性:

         public class EntityA : InterfaceMV 
         { 
             public string Property1 { get; set; } 
             public int Property2 { get; set; } 
             EntityB entityB;  <-- foreign key to an entity in the same namespace 
         } 

我没有在模型和 View 中共享的实体,所以我的问题是当我从 View 直接访问模型以使用上下文检索一些数据时,所以从 View 我这样做:

          using (DBContext context = new DBContext()) 
          { 
              Namespace.Model.EntityA a = context.EntitiesA.Find(id); 
          } 

由于模型和 View 使用不同的实体(尽管它们具有相同的属性和相同的名称,但它们被视为不同的对象,因为它们属于不同的命名空间),我必须将数据从模型对象转换为 View 对象。此转换是使用我在上面评论的项目 MV 中包含的接口(interface) (InterfaceMV) 中的转换器(接口(interface))完成的。界面MV如下:

public interface IDataTypeConverterEntityA 
{ 
    string Property1 {get; set;} 
    int Property2 {get; set; } 
} 

而InterfaceMV是由模型中的EntityA和View中的EntityA实现的。因此,一旦我从 View 中检索模型中的数据:

         Namespace.Model.EntityA a = context.EntitiesA.Find(id); 

我必须将“a”对象(属于 Namespace.Model.EntityA 类型)转换为 View 中的等效项:

         EntityA aConverted = DataTypeConverterEntityA.Convert<Namespace.Model.EntityA, EntityA>(a); 

转换器如下所示(它在 DataTypeConverterEntityA 类中):

    public static Y Convert<T, Y>(T itemToConvert)  
         where T : new(), IDataTypeConverterEntityA  
         where Y : new(), IDataTypeConverterEntityA 
    { 
        return new Y  
                { 
                    property1 = itemToConvert.property1, 
                    property2 = itemToConvert.property2                         
                }; 
     } 

基本上,我的 POCO 实体表示我的数据库中的表,我有一些业务类,我使用转换器从一个转移到另一个。

问题如下: 1.- 由于包含interfaceMV的项目MV既不引用模型也不引用 View ,convert方法只能转换标量类型而不能转换其他类型,例如EntityB属性所以一旦在 View 中获得转换对象aConverted:

// I retrieve the matching entity from the context 
EntityA aConverted = DataTypeConverter.Convert<Namespace.Model.EntityA, EntityA>(a); 

我必须向数据库发出另一个请求以获取 EntityB,然后将其转换并最终从 View 分配给 EntityA.entityB,方法是:

// I retrieve the matching entity from the context 
Namespace.Model.EntityA a = context.EntitiesA.Find(id); 
EntityA aConverted = DataTypeConverterEntityA.Convert<Namespace.Model.EntityA, EntityA>(a); 
 
// Inject values into the entity 
Namespace.Model.EntityB b = context.EntitiesB.Find(id); 
EntityB bConverted = DataTypeConverterEntityB.Convert<Namespace.Model.EntityB, EntityB>(b); 
 
aConverted.entityB = bConverted; 

2.- 如您所见,我必须为每个实体设置一个转换器,因为从 View 来看,我无法处理从数据库请求接收到的对象,因为 View 使用自己的实体......显然我必须实现每个实体中的接口(interface),模型实体中的接口(interface)和 View 实体中的接口(interface)。

原因是我完全隔离了模型项目,它使用自己的实体, View 也使用自己的实体。

也许将实体(EntityA、EntityB、....)放在一个地方并在模型和 View 之间共享它们可以解决这个问题....避免使用数据类型转换器。

所以我的问题是:是否有更好的方法来避免每次都使用数据类型转换器?还是您认为这种架构很好,因为它将模型与 View 隔离开来?使用这种方法,如果您在 View 中更改它的实体,则模型不会受到影响,因为它有自己的实体。我的问题是每次都使用数据类型转换器....

有什么想法吗?

请您参考如下方法:

与其为每个类编写自己的转换器,不如看看 AutoMapper .它将使您免于大量编码。