-
-
Save stevenwaterman/28f0ec03a0cc0370a3b0c9351917976b to your computer and use it in GitHub Desktop.
Perfect Spring DTO (ours)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.scottlogic.springdto; | |
import javax.validation.constraints.NotBlank; | |
import javax.validation.constraints.Positive; | |
import lombok.Value; | |
public class Example { | |
public enum ProductDto{; | |
public interface Id{ | |
/** | |
* The identifier for this product. | |
* IDs are unique amongst products but may be | |
* repeated across other entity types | |
*/ | |
@Positive | |
Long getId(); | |
} | |
public interface Name{ | |
/** | |
* The product's name as should appear on the site. | |
* Any capitalisation here should be stored in the | |
* database but any filtering should be case-insensitive. | |
*/ | |
@NotBlank | |
String getName(); | |
} | |
public interface Price{ | |
/** | |
* The amount that we sell a product for in GBP. | |
* This includes our margin but should not include | |
* any delivery fees or VAT. | |
*/ | |
@Positive | |
Double getPrice(); | |
} | |
public interface Cost{ | |
/** | |
* The amount that it costs us to purchase this product | |
* For the amount we sell a product for, see {@link Price}. | |
* <b>This data is confidential</b> | |
*/ | |
@Positive | |
Double getCost(); | |
} | |
public enum Request{; | |
@Value public static class Create implements Name, Price, Cost{ | |
String name; | |
Double price; | |
Double cost; | |
} | |
} | |
public enum Response{; | |
@Value public static class Public implements Id, Name, Price{ | |
Long id; | |
String name; | |
Double price; | |
} | |
@Value public static class Private implements Id, Name, Price, Cost{ | |
Long id; | |
String name; | |
Double price; | |
Double cost; | |
} | |
} | |
private static <DTO extends Price & Cost> Double getMarkup(DTO dto){ | |
return (dto.getPrice() - dto.getCost()) / dto.getCost(); | |
} | |
} | |
} |
Actually, I think I'm mixing things up... you don't mention transformation of the domain objects to DTOs.
I still want to hear what you have to say about the previous structure, so I'll keep the comment. =)
I have the same question of @brunodrugowick, how do you go from a model to a dto like this?
I might suggest that records would be an nicer fit removing the need for the static classes and the lombok annotations while maintaining immutability. For even more conciseness take a look at the @RecordInterface part of RecordBuilder here https://github.com/Randgalt/record-builder#RecordInterface-Example
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you could expand to show an example just a bit more complex, like, returning the following representation:
I ended up with something like this:
Does this makes sense?