简体   繁体   English

WCF和工厂设计模式

[英]WCF and factory design pattern

I have created a pretty complicated app using some sort of "factory" design pattern.我使用某种“工厂”设计模式创建了一个非常复杂的应用程序。 That way, i have interface defining a class, i have a class implementing the interface and i have a static class with static method for creating an instance. That way, i have interface defining a class, i have a class implementing the interface and i have a static class with static method for creating an instance. That static method (factory method) returns interface type, and only factory class can access my type class.那个 static 方法(工厂方法)返回接口类型,只有工厂 class 可以访问我的类型 class。 So, if i have private class A, i'll have public interface IA and static factory class Factory.所以,如果我有私有 class A,我将有公共接口 IA 和 static 工厂 class 工厂。 "Factory" class is the only one that can access "A", and it returns type "IA". “工厂”class 是唯一可以访问“A”的,它返回类型“IA”。 That way, entire project is working using interfaces (for example "IA"), passing interfaces as arguments and so long.这样,整个项目都在使用接口(例如“IA”),将接口传递为 arguments 等等。 Simply put, my app is NEVER using class "A" except in factory class.简而言之,我的应用程序从不使用 class “A”,除非在工厂 class 中。

Now, the tricky part - i have decided to split my app to client and server part.现在,棘手的部分 - 我决定将我的应用程序拆分为客户端和服务器部分。 Those to parts will communicate using WCF.那些到部件将使用 WCF 进行通信。 But i have a problem - with WCF i can't use interfaces as return types / arguments like i did in WHOLE CODE SO FAR.但我有一个问题 - 使用 WCF 我不能使用接口作为返回类型 / arguments 就像我在整个代码中所做的那样。

Please is there ANY way to make my project work without changing the very base of my "know-only-interface" code?请问有什么方法可以让我的项目工作而不改变我的“只知道接口”代码的基础吗?

Create a class with the same properties as in the interface IA .创建一个与接口IA具有相同属性的 class 。 You can specify the return type of the service operation to be the type of this class.您可以将服务操作的返回类型指定为该 class 的类型。 In the service operation, create an object (DTO) of this class and map the values from the object of type IA .在服务操作中,创建此 class 的 object (DTO) 和 map 的值来自IA类型的值。 Now you can just return this DTO.现在您可以返回这个 DTO。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM