官术网_书友最值得收藏!

Repository implementation

Now we can implement the repository pattern, as learned about in this chapter. To start with, you will first create the two interfaces, Repository and ReadOnlyRepository. The ReadOnlyRepository interface will be used to provide an abstraction for read-only operations, whereas the Repository abstraction will be used to perform all types of operations:

public interface ReadOnlyRepository<TE, T> { 
 
    boolean contains(T id); 
 
    TE get(T id); 
 
    Collection<TE> getAll(); 
}

The following diagram contains the OTRS domain repositories:

Domain repositories

Based on the defined interface, we could create the abstraction of the Repository, which would execute additional operations such as adding, removing, and updating:

public interface Repository<TE, T> extends ReadOnlyRepository<TE, T> {  
    void add(TE entity);  
    void remove(T id);  
    void update(TE entity); 
} 

The Repository abstraction, as defined previously, could be implemented, in a way that suits you, to persist your objects. The change in persistence code, which is a part of the infrastructure layer, won't impact on your domain layer code, as the contract and abstraction are defined by the domain layer. The domain layer uses abstraction classes and interfaces that remove the use of the direct concrete class, and provides loose coupling. For demonstration purposes, we could simply use the map that remains in the memory to persist the objects:

public interface RestaurantRepository<Restaurant, String> extends Repository<Restaurant, String> { 
 
    boolean containsName(String name); 
} 
 
public class InMemRestaurantRepository implements RestaurantRepository<Restaurant, String> { 
 
    private Map<String, Restaurant> entities; 
 
    public InMemRestaurantRepository() { 
        entities = new HashMap(); 
    } 
 
    @Override 
    public boolean containsName(String name) { 
        return entities.containsKey(name); 
    } 
 
    @Override 
    public void add(Restaurant entity) { 
        entities.put(entity.getName(), entity); 
    } 
 
    @Override 
    public void remove(String id) { 
        if (entities.containsKey(id)) { 
            entities.remove(id); 
        } 
    } 
 
    @Override 
    public void update(Restaurant entity) { 
        if (entities.containsKey(entity.getName())) { 
            entities.put(entity.getName(), entity); 
        } 
    } 
 
    @Override 
    public boolean contains(String id) { 
        throw new UnsupportedOperationException("Not supported yet."); 
//To change body of generated methods, choose Tools | Templates. } @Override public Entity get(String id) { throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates. } @Override public Collection<Restaurant> getAll() { return entities.values(); } }
主站蜘蛛池模板: 酉阳| 敖汉旗| 怀宁县| 德保县| 黄浦区| 肥东县| 亳州市| 茶陵县| 长海县| 和平县| 奈曼旗| 清河县| 买车| 越西县| 镶黄旗| 东莞市| 乌兰察布市| 延寿县| 陈巴尔虎旗| 凌海市| 正镶白旗| 克拉玛依市| 贞丰县| 梅州市| 临澧县| 海口市| 高碑店市| 巩留县| 黔东| 宣威市| 丹寨县| 奇台县| 长兴县| 汝州市| 增城市| 苍溪县| 砚山县| 蓬安县| 阿拉善右旗| 镇宁| 屯门区|