Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataContracts with behavior

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.

like image 296
user24985 Avatar asked Jul 13 '09 17:07

user24985


People also ask

What is a data contract?

Data contracts involve large quantities of technical metadata. To document your data pipelines and data products, you must have a clear description of your data sources, all transformations your data has undergone, and how you ultimately deliver the data.

What are data contracts in data mesh?

Data contracts are a relatively new yet important addition to data mesh architecture, providing transparency for data usage and dependencies. Focus on technical stability and standardization as you first begin to use data contracts, then use a lessons-learned process as you iterate.

What is a behavior contract and how does it work?

During the weekdays, students spend most of their time inside classrooms anyway. On that note, a behavior contract is planned. This marks as a written agreement form between the child, the parent, the teacher, and the school.

What is a data contract in WCF?

A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged. Windows Communication Foundation (WCF) uses a serialization engine called the Data Contract Serializer by default to serialize and deserialize data (convert it to and from XML).


3 Answers

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.

like image 138
John Saunders Avatar answered Oct 22 '22 14:10

John Saunders


Why can't you create your data contract (MyDataContract) in a classic fashion, just data fields, nothing else, and then derive your behavior class from it?

public class BehaviorialClass : MyDataContract
{
.....
}

That way, you have a nice, clean separation of concerns, your data contract isn't "polluted" by behavior it can't really deal with anyway.....

Marc

like image 24
marc_s Avatar answered Oct 22 '22 14:10

marc_s


A good compromise to putting behavior directly in your DataContracts would be define behaviors as extension methods in either the same assembly as your Contracts or a different assembly entirely. Optionally, extension methods can be placed in a separate namespace from the contracts to further insulate the separation of data and behavior.

That way your Contracts are kept clean but at the same time, .NET consumers of your contracts would have an easy way to import additional functionality relating to those DataContracts.

like image 24
Ray Vernagus Avatar answered Oct 22 '22 14:10

Ray Vernagus