Skip to content

Instantly share code, notes, and snippets.

@stevenwaterman

stevenwaterman/Example.java Secret

Created Dec 27, 2019
Embed
What would you like to do?
Perfect Spring DTO (ours)
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();
}
}
}
@brunodrugowick

This comment has been minimized.

Copy link

@brunodrugowick brunodrugowick commented Jun 19, 2020

I think you could expand to show an example just a bit more complex, like, returning the following representation:

{
    "postalCode": "13020",
    "addressLine_1": "Cocada Street, 123456",
    "addressLine_2": "Neighborhood",
    "region": "Region1",
    "city": {
      "name": "São José dos Campos",
      "province": "São Paulo"
    }
}

I ended up with something like this:

package dev.drugowick.algaworks.algafoodapi.api.model.dtoPattern;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

public enum AddressDTO {;

    private interface PostalCode { String getPostalCode(); }
    private interface AddressLine_1 { String getAddressLine_1(); }
    private interface AddressLine_2 { String getAddressLine_2(); }
    private interface Region { String getRegion(); }
    private interface City { CityDTO.Default getCity(); }

    private enum CityDTO {;

        private interface Name { String getName(); }
        private interface Province { String getProvince(); }

        @Getter @Setter @NoArgsConstructor
        private static class Default implements Name, Province {
            String name;
            String province;
        }

    }

    public enum Response {;
        @Getter @Setter @NoArgsConstructor
        public static class Default implements PostalCode, AddressLine_1, AddressLine_2, Region, City {
            String postalCode;
            String addressLine_1;
            String addressLine_2;
            String region;
            CityDTO.Default city;
        }
    }
}

Does this makes sense?

@brunodrugowick

This comment has been minimized.

Copy link

@brunodrugowick brunodrugowick commented Jun 19, 2020

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. =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.