WCF - 用漂亮的相同数据的多个合同,服务合同 [英] WCF - multiple service contracts using pretty same data contracts

查看:169
本文介绍了WCF - 用漂亮的相同数据的多个合同,服务合同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF大师的新问题。

I have a new question for WCF gurus.

所以,我有一个类用户这是接近从我使用的数据库操作数据库的用户表示。现在,我想有一个使用这个类作为数据契约2个不同的服务合同,但每个以自己的方式......我的意思是,

So, I have a class User which is close to the 'User' representation from the DB which I use for database operations. Now, I would like to have 2 different service contracts that use this class as data contract, but each in their own way... I mean,

public class DBLayer
{
    void InsertUsers(List<User> userList)
    {
        // both 'PropertyVisibleForService1' and 'PropertyVisibleForService2'
        // are used HERE to be inserted into their columns 
    }
}

[DataContract]
public class User
{
  [DataMember] public string PropertyVisibleOnlyForService1{...}
  [DataMember] public string PropertyVisibleOnlyForService2{...}
}

[ServiceContract]
public interface IService1  
{   
   List<User> GetUsers();  // user with 'PropertyVisibleOnlyForService1' inside
}

[ServiceContract]
public interface IService2  
{   
    List<User> GetUsers(); // user with 'PropertyVisibleOnlyForService2' inside 
}



用户

那么,这个想法是,每个服务会得到一个不同的用户,用户的子集。请记住,我想使用用户的是DB的操作,这将是我的选择,以实现这一目标?我真的需要创建不同的数据合同或有另一种更聪明的方法?

So, the idea is that each service will get a different kind of user, subset of 'User'. Keeping in mind that I want to use the 'User' as is for DB operations, what would be my options to achieve this? Do I really need to create different data contracts or is there another smarter way?

最好的将不仅给我的解决方案,同时也解释了我的一些最佳做法。和替代

Best would be to not only give me the solution, but also to explain me some best practices and alternatives.

感谢您提前

EDIT1:
我添加了一个虚拟DBLayer类这里更好地了解为什么我觉得继承可能无法在这种情况下,良好的。

I added a dummy DBLayer class here for a better overview and why I think the inheritance may not be good in this case.

一个解决方案是有另一个 UserForService1 UserForService2 数据合约,将映射从/末到用户',但我想来看一些其他问题。

A solution would be of having another 'UserForService1' and 'UserForService2' as data contracts which would map at the end from/into an 'User' but I wanted some other points of view.

EDIT2:非常好的文章这让我在这种情况下:的 http://bloggingabout.net/blogs/vagif/archive/2009/03/29/iextensibledataobject-is-not-只换向后compatibility.aspx

Very good article which helped me in this case: http://bloggingabout.net/blogs/vagif/archive/2009/03/29/iextensibledataobject-is-not-only-for-backward-compatibility.aspx

推荐答案

您可以创建单独的DTO的每一个服务,但你的情况会实际上是理想的 Decorator模式的:

You could create separate DTO's for each service but your case would actually be ideal for a Decorator pattern:

[DataContract]
public class UserForService1 : User
{
     private User mUser;
     public UserForService1(User u)
     {
         mUser = u;
     }

     //expose only properties you'd like the user of this data contract to see
     [DataMember]
     public string SomeProperty
     {
         get
         {
            //always call into the 'wrapped' object
            return mUser.SomeProperty;
         }
         set
         {
            mUser.SomeProperty = value;
         }
     }
     // etc...
}

和服务2类似的代码,即公开只有你关心什么有...

and for Service2 similar code, that exposes only what you care for there...

这篇关于WCF - 用漂亮的相同数据的多个合同,服务合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆