-3

I, am trying to implement a generic method to convert one object to another object in c#.

Here is the some code I tried

 public interface IDTOMapper<TSource, TDestination>
        {
            TDestination Convert(TSource source_object);
        }


    public class DtoMapper<TSource, TDestination> : IDTOMapper<TSource, TDestination>
        {
            public TDestination Convert(TSource source_object)
            {
           // How to write the code to convert one class object to another

            }
        }

The code which I used inside the convert method has lots of errors.Can anyone let me know how to implement with any class type objects. Some example I found has specific class object conversion.

When I google I found there is auto-mapper library I, don't want to use auto-mapper library. Just want to write custom generic method

Some of the reference

Best Practices for mapping one object to another

https://softwareengineering.stackexchange.com/questions/301580/best-practices-regarding-type-mapping-and-extension-methods

Generic method to map objects of different types

https://www.codeproject.com/Tips/781202/Csharp-Custom-Mapper

San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • 2
    What _exactly_ do you mean by "convert"? If they inherit from each other, you could just cast. If not, why not? Why do you need to do this at all? This is a pretty strange request; under normal circumstances you shouldn't need to do this. Or, alternatively, a mapper is _exactly_ what you need. – Vilx- Dec 21 '18 at 00:11
  • They are not inherit from each other. One is the database model and another is the view model which almost has the same properties. I know that auto-mapper does that thing but I don't want to use auto-mapper for that. – San Jaisy Dec 21 '18 at 00:17
  • So, what problem are you actually having. You haven't asked a question. – AndrewP Dec 21 '18 at 00:22
  • The code which I used inside the convert method that code doesn't work. I have updated the code. – San Jaisy Dec 21 '18 at 00:27
  • You can use reflection for this, so only matching properties. Which any automapper does ultimately. You can find it here https://stackoverflow.com/questions/2142650/reflection-help-set-properties-on-object-based-on-another-object/2142700#2142700. But if your properties are not same, then I think there is no way you can have a generic implementation and you have to have separate implementation for each case. – PM. Dec 21 '18 at 00:32

1 Answers1

1

You say you don't want to use an auto-mapper, but what you're trying to write is the same thing. You're just going to reinvent the wheel and write your own auto-mapper.

I strongly suggest using an existing library like AutoMapper. It'll be faster and have less bugs.

If not that, then I myself would avoid trying to automate this task. For each ViewModel class, add a method CopyFromBusiness and write the copying code there yourself by hand. Yes, it's quite repetitive, but writing your own auto-mapper will be nearly as much work and the result will invariably be poorer than an existing library. A manually written CopyFromBusiness method will be the fastest possible option (at runtime) and in the future the code will be a lot easier to maintain because there will not be any hidden magic.

Vilx-
  • 104,512
  • 87
  • 279
  • 422