Where should I place functions to convert between types?

I have two instances in the application where I need to implement functionality to convert an object from one type to another. One, to go from a domain object to another domain object, the other to go from a domain object to XmlDocument.

First I set it to include ToXml() and ToMyOtherDO() within DomainObject1 and DomainObject2 respectively.

ToXml functionality is not very specific for the implementation of DomainObject1 and probably can live anywhere. ToMyOtherDO() has functionality very specific to the type in which it lives. Both perform type conversions, but are completely different.

My question is: have I put this functionality in the right place? Would this functionality fit better in a helper class? Or some place - perhaps overriding the explicit casting operator?

+4
source share
4 answers

I usually like to use this functionality in extension methods .

EDIT : Explanation
I usually prefer to use this function in extension methods, because I don’t feel that the class that needs to be "transformed" really needs to know how to do the conversion. This provides some isolation. You can easily change the format of your XML without changing the object that you are converting to XML (especially useful when the object you want to convert is in a separate assembly).

In the end, you will come across a situation when you want to transform a domain object into something that it really does not need to know about. For example, perhaps you want to convert a domain object to a Json.NET JObject . The assembly containing your domain object does not need a reference to the Json.NET library if you used extension methods to handle the conversion in a separate assembly.

Extension methods are great, especially when working with the Entity Framework. The code for entity classes is auto-generated, so you cannot just add ToXML() or ToJObject() to the class.

These are only my preferences, and I do not always use extension methods to work with such functions. This is just another great tool in your arsenal to solve these problems.

+4
source

I would put it in a static class as extension methods. This way you can call it as an instance method, but it does not clutter up the domain object code.

0
source

The ToXml() function must be placed in a helper class if it is not object specific.

I would suggest implementing ToMyOtherDO () as an extension method of your class, since it is specific to it.

 public void ToMyOtherDo(this MyClass object1) { //Do stuff to object1 } 

Then you can call myobject.ToMyOtherDo() (treating myobject as a MyClass object)

0
source

If ToXml not of type, I would do as blachniet and others suggested, and put it in the extension method.

If ToMyOtherDO uses only publicly available elements, then the extension method is a good place for it. However, if ToMyOtherDO requires a private member, then it must be the type from which it is converted.

0
source

Source: https://habr.com/ru/post/1400220/


All Articles