Usage

fooBar.InjectFrom(foo)
      .InjectFrom(bar);
or like this
 IValueInjecter injecter = new ValueInjecter();

 injecter.Inject(fooBar, foo);
 injecter.Inject(fooBar, bar);
as a result the properties from fooBar with same name an type as in foo and bar will be correspondingly filled in with values from foo and bar.

or something like this using a custom injection (your own convention and data transformation algorithm)
personViewModel.InjectFrom<CountryToLookup>(person);

a bigger example
viewModel.InjectFrom(entity)
                .InjectFrom<CountryToLookup>(entity)
                .InjectFrom<AnythingElseYouMightImagine>(entity)
                .InjectFrom(new StuffInjection(stuffRepository), anotherEntity); 

CountryToLookup, AnythingElseYouMightImagine, StuffInjection are ValueInjections created by the user (more info below)

The ValueInjection

The Value Injection is the algorithm that takes the values from the specific Properties (if it has properties, if it's an int/string etc. than just take the value) of the source object and inserts them into the properties of the target object. It chooses from which properties of the source object to take the values and where to put them in the source object.

Example

   a.InjectFrom(b);
   //it's the same as doing a.InjectFrom<LoopValueInjection>(b);

in here the ValueInjection LoopValueInjection takes each property of the source object (a) and looks for a property with the same name and type in the target object (a), if it finds one then it sets its value with the value from the property of the source object (a)

You can create value injections by extending one of the following classes:
  • ValueInjection
  • LoopValueInjection
  • LoopValueInjection<TSourceProperty, TTargetProperty>
  • FlatLoopValueInjection
  • FlatLoopValueInjection<TSourceProperty, TTargetProperty>
  • UnflatLoopValueInjection
  • UnflatLoopValueInjection<TSourceProperty, TTargetProperty>
  • KnownSourceValueInjection<TSource>
  • KnownTargetValueInjection<TTarget>

to see how to do that go here:

step by step explanation
Getting started
flattening
unflattening
Data access layer (ORM) with the Value Injecter

Last edited Jan 21, 2011 at 9:05 AM by o, version 29

Comments

No comments yet.