To me, naming is a critical concept in Software Engineering. For that reason, I'm a huge fan of Design Patterns because it gives me (what I think are) appropriate names for commonly found patterns/objects.

When creating objects whose purpose it is to convert one object to another, and vice versa, I usually use the name "map":

IDictionary<long, User> _userIdMap = new // ...

public class UserMapper
{
    UserModel ToModel(UserViewModelModel viewModelModel) { ... }
    UserViewModelModel ToViewModelModel(UserModel model) { ... }
}

However recently I joined a project where objects of this nature are called Transformers and I understand why they could also be called this.

It is my understanding that the term "map" is appropriate when there is a clear bidirectional one-to-one relationship: ObjectA can be "mapped" to ObjectB if ObjectB can be "mapped" to ObjectA. If the relationship is one-way then (my understanding is) that it's not truly a "map" in classic Computer Science terms.

Is there a standard, traditional distinction between mappers, transformers, converters, or other (not already mentioned here) names for objects that change one object into another, and if so, what delineations or rules of thumb should be considered when naming these objects?

有帮助吗?

解决方案

In computer science, a "Map" is used to associate a known value/entity to another known value/entity e.g as in your question, ObjectA can be mapped to ObjectB.

A "Transform" is used to convert/transform any acceptable value into another using a function e.g. a "transform" to calculate x^2; x could be any value.

Hence as a "rule of thumb" a map should be used if a finite set of objects/values have a clear relationship, whereas a transform should be used if dealing with a non-finite set of values/entities where a value should be calculated using a function.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top