Gripsoft
Gripsoft

Reputation: 2600

Type conversion among data and business layers

I have two standard layers i.e. Business and domain layers which contain similar objects and now i want to provide some standard way of converting objects back and fro from both sides. I found three options.

  1. Using Implicit or Explicit conversion.
    • I basically don't like them as they don't provide a clean interface and no proper indications are available while converting.
  2. Using the TypeConverter class
    • This provide the methods I am looking for but it forces me to inherit from this class which I would love to do if it is an interface but not a class. As it will restrict my options.
  3. Using IConvertable
    • This just allow to convert to basic types but not to the custom types.
  4. Creating my own interface
    • I can create my own interface which allow my proper validation as well cleaner interface like IConvertable but I was just thinking is there any other option available rather then reinventing the wheel

So what other methods are available in .NET ?

Upvotes: 1

Views: 157

Answers (2)

ybo
ybo

Reputation: 17152

I think AutoMapper is what you're looking for.

Upvotes: 1

GvS
GvS

Reputation: 52518

You could use reflection to copy properties with the same name (and datatype) from one class to the other.

If you want to go one step further, you can use reflection to generate classes that copy one class to the other.

Upvotes: 0

Related Questions