Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Terra CircleCI

A minimalistic library for object hydration. Useful for data to object reconstruction mechanics.

Installation

Just add the following configuration to your build.gradle file

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    compile 'com.github.transferwise:terra:1.0.0'
}

Usage

Given a value object like the following

import java.util.regex.Pattern;

class Email {
    private static final Pattern FORMAT = 
        Pattern.compile("^[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
    
    private final String value;
    
    Email(String aValue) {
        if (!FORMAT.matcher(aValue).matches()) {
            throw new RuntimeException("Email " + aValue + " is incorrect");
        }
        value = aValue;
    }
    
    public String getValue() {
        return value;
    }
}

Imagine you'd want to rebuild the object, bypassing the constructor validation.

import static com.transferwise.terra.Terra.hydrate;

class Example {
    public static void main(String[] args) {
        Email e = hydrate(Email.class, "value", "not+valid+email@example.com");
        
        System.out.println(e.getValue());
    }
}

Why would we disable the constructor?

This is a very common behaviour when you reconstruct objects from data stored in your persistence mechanism. The validation rules usually live in the write model, not in the read model. Business constraints change all the time and you might end up with data that does not follow these rules at a given moment.

About

A minimalistic library for object hydration. Useful for data to object reconstruction mechanics.

Topics

Resources

License

Packages

No packages published

Languages

You can’t perform that action at this time.