Giter VIP home page Giter VIP logo

valueinjecter's Introduction

get via nuget ValueInjecter or download here

####usage

var customerInput = Mapper.Map<CustomerInput>(customer); 

or like this:

var customerInput = Mapper.Map<Customer, CustomerInput>(customer); 

(useful when working with EF proxy objects)

by default it will only map properties with the exact same name and type (this can be changed)

####custom maps can be added, like this:

Mapper.AddMap<Customer, CustomerInput>(src =>
{
    var res = new CustomerInput();
    res.InjectFrom(src); // maps properties with same name and type
    res.FullName = src.FirstName + " " + src.LastName;
    return res;
});

####InjectFrom InjectFrom<TInjection>(source) is used to map using a convention, when TInjection is not specified it will map properties with exact same name and type

it's used like this:

target.InjectFrom(source);
target.InjectFrom<Injection>(source);
target.InjectFrom(new Injection(parameters), source);
target.InjectFrom<Injection>(); // without source

you can create you own injections by inheriting LoopInjection, PropertyInjection and other base injections

see some examples of custom injections here: [injections examples] (https://github.com/omuleanu/ValueInjecter/wiki/custom-injections-examples)

####Additional parameters an additional parameter can be set when mapping:

var customer = Mapper.Map<Customer>(foo, new MyClass { Title = "hi" });

you can use this parameter in AddMap like this:

Mapper.AddMap<Foo, Customer>((src, tag) =>
    {
        var par = (MyClass)tag;
        var res = new Customer { LastName = par.Title };
        ...
        return res;
    });

when using InjectFrom additional parameters can be sent to the injection:

    res.InjectFrom(new LoopInjection(new[] { "FirstName" }), customer); 

in this case LoopInjection will ignore "FirstName" property; you can add private fields to your custom injections and give them value via the constructor as shown above

####Flattening and unflattening you can use FlatLoopInjection and UnflatLoopInjection directly or inherit them, you can also use the UberFlatter class in you custom injections, have look at the source code for these injections.

####Default map By default Mapper.Map will only map properties with the exact same name and type, this can be changed by setting Mapper.DefaultMap, here's an example:

    Mapper.DefaultMap = (src, resType, tag) =>
    {
        var res = Activator.CreateInstance(resType);
        res.InjectFrom(src);
        return res;
    };

####Default InjectFrom You can change the default injection by setting

    StaticValueInjecter.DefaultInjection = new MyInjection();

####Multiple mappers Multiple mappers with different configurations can be used by creating multiple instances of MapperInstance

	var mapper1 = new MapperInstance();
	mapper1.AddMap<Customer, Customer>(o => new Customer { FirstName = "mapper1" });
	
	var mapper2 = new MapperInstance();
	mapper2.AddMap<Customer, Customer>(o => new Customer { FirstName = "mapper2" });	

	var m1 = mapper1.Map<Customer>(customer);
	var m2 = mapper2.Map<Customer>(customer);

you could store the instance in a static member, or use your IoC Container

####Samples there's samples in the source code for winforms, ASP.net web-forms, DAL, and wpf

deep cloning sample here, and the CloneInjection

for ASP.net MVC see http://prodinner.codeplex.com

questions: http://stackoverflow.com/questions/tagged/valueinjecter

chat: https://gitter.im/omuleanu/ValueInjecter

valueinjecter's People

Contributors

omuleanu avatar amitbhatnagar24 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.