2015-06-14 94 views
2

我试图改进我的Android持久层用于多个应用程序。存储库模式与存储库工厂

是我迄今所做的是要建立一个基础库的抽象类,也是一个基础库接口,完整的代码可以点击这里:https://github.com/grmaciel/android-repository-ormlite

接口:现在所有

public interface IRepository<T, Id> { 
    public void save(T entity) throws SQLException; 
    public void saveBatch(List<T> entities) throws Exception; 
    public List<T> queryAll() throws SQLException; 
    public T findById(Id id) throws SQLException; 
    public void delete(T entity) throws SQLException; 
} 

我存储库扩展我的基础知识库,如下所示:

public class DependencyRepository extends BaseRepository<Dependency> 
            implements IDependenceyRepository { 
    public DependencyRepository(Context context) { 
     super(context); 
    } 
} 

我现在想实现的目标是创建一个存储库工厂,允许pe德隆到不一定要建立自己的仓库遍布与new Instance()

的地方我所做的就是创建一个Singleton工厂必须与具有阶级关系的容器进行初始化,像这样:

public abstract class BaseRepositoryContainer { 
    private final Context context; 

    public BaseRepositoryContainer(Context context) { 
     this.context = context; 
    } 

    public abstract <T extends IRepository> Map<Class<T>, Class<T>> getRepositoriesMap(); 

    public Context getContext() { 
     return context; 
    } 
} 

工厂:

public class RepositoryFactory { 
    private Map<Object, Object> repositories = new HashMap<>(); 
    private final String LOG_TAG = RepositoryFactory.class.getSimpleName(); 
    private Context context; 
    private static RepositoryFactory instance; 
    private BaseRepositoryContainer container; 

    private RepositoryFactory() {} 

    public void init(BaseRepositoryContainer container) { 
     this.container = container; 
     this.context = container.getContext(); 
     this.configureRepositories(container.getRepositoriesMap()); 
    } 

    private <T extends IRepository> void configureRepositories(Map<Class<T>, Class<T>> repositoriesMap) { 
     for (Entry<Class<T>, Class<T>> entry : repositoriesMap.entrySet()) { 
      this.registerRepository(entry.getKey(), entry.getValue()); 
     } 

    } 

    private <T extends IRepository> void registerRepository(Class<T> repInterface, Class<T> realRepository) { 
     repositories.put(repInterface, this.createRepository(realRepository)); 
    } 

    public <T extends IRepository> T getRepository(Class<T> repInterface) { 
     if (container == null) { 
      throw new UnsupportedOperationException("You should call init method providing a container."); 
     } 

     return (T) repositories.get(repInterface); 
    } 

    private <T extends IRepository> T createRepository(Class<T> repoClass) { 
     try { 
      T instance = repoClass.getConstructor(Context.class).newInstance(context); 
      Log.d(LOG_TAG, "Repository " + repoClass.getSimpleName() + " created"); 
      return instance; 
     } catch (InstantiationException e) { 
      Log.d(LOG_TAG, e.toString()); 
     } catch (IllegalAccessException e) { 
      Log.d(LOG_TAG, e.toString()); 
     } catch (InvocationTargetException e) { 
      Log.d(LOG_TAG, e.toString()); 
     } catch (NoSuchMethodException e) { 
      Log.d(LOG_TAG, e.toString()); 
     } 

     return null; 
    } 

    public static RepositoryFactory getInstance() { 
     if (instance == null) { 
      instance = new RepositoryFactory(); 
     } 

     return instance; 
    } 
} 

然后它可以被称为是这样的:

// when the application is first run 
RepositoryFactory.getInstance().init(new RepositoryContainer(this)); 
// retreaving the repository 
IDependenceyRepository repository = RepositoryFactory.getInstance() 
       .getRepository(IDependenceyRepository.class); 

所以我想知道这是一个很好的方法来帮助实现抽象吗?我不喜欢在没有强制要求的情况下调用工厂的初始化方法,唯一的方法是知道如果你不打电话会抛出一个我不喜欢的异常。

有没有人能指出我正确的方向?一种改进这种设计的方法?我不想在后来的项目中发现我创建了很多强大的依赖项,并且很难改变某些内容。

任何意见,将不胜感激。

回答

3

我做了什么来改善我的代码,而不是重新发明轮子,我开始使用依赖注入库(Dagger 2 - http://google.github.io/dagger/)。

您可以定义模块,根据您的需求将您的需求存储库返回到应用程序或活动中。