Git Product home page Git Product logo

easy-dto-core's Introduction

EasyDto

EasyDto is java based library for working with DTOs (data transfer objects) of domain objects without requiring to write any Dto class or mapping logic explicitly

Declare DTO classes by simple annotations

public class Student {

    @DtoProperty
    public String name;

    @DtoProperty(value = "dept")
    public Department department;
}

Also, the field name in DTO can be customized in the annotation, otherwise the field name is used.

Derived Properties

Properties can also be derived for DTO. In this case annotate a getter with @DtoProperty annotation.

@DtoProperty("isEnrolled")
public boolean hasDepartment(){
    return department != null;
}

Support of Profiles

Often we need different types of a single domain object depending on requirement. That can addressed here using profile. Simply declare in the annotation in which profile the field should be present in the DTO. No profile declaration means all profiles. All properties are picked up when no profile is used during conversion.

public class Student {
    @DtoProperty(profile = {"WEB"})
    public String name;
}

Creating a DTO object

For a domain object of say Student class, a DTO can simply be created as following and the mapping is done for you under the hood.

Student student = new Student("John", new Department(1, "CST"));
Dto<Student> dto = Dto.from(student);

Of course, the target profile can be provided and even a custom DtoConverter instance can be passed. Otherwise the default is used.

Dto<Student> dto = Dto.from(student, "REST");

Creating Domain Object from DTO

Simply pass an instance of the target domain object to the DTO to map. This is type-safe.

Dto<Student> dto = deserialize();// some deserialzation logic
Student student = new Student();
dto.map(student);

Similary profile and custom DtoDeConverter can be provided.

Serialization

One can not work with DTOs without some sort of serialization. Currently, there is library support for jackson library only i.e., DTOs can be serialized/deserialized to json. Support for other formats can be extended.

Working with Jackson

Create an ObjectMapper instance and register the out-of-the-box serialization module.

ObjectMapper mapper = new ObjectMapper();
Registerer.registerModules(mapper);

Note to use same instance of ObjectMapper throughout, modules are registered in Mapper level. PS - ObjectMapper is thread-safe. For Jackson support import EasyDto Jackson.

Working with Spring

As long as you are using Jackson for serialization or write your own support for your format, you can use it seamlessly with Spring.

@Autowired
private ObjectMapper mapper;

// ....
Registerer.registerModules(mapper);

Simply autowire the object mapper, the one spring has created and will use internally, and register the modules. And you can run your spring application without writing the DTO class or any mapping logic of any sort. Following is a sample Spring Rest Endpoint.

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/{id}")
    public Dto<Student> get(@PathVariable Long id) {
        Dto<Student> dto = Dto.from(studentService.getStudent(id));
        return dto;
    }

    @PostMapping
    public void post(@RequestBody Dto<Student> dto) {
        Student newStudent = new Student();
        dto.map(newStudent);
        studentService.saveStudent(newStudent);
    }

}

Installation

clone this repository and build locally to install in local .m2. Currently, only maven is supported.

mvn clean install

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

easy-dto-core's People

Contributors

rahul-acr avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

xo-6

easy-dto-core's Issues

Implementing maybe support for Lists

It would be cool if we could implement the following: List entity = new ArrayList<>();. For example, I have a UserEntity with this property:

@DtoProperty(profile="SESSION")
List<Session> sessions = new ArrayList<>();

And in the session entity, I have a DTO property:

@DtoProperty(profile="SESSION")
public UUID sessionId;

Because when I get this with the Spring REST API, it looks like this:

"sessions": {}

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.