简体   繁体   English

WCF和实体框架。 如何为所有实体创建服务层

[英]WCF and Entity Framework . How to Create a Service Layer for all entities - Generically

Ok so I am a bit lazy here. 好的,所以我在这里有点懒。 I want a Generic WCF service Layer to support my database functionality. 我希望通用WCF服务层支持我的数据库功能。 I have a repository pattern for DB access - but in WCF from what I am reading it seems if I want to access each of these Entities - I need to create a Service Contract for each entity (I guess multiple endpoints.) As I do not want multiple hosts. 我有一个用于数据库访问的存储库模式-但是从WCF看来,如果我想访问这些实体中的每个实体,则需要为每个实体创建服务合同(我想有多个端点)。想要多个主机。 WCF Data Services only supports webHTTPBinding and not Net.Tcp Binding. WCF数据服务仅支持webHTTPBinding,不支持Net.Tcp绑定。 The DAL EF data POCO's are shared between client and service host. 客户端和服务主机之间共享DAL EF数据POCO。 So is it possible to use Generics since the types are shared in both client and server? 因为类型在客户端和服务器中共享,所以可以使用泛型吗? If so a small simple example should be enough to get me on my way. 如果是这样的话,一个简单的例子就足以让我步入正轨。

Basically what you need to do here is break everything down into seperate libraries. 基本上,您需要在此处将所有内容分解为单独的库。 You need to create seperate classes for the Service Contract, Service Interface, Data Layer and a Data Transfer Objects library. 您需要为服务合同,服务接口,数据层和数据传输对象库创建单独的类。

You do this so that the Service Interface library can be shared on the client side and used to build a proxy using the ChannelFactory object and being passed a reference to the interface. 这样做是为了可以在客户端共享服务接口库,并使用ChannelFactory对象将其传递给接口引用,以用于构建代理。 (Dont use the "Add Service Reference" option in Visual Studio - build it yourself manually) (不要在Visual Studio中使用“添加服务引用”选项-自己手动构建)

Doing this will enable to customise the proxy creation. 这样做可以自定义代理创建。 What you cant do is make generic functions to access the service methods since the whole point of SOAP and WCF is that the methods form a solid contract between client and server. 您不能做的是使通用函数访问服务方法,因为SOAP和WCF的全部要点是,这些方法在客户端和服务器之间形成了牢固的契约。 However, doing it this way will at least provide you with a way of sharing some of the burden between client and server in terms of code writing. 但是,这样做至少可以为您提供一种在代码编写方面分担客户端和服务器之间的负担的方法。

Miguel Castro offers up a good article here that explains the method: www.codemag.com/article/0809101 Miguel Castro在这里提供了一篇很好的文章来说明方法:www.codemag.com/article/0809101

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

相关问题 删除实体框架中的所有实体 - Delete all entities in Entity Framework WCF数据服务:启用分页时如何查询所有实体? - WCF Data Service : How to query ALL entities when Paging is enabled? 如何避免在我的服务层上引用Entity Framework? - How to avoid the need to reference Entity Framework on my service layer? 实体框架 - 一般地保持枚举? - Entity Framework - Persisting Enums Generically? 实体框架如何更新具有相同值的所有实体 - entity framework How to update all entities with the same value 如何急切地在Entity Framework中动态加载所有引用的实体 - How to eagerly load all referenced entities dynamically in Entity Framework 如何在Entity Framework 5.0生成的所有实体中添加Serializable属性 - How to add Serializable attributes in all entities generated by Entity Framework 5.0 如何在 Entity Framework LINQ To Entities 中进行联合? - How can I do a Union all in Entity Framework LINQ To Entities? 实体框架:创建一个实体,附加子实体,做一些事情然后保留实体……如何? - Entity Framework: Create an entity, attach child entities, do some stuff then persist entities… how? 如何使用实体框架在ASP MVC 3中为相关实体创建有意义的路由? - How to create meaningful route in ASP MVC 3 with Entity Framework for related Entities?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM