0

I've been using Omu.ValueInjecter for a while now but only to 'inject' values into a newly created object.

    public K MapNew(T source) {

        K target = new K();

        target.InjectFrom(source);

        return target;
    }

The code above works well, mapping values from the source to the newly created target.

However, what I'm trying (and failing to do) is to map values from a source object to an existing target. The code below is similar to the one above with the difference being that I'm not creating a new object.

    public T MapToTarget(T target, K source) {
        return target.InjectFrom(source) as T;
    }

Is this possible? The code above merely returns the target with its properties un-modified.

EDIT

Note, in both cases both source and target objects have the same property names.

NOTE

Thanks to Omu for all the help, much appreciated.

Daniel Hollinrake
  • 1,768
  • 2
  • 19
  • 39

1 Answers1

1

the InjectFrom() method matches the properties with same name and type, so if there are properties that match they will be modified

with valueinjecter you only inject into existing objects

Omu
  • 69,856
  • 92
  • 277
  • 407
  • The property names of the source and target are the same, but in the second bit of code I want the values of the source to be written into the target. That isn't happening. I'm wondering if I'm either doing something wrong or it can't be done. – Daniel Hollinrake Aug 07 '14 at 12:16
  • @DanielHollinrake names and types, it has to be the exact name and type; if you want to transform from one type to another you should create a custom valueinjection – Omu Aug 07 '14 at 14:09
  • Hiya, just to confirm that the properties I'm expecting to match are the exact name and type. There are some properties on the target object that don't exist on the source but in this case I don't want, or expect, a match. – Daniel Hollinrake Aug 07 '14 at 15:18
  • in this case it should work, put the source and target classes in your question, or upload a zip with a mini project showing the problem – Omu Aug 07 '14 at 17:49
  • Helllo, Thank you very much for replying. I'll create a test project and let you know how I get on. If there's problems I'll post the code. As you can see from the snippits I've included above my current use of ValueInjecter uses it in a generic mapping service. – Daniel Hollinrake Aug 08 '14 at 08:28
  • Hi Omu, Thank you for your comments I think the problem lies with me and the types on the objects I'm using. When I map a Nullable property to a bool then the value is copied, however when I map from a bool to a Nullable then the mapping doesn't seem to take place. – Daniel Hollinrake Aug 08 '14 at 13:57
  • 1
    there are examples of nullable injections here: https://valueinjecter.codeplex.com/wikipage?title=Useful%20injections&referringTitle=Home – Omu Aug 08 '14 at 15:31