简体   繁体   English

如何使用存储库模式将 EF 对象转换为 WCF 模型

[英]How to transform EF Objects to WCF Models using Repository pattern

I'm trying to build my first (Rest?) WCF service on .NET with entity framework 6 following Repository pattern.我正在尝试使用实体框架 6 遵循存储库模式在 .NET 上构建我的第一个(Rest?)WCF 服务。

So it is supposed that I have to expose only simple object from the service, not the entire EF classes, right?所以应该我只需要从服务中公开简单的对象,而不是整个 EF 类,对吗?

So I build a simple Model Like this, that represent some field from a database table:所以我构建了一个像这样的简单模型,它代表数据库表中的某个字段:

[DataContract]
public class FormasPagoModel
{
    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string FormaPago { get; set; }        
}

This is the data type returned by my WCF service method.这是我的 WCF 服务方法返回的数据类型。 So I whant a method GetbyID on my service.所以我想要在我的服务上使用 GetbyID 方法。 To do so, i create an interface:为此,我创建了一个界面:

public interface IRepository<T> where T:class
{
    T GetEntityByID(int Key);
}

and a Repository abstract class:和一个 Repository 抽象类:

public abstract class Repository<T> : IRepository<T> where T : class
{
    protected readonly OhmioNETEntities context = new OhmioNETEntities();

    public T GetEntityByID(int Key)
    {         
        return context.Set<T>().Find(Key);
    }
}

and a concrete implementation (ANX_FormasPago is my EF Class that is linked to a database table):和一个具体的实现(ANX_FormasPago 是我链接到数据库表的 EF 类):

public class FormasPagoRep : Repository<ANX_FormasPago>
{
}    

Finally my WCF Service class最后我的 WCF 服务类

public class WCFService
{
    public FormasPagoModel FormasPago_GetbyID(int Key)
    {            
        ANX_FormasPago EFEntity = new FormasPagoRep().GetEntityByID(Key);

        return new FormasPagoModel
        {
            ID = EFEntity.ID_FormaPago,
            FormaPago = EFEntity.FormaPago,
        };
    }
}

As you can see, internally I get an EF class of type ANX_FormasPago and transform it to FormasPagoModel.如您所见,我在内部获得了一个类型为 ANX_FormasPago 的 EF 类并将其转换为 FormasPagoModel。 Of course if I need for instance FormasPago_Save, i need to write the exact oposite code that is transform my Model(FormasPagoModel) to EF class(ANX_FormasPago)当然,如果我需要例如 FormasPago_Save,我需要编写将我的模型(FormasPagoModel)转换为 EF 类(ANX_FormasPago)的确切相反代码

With this code, I end up having, for each database table:有了这段代码,我最终得到了每个数据库表:

A) An EF class. A) EF 类。

B) A Simplified Model class. B) 一个简化的模型类。

C) A concrete Repository. C) 一个具体的存储库。

D) A WCF Service method for each method. D) 每个方法的 WCF 服务方法。

So, I'm I in the right path here?那么,我在正确的道路上吗? Or am i just complicating things up.或者我只是把事情复杂化了。

I think, you're almost there.我想,你快到了。 In my opinion you are actually missing one layer ( Business logic ).在我看来,您实际上缺少一层(业务逻辑)。 It's not the responsibility of your WCF service to get data from repository and convert it to a different type.您的 WCF 服务不负责从存储库中获取数据并将其转换为其他类型。 The reason is that in many cases the creation of the final model object can be much more complex.原因是在许多情况下,最终模型对象的创建可能要复杂得多。 I wouldn't like to clutter the WCF service with that.我不想用它弄乱 WCF 服务。

Ideally the architecture would look like that:理想情况下,架构应该是这样的:

WCF --> Business Logic --> Repository --> Entity Framework WCF --> 业务逻辑 --> 存储库 --> 实体框架

Every layer can only talk to the layer directly on it's right .每一层都只能跟在层直接在它的右边

  • Repository returns filtered EF entities存储库返回过滤的 EF 实体
  • Business processes them (can use multiple repositories, perform calculations and other stuff)业务处理它们(可以使用多个存储库,执行计算和其他东西)
  • Business returns simple DTO objects ( Business should not reference WCF)业务返回简单的 DTO 对象(业务不应引用 WCF)
  • WCF grabs DTOs and maps them to WCF models (I'd use AutoMApper for that btw). WCF抓取 DTO 并将它们映射到WCF模型(顺便说一句,我会使用AutoMApper )。 This mapping is necessary as Business doesn't reference WCF components (DataContract and DataMember attributes are not accessible in Business ).此映射是必要的,因为Business不引用WCF组件(DataContract 和 DataMember 属性在Business中不可访问)。
  • WFC releases models to the web WFC将模型发布到网络

Also I wouldn't bother to have an abstract / base / generic repository.我也不会费心拥有一个抽象/基础/通用存储库。 Sooner or later you'll end up having some ugly workarounds to handle not compatible entities ( Refused bequest problem).迟早你会遇到一些丑陋的解决方法来处理不兼容的实体(拒绝遗赠问题)。

Ultimately you'll end up having even more classes than you proposes, but each of them will have single responsibility .最终,您将拥有比您建议的更多的类,但每个类都将 承担单一职责 They'll be simpler, easier to test and more reusable (eg you can replace WCF layer and use WPF or MVC instead without touching different layers).它们将更简单、更易于测试和更可重用(例如,您可以替换 WCF 层并使用 WPF 或 MVC 代替,而无需接触不同的层)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在EF中使用存储库模式使用存储过程和复杂类型? - How to use Store Procedure and Complex types using Repository Pattern in EF? EF如何使用.include()和使用存储库模式查询更多实体 - EF How to query more entities with .include() and using repository pattern 如何使用存储库模式访问EF中的导航属性 - How to access navigation properties in EF using repository pattern EF 4:如何使用具有存储库模式的MVC正确更新DbContext中的对象 - EF 4: How to properly update object in DbContext using MVC with repository pattern 使用EF或NHibernate的存储库模式中的层次结构 - Hierarchical structure in repository pattern using EF or NHibernate 使用具有存储库模式的View-Models - Using View-Models with Repository pattern 使用存储库模式实现 WCF 数据服务 - Implement WCF Data Service using the Repository Pattern 如何为 EF 和存储库模式设置项目结构 - How to setup project structure for EF and repository pattern 如何使用存储库模式、服务模式、UnitOfWork、ORM(EF、NHibernate 等)构建带有 ASP MVC 的项目? - How to structure projects with ASP MVC using Repository Pattern, Service Pattern, UnitOfWork, ORM (EF, NHibernate etc..)? EF和存储库模式 - EF and Repository Pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM