Object Factories

Object Factory instance are used to specify how new object instances are created; use this mapping object type when your destination type requires use of a special constructor, a custom factory method, or some other special instantiation circumstances.
To define a custom object factory, implement the ma.glasnost.orika.ObjectFactory<T> interface in your implementation class, like so:

public class PersonFactory implements ObjectFactory<Person> {

   public Person create(Object source, Type<Person> destinationType) {
      Person person = new Person();
      // set the default address
      person.setAdress(new Address("Morocco", "Casablanca"));
      return person;
   }
}

Register object factories

To be eligible for use by Orika, an instance of your custom object factory should be registered with the type it creates, like so:

mapperFactory.registerObjectFactory(new PersonFactory(), Person.class);