2

I have a service contract:

[DataContract]
public class Something
{
    [DataMember]
    public MyEnum SomeEnumMember {get;set;}
}

Some of our devs are doing this sort of thing:

public Something()
{
    SomeEnumMember = MyEnum.SecondEnumValue;
}

I am of the opinion that constructor logic does not belong in our service contracts since that code won't work if you use "Add Service Reference..." and are working with proxy classes generated by Visual Studio.

Internally, we are using Castle DynamicProxy as shown here. However, I would prefer our devs avoid constructor logic in service contract classes in case we can't use DynamicProxy for some reason.

So: does constructor logic have a place in these classes, or as a matter of best practice should we look at them as more of a DTO and implement them with no behavior?

4
  • I may be wrong, but when deserializing the object, if the incoming XML (or other content) doesn't have value for a property, then it won't modify it from it's original value. (On the other hand, I may be wrong and will always assign the type's default value) If I'm right, then assigning a value in the constructor can set the default value if none is explicitly provided. Other than that, I don't see a good case for it off the top of my head. I don't think logic/methods should be in the class if its purpose is strictly to transfer information between domains. May 11, 2012 at 19:51
  • Thanks. This is my line of thinking exactly: " I don't think logic/methods should be in the class if its purpose is strictly to transfer information between domains" May 11, 2012 at 19:54
  • We ran some tests this morning and the enums were defaulting to the first member -- 0, as expected -- even though we had initialized it in the constructor to something else. May 11, 2012 at 19:56
  • Just to add, it may depend on your usage of your class. Is it used strictly for data transfer via the service and converted to another domain object for use by your application, then it likely serves no purpose. If you're reusing it throughout your application and depend on default values for your properties other than the type-default, then it can make sense (just as it would for any other class). However, I would recommend you refactor your architecture to avoid dependencies on the data transfer object. May 11, 2012 at 19:59

2 Answers 2

1

It may only make a difference when creating the objects in the first place to transfer via your services. If you wish to have the default value of your SomeEnumMember to be different when instantiating to be transmitted as a convenience, it may make sense:

var mySomething = new Something();
mySomething.SomeEnumMember = MyEnum.SecondEnumValue; //this line may be omitted if it's set automatically by the constructor
SendData(mySomething);

On the receiving side, it will make no difference as the values (I think, and from your tests) are explicitly set by the sender anyway. Additionally, it will be extra/wasted work by your receivers instantiating/deserializing the objects, but most likely this is a negligible overhead.

In general, your data transfer objects shouldn't contain logic anyway. Perhaps depending on your application architecture if you're using this object in a bunch of places other than just transferring information, it might make sense. Though I would question that architecture: I like to keep the objects concerned with client-server communication abstracted away from the rest of my application architecture. That way I can freely make changes to that (specialized serialization, architecture changes, etc.) without seriously affecting the rest of my application(s).

3
  • 1
    Good points. I see these as DTOs only and do not want any behavior in them at all. I believe there are two reasons why I'm seeing this, 1) we are using DynamicProxy so the constructor logic is getting hit, 2) it is seen as a workaround for default enum values. I just don't want to get six months down the road and someone ties into one of our services without using DynamicProxy and wonder why things are behaving strangely. Well, the init code wasn't hit! May 11, 2012 at 20:11
  • 1
    @JackT.Colton That's what I figured too. Also, any logic you add to your DataContract objects means logic that may need to be tested or could cause errors. Having your DataContract objects throw exceptions in the service is not pleasant. Your case is pretty trivial though and unlikely to cause any issues. It may however have developers thinking to add more non-trivial code (validation, business logic, logging, etc.) to the DataContract objects which can cause headaches in the future or unexpected behaviour. May 11, 2012 at 20:56
  • Exactly. Thanks for the discussion. May 11, 2012 at 21:09
1

I agree with you, I don't think constructor logic belongs there and I never actually worked with a wcf that had constructor logic in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.