[英]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 .每一层都只能跟在层直接在它的右边。
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.