数据契约与行为 [英] DataContracts with behavior

查看:72
本文介绍了数据契约与行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有多糟?我已经阅读了无数的文章,并且以前从未创建具有行为的抽象DataContracts,但是这样做似乎可以解决我遇到的一个问题,这将阻止我在各地创建工厂来确定子类实现.我的问题是,如果我决定在数据合同中添加行为,是否会受到惩罚?当然,它们不能被消耗,并且可以在调用存储库调用和数据之前持久地执行特定于该子类类型的某些操作.我可以为每个子类创建管理器"类,但这使我回到工厂,并且我正在尝试一种更加多态的方法.预先感谢.

How bad is it? I have read countless articles and never created abstract DataContracts with behavior before, but it seems that doing so will solve an issue I am having that will prevent me from creating factories everywhere to determine a subclass implementation. My question is, will I be punished if I decide to add behavior to my data contracts? Of course they can't be consumed and are there to perform certain operations specific to that subclass type before invoking repository calls and data is persisted. I can create "Manager" classes for each subclass but that puts me back at factories and I am trying a more polymorphic approach. Thanks in advance.

推荐答案

您可以将所需的所有行为添加到数据合同中.您应该清楚地记录以下事实:该行为对客户不可见,否则某人以后会感到失望.还要记录一个事实,必须小心,不要将任何与实现相关的数据添加到数据协定中,因为它不是要传递给客户端的任何内容.

You can add all the behavior you want to your data contracts. You should clearly document the fact that the behavior won't be visible to clients, or someone will be disappointed later on. Also document the fact that care must be taken to not add any implementation-dependent data to the data contract, since it's not anything you want to pass to the clients.

总而言之,我认为最好让数据合同成为数据合同,并把行为排除在外.

All in all, I think you'd be better off to let data contracts be data contracts, and to leave the behavior out of them.

这篇关于数据契约与行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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