Git Product home page Git Product logo

columbus's Introduction

Columbus - A simple Java object mapper

Build Status Coverage Status

In a pure layered application UI-, domain-, and data-entities are completely separated classes. While being beneficial, the process of mapping between these enitities can easily become rather mind numbing work.

This is where Columbus enters. Columbus uses annotations to map fields between classes.

How to use

Columbus relies on field annotations and reflections to perform the mapping. The Mapping annotation contains two elements. A class reference and a field name to which the annotated field should be mapped to.

@Mapping(clazz = Person.class, field="name")
private String owner;

When the fields which should be mapped together have been annotated correctly only two methods are needed:

Columbus.mapTo(sourceObject, destinationObject);  // Retrieves the mapped fields from the sourceObject and writes them to the correct field in the destinationObject

Columbus.mapFrom(destinationObject, sourceObject);  // Retrieves the mapped fields from sourceObject and writes them to the correct field in the destinationObject

One-to-one mapping

A common use case is when you want mapping between your UI entities and your domain entities. However in most cases field names stay the same. Here is an example how to create a mapping between Movie domain-entity and a MovieViewModel UI-entity.

Movie

public class Movie {

  private String title;

  private float cost;

  private int rating;
  
}

MovieViewModel (Note that only one of the classes needs to be annotated.)

public class MovieViewModel {

  @Mapping(clazz = Movie.class, field="title")
  private String title;

  @Mapping(clazz = Movie.class, field="cost")
  private float cost;
  
  @Mapping(clazz = Movie.class, field="rating")
  private int rating;
  
}

Now instances of these two classes can be mapped with each other:

// Create a Movie instance (this is probably created somewhere else)
Movie movie = new Movie();
movie.setTitle("Star Wars: The Force Awakens");
movie.setCoset(123.50f);
movie.setRating(5);

// Create the destionation object where the mapped values will be written to
MovieViewModel movieViewModel = new MovieViewModel();

Columbus.mapFrom(movieViewModel, movie);  // Retrieve the mapped fields from movie to moviewViewModel

The movieViewModel now contains the values of the mapped fields.

If you want to go the other way around it's just as simple:

MovieViewModel movieViewModel = new MovieViewModel();
movieViewModel.setTitle("Star Wars: The Force Awakens");
movieViewModel.setCoset(123.50f);
movieViewModel.setRating(5);

Movie movie = new Movie();

Columbus.mapTo(movieViewModel, movie);  // Writes the mapped fields from movieViewModel to movie

One-to-many mapping

In some cases you might find yourself in a situation where you want to combine two or more entities into one. With Columbus this is just as simple as in the One-to-one case. As an example, imagine that we want to combine information from a Movie- and a Person-entity to create a TicketViewModel. The Movie-entity is defined as above.

Person

public class Movie {

  private String name;

  private float age;
  
}

TicketViewModel

public class TicketViewModel {

  @Mapping(clazz = Movie.class, field="title")
  private String movie;
  
  @Mapping(clazz = Movie.class, field="cost")
  private float cost;
  
  @Mapping(clazz = Person.class, field="name")
  private String boughtBy;
  
}

Mapping the values from a person and a movie object you can now simply be done by:

Movie movie = new Movie();
movie.setTitle("Star Wars: The Force Awakens");
movie.setCost(123.50f);

Person person = new Person();
person.setName("Simon Evertsson");

TicketViewModel ticketViewModel = new TicketViewModel();

Columbus.mapFrom(ticketViewModel, movie, person);  // mapFrom and mapTo accepts varArgs to support one-to-many mappings

And to write the corresponding fields back to a Movie and a Merson instance:

TicketViewModel ticketViewModel = new TicketViewModel();
ticketViewModel.setMovie("Star Wars: The Force Awakens");
ticketViewModel.setCost(123.50f);
ticketViewModel.boughtBy("Simon Evertsson");

Movie movie = new Movie();
Person person = new Person();

Columbus.mapTo(ticketViewModel, movie, person);

License

The MIT License (MIT)

Copyright (c) 2016 Simon Evertsson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

columbus's People

Contributors

simonejenstam avatar

Watchers

 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.