带有 Neo4j 的 Spring,GraphRepository<?>vs手工界面 [英] Spring with Neo4j, GraphRepository&lt;?&gt; vs handmade interface

查看:36
本文介绍了带有 Neo4j 的 Spring,GraphRepository<?>vs手工界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有一个叫做GraphRepository的接口.我有一个供用户实现其工作的自制界面的存储库,但我想知道,我不应该实现 GraphRepository 吗?即使实现时间很长,有些方法没用,我认为这是一个标准,我已经重新编码了这个接口中定义的很多方法.

I found out that there is an interface called GraphRepository. I have a repository for users implementing a homemade interface that does its job, but I was wondering, shouldn't I implement GraphRepository instead ? Even if it will be quite long to implement and some methods will be useless, I think it is a standard and I already re-coded a lot of methods that are defined in this interface.

那么我应该编写YAGNI"代码还是不遵守标准?你有什么建议?

So should I write "YAGNI" code or not respect the standard ? What is your advice ?

推荐答案

您不需要实际实现 GraphRepository 而是扩展它.Spring-Data 的原则是处理所有样板 CRUD 代码(通过在启动时代理),因此您要做的就是为扩展 GraphRepository 的特定实体创建一个接口和然后只添加您需要的特定方法.

you don't need to actually implement GraphRepository but extend it. the principals of Spring-Data is that all the boiler-plate CRUD code is taken care of (by proxying at startup time) so all you have to do is create an interface for your specific entity extending GraphRepository and then add only specific methods that you require.

例如;如果我有一个实体 CustomerNode,要创建标准的 CRUD 方法,我可以创建一个新接口 CustomerNodeRepository extends GraphRepository.GraphRepository 中的所有方法(例如 save、findAll、findOne、delete、deleteAll 等)现在都可以从 CustomerNodeRepository 访问并由 Spring-Data-Neo4J 实现,而无需写一行实现代码.

for example; if i have an entity CustomerNode, to create standard CRUD methods, i can create a new interface CustomerNodeRepository extends GraphRepository<CustomerNode,Long>. all the methods from GraphRepository (e.g. save, findAll, findOne, delete, deleteAll, etc.) are now accessible from CustomerNodeRepository and implemented by Spring-Data-Neo4J without having to write a single line of implementation code.

该模式现在允许您处理特定的存储库代码(例如 findByNameAndDateOfBirth),而不是简单的 CRUD 内容.

the pattern now allows you to work on your specific repository code (e.g. findByNameAndDateOfBirth) rather than the simple CRUD stuff.

Spring-Data 包对于存储库交互非常有用.它可以减少大量代码(代码行减少了 80% 以上),强烈推荐使用它

Spring-Data package is very useful for repository interaction. it can reduce huge amounts of code (have seen 80%+ reduction in code lines) and would highly recommend using it

实现自定义执行

如果您想将自己的自定义行为添加到 Repository 方法中,您可以创建合并接口和自定义实现的概念.例如,假设我想创建一个名为 findCustomerNodeBySomeStrangeCriteria 的方法,为此,我实际上想链接到关系数据库来执行该功能.

if you want to add your own custom behavior to a Repository method, you create the concept of merging interfaces and custom implementation. for example, lets say i want to create a method called findCustomerNodeBySomeStrangeCriteria and to do this, i actually want to link off to a relational database to perform the function.

首先我们定义一个单独的、独立的接口,包含我们的额外"方法.

first we define a separate, standalone interface that only includes our 'extra' method.

public interface CustomCustomerNodeRepository {
   List<CustomerNode> findCustomerNodeBySomeStrangeCriteria(Object strangeCriteria);
}

接下来我们更新我们的普通接口,不仅扩展 GraphRepository,还扩展我们新的自定义接口

next we update our normal interface to extend not only GraphRepository, but our new custom one too

public interface CustomerNodeRepository extends GraphRepository<CustomerNode,Long>, CustomCustomerNodeRepository {

}

最后一部分,是实际实现我们的findCustomerNodeBySomeStrangeCriteria方法

the last piece, is to actually implement our findCustomerNodeBySomeStrangeCriteria method

public class CustomerNodeRepositoryImpl implements CustomCustomerNodeRepository {

   public List<CustomerNode> findCustomerNodeBySomeStrangeCriteria(Object criteria) {
    //implementation code
}

}

所以,有几点需要注意;

so, there's a couple of points to note;

  • 我们创建了一个单独的接口来定义任何具有自定义实现的自定义方法(与 Spring-Data 兼容的findBy..."方法不同)
  • 我们的 CustomerNodeRepository 接口(我们的主"接口)扩展了 GraphRepository 我们的自定义"接口
  • 我们在实现自定义接口的类中实现自定义"方法
  • 自定义"实现类必须(默认情况下)称为我们的主"接口 Impl 以供 Spring Data 选择(因此在本例中为 CustomNodeRepositoryImpl)
  • we create a separate interface to define any custom methods that have custom implementations (as distinct from Spring-Data compatible "findBy..." methods)
  • our CustomerNodeRepository interface (our 'main' interface) extends both the GraphRepository and our 'custom' one
  • we implement only the 'custom' method in a class that implements only the custom interface
  • the 'custom' implementation class must (by default) be called our 'main' interface Impl to be picked up by Spring Data (so in this case CustomNodeRepositoryImpl)

在幕后,Spring Data 提供了 CustomerNodeRepository 的代理实现,作为自动构建的 GraphRepository 和我们实现 CustomCustomerNodeRepository 的类的合并.类名的原因是让 Spring Data 可以轻松/成功地获取它(这个 可以 被覆盖,因此它不会寻找 *Impl)

under the covers, Spring Data delivers a proxy implementation of CustomerNodeRepository as a merge of the auto-built GraphRepository and our class implementing CustomCustomerNodeRepository. the reason for the name of the class is to allow Spring Data to pick it up easily/successfully (this can be overwritten so it doesn't look for *Impl)

这篇关于带有 Neo4j 的 Spring,GraphRepository<?>vs手工界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆