序列化的WCF接口接口、序列化、WCF

2023-09-08 08:33:31 作者:农夫三拳有点疼

我设计的,有一个接口和两个继承的类的应用程序:

I designed an application that has an interface and two inherited classes:

interface IPerson {}
class Man:IPerson{}
class Woman:IPerson{}

现在,我写了一个看起来像这样的方法:

Now, I wrote a method that looks like this:

public IPerson GetPeron(int idNumber);

此方法获得一个ID号,并会在数据库的数量。如果这个数字belogns一个人,就会创建一个新好男人的实例,从数据库数据被放入新的实例,最后的实例返回。如果给定ID属于同去的女人

This method gets an ID number and looks for that number in the DB. If that number belogns to a man, a new Man instance is created, data from the DB is being put into the new instance and finally the instance returns. The same goes if the given ID belongs to a woman

现在我想的方法放置未在客户端上,但一个WCF服务器上。的问题是,不能返回是一个接口了。

Now I want that method to be placed not on the client side but on a WCF server. The problem is that the return type can't be an Interface anymore.

显而易见的解决方案是创建三个方法:

The obvious solution is to create three methods:

一个。获得一个ID,并返回重新present人类型的枚举:

A. Get an ID and return an enum that represent the person type:

enum PersonType { Man,Woman }
PersonType GetType(int personID);

乙。获取一个ID,并返回一个人:

B. Gets an ID and return a Man:

Man GetMan(int personID);

℃。获取一个ID,并返回一个女人:

C. Gets an ID and return a Woman:

Woman GetWoman(int personID);

我认为这样的糟糕,没有更好的建议?

I think that's kind a lousy, any better suggestion?

推荐答案

与WCF的工作很遗憾,当你不能有接口,抽象基类或类没有默认的(空的)构造函数。这困扰了我很多,但你有WCF和良好的面向对象的做法之间做出选择......

Unfortunatelly when working with WCF you cannot have interfaces, abstract base classes or classes without the default (empty) constructor. That bothered me a lot, but you have to choose between WCF and good OO practices...

我来是要创建辅助的 WCF-interfaceable 的具体类对WCF的合同,我需要他们的实例之间隐含的转换运营商采用最好的和相应的企业类。

The best I came to was to create auxiliary wcf-interfaceable concrete classes to use on WCF contracts and the operators I needed to implicit convert between their instances and the corresponding business classes.