Skip to content

Getting started

Bert Loedeman edited this page Feb 1, 2016 · 12 revisions

Getting started

What is AutoMapperTS?

This is an JavaScript functionality port of the original AutoMapper implementation for .NET. In line with the original implementation, AutoMapperTS uses a fluent configuration API to define an object-object mapping strategy. The basic idea of the fluent configuration syntax implementation are based on the jsAutoMapper implementation.

The pursuit of this implementation is to reach a look-and-feel which is as comparable as possible to the original AutoMapper implementation. For type safety (both for writing and maintaining the AutoMapper source code), this library port has been implemented in TypeScript. All JavaScript files in the repository therefore are TypeScript generated files, mainly here for sake of the automapper-ts NPM package and automapper-ts Bower package.

A lot of functionality is implemented in JavaScript. Feel free to suggest additional features and/or priority. Should there be an issue with the current implementation, please let us know!

Why use AutoMapper?

Well, an excellent explanation on using AutoMapper can be found on the original project's wiki. The AutoMapperTS port brings as much as possible of the fine stuff to JavaScript - that's probably why you would want to use this library ;) .

How do I use AutoMapperTS?

You can clone the GitHub library, use the automapper-ts NPM package or use the automapper-ts Bower package to start using this library.

The AutoMapperTS implementation is created as a Singleton. AutoMapperTS registers itself on the global scope with the automapper variable. The automapper instance offers a few public functions:

automapper.initialize(configFunction: (config: IConfiguration) => void): void;
automapper.createMap(sourceKey: string, destinationKey: string);
automapper.map(sourceKey: string, destinationKey: string, sourceObject: any);

Find out more about each function by following its link:

All functionality is tested using Jasmine unit tests. In case you are wondering about an undocumented piece of functionality, you could of course have a look at the Jasmine test files, covering all functionality. Also, you can have a look at the samples section at the bottom of this page.

AutoMapperTS basic sample

Should you want to see all functionality offered by the AutoMapperTS implementation, please head over to the Jasmine unit tests covering (almost) every bit of functionality. A basic AutoMapper sample is displayed below:

var objA = { prop1: 'From A', prop2: 'From A too', prop3: 'Also from A (really)' };

automapper
    .createMap('sourceType', 'destinationType')
    .forMember('prop1', function (opts) { opts.mapFrom('prop2'); })
    .forMember('prop2', function (opts) { opts.ignore(); })
    .forSourceMember('prop3', function (opts) { opts.ignore(); })
    .forMember('prop4', function () { return 12; })

var objB = automapper.map('sourceType', 'destinationType', objA);

objB will look as follows (represented using JSON):

{
  "prop1": "From A too", 
  "prop4": 12 
}

Samples

The source code contains nearly 100% unit tests for every implemented feature. To view the unit tests, browse the source code.

You can also run the configuration samples:

Happy coding!

Clone this wiki locally