1

I am trying to implement the repository pattern to query users and the corresponding settings associated to each user. I am having a repository interface which has the crud operations to be implemented by each and every repository.

import java.util.List;

public interface Repository<T> {

    void add(T item);
    void add(Iterable<T> items);
    void update(T item);
    void remove(T item);
    void remove(Specification specification);
    List<T> query(Specification specification);

}

I am reading about the specification pattern but i do not understand how to apply the concept to my usecase.

I have two repositories which implements the Repository interface. My classes will be having the below operations which translate into the crud operations as mentioned in the interface

GetSettingsForUserIDMatchingSettingName(string userid, string settingname);
CreateSettingForProfile(string userid, Setting setting);
Create(string userid, Setting setting);
Update(string userid, Setting setting); 
GetAllProfiles();
GetProfilesMatchingUserID(string userid);
GetSettingsForUserID(string userid);        
GetProfilesForUserIDWithSettingName(string userid, string settingname);

How to create my specification class or interface so that my specification caters to all the use cases.

I am referring to the below github link to implement but i am lost.

https://github.com/patrikfr/specification/blob/master/specification/src/com/granular8/specification/genericspec/Specification.java

Kindly, help.

wandermonk
  • 6,856
  • 6
  • 43
  • 93
  • There are many ways to implement specs. Maybe this helps you https://dzone.com/articles/java-using-specification – Héctor Nov 03 '17 at 10:47

1 Answers1

0

Specification pattern can help if you are querying or if you need it for filtering the objects that you want to delete from a certain repository thats fine. There is a good implementation of the pattern in C#. Hopefully you translate it to Java since it already supports lambda expressions. https://github.com/jnicolau/NSpecifications

jnicolau
  • 106
  • 3