Let's say I own a service and publish a contract like
public class MyObject
{
public int Foo { get; }
public string Bar { set; }
}
I imagine that it is not proper have "behavior" in a data contract, things like
public class MyObject
{
public int Foo { get; }
public string Bar { set; }
public void DoSomeAlgorithmWithMyProperties() { … }
}
In other words, data contracts are supposed to be just value bags.
So my question is how I create behavior on a such an object. One way I can see is to just create a separate mirroring object, like
public class MyObjectInternal
{
public int Foo { get; }
public string Bar { set; }
public class MyObjectInternal(int foo, string bar)
{
this.Foo = foo;
this.Bar = bar;
}
public void DoSomeAlgorithmWithMyProperties() { … }
}
Another is inheritance
public class MyObjectInternal : MyObject
{
public class MyObjectInternal(int foo, string bar)
{
this.Foo = foo;
this.Bar = bar;
}
public void DoSomeAlgorithmWithMyProperties() { … }
}
and other possibility could be to just completely separate out behavior and data by having separate classes that operation on the values, like
public static class MyObjectAlgorithmDoer
{
public static void DoSomeAlgorithmWithMyProperties(MyObject myObject) { … }
}
Aucun commentaire:
Enregistrer un commentaire