【问题标题】:What is the name for class used to store row from result set?用于存储结果集中行的类的名称是什么?
【发布时间】:2017-06-26 01:36:03
【问题描述】:

出于性能原因,许多报表查询在 ORM 托管对象之外执行。

为了存储结果,我使用每个查询一个类来保存 DB 访问方法和服务之间的数据行。

例如在 Java/Hibernate/Spring 中:

@Repository
public interface DataRepository extends JpaRepository<DataDAO, Long> {

    @Query("select new com.evil.db.XXX(...) from #{#entityName} where ...")
    List<XXX> findXXX(...);      

XXX 的名称是什么?是DTO 还是DAO 还是Business object?模式书和框架参考手册中如何命名中间存储类?这是一个POJO,但这个词太笼统了......

我通常将这些类作为内部静态类,并且可以为包含数据的名称提供合适的名称。

但是如果顶级类代表它的角色,我喜欢为类名添加后缀,就像传统上为DAO/Service/Controller/etc 所做的那样......

【问题讨论】:

  • 从数据库加载的 IMO 对象不需要任何额外的后缀。比如调用UserLocation等类没有错。

标签: java design-patterns orm naming-conventions


【解决方案1】:

假设您使用的是存储库模式,您不应该遇到这个命名问题。存储库模式通常由名为 IRepository 的接口定向 - 或者也可以是一个抽象类 - 表示对数据库的通用操作,如下所示(C# 代码):

public interface IRepository<T>
{
    T GetById(int id);
    IEnumerable<T> List(Expression<Func<T, bool>> predicate);
    void Add(T entity);
    void Delete(T entity);
    void Update(T entity);
}

在您的情况下,由于您需要一些自定义查询,您应该为您的具体情况创建一个界面。以下示例引用了强制实施 IRepository 的产品域 (IProductRepository) 的特定存储库。

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public interface IProductRepository : IRepository<Product>
{
    Product GetProductBySomething(string somemething);
    IEnumerable<Product> GetAvailableProducts();
}

最后,您将创建一个实现 IRepositoryProduct 的具体类。

public class ProductRepository : IProductRepository
{
    public void Add(Product entity)
    {
        // ...
    }

    public void Delete(Product entity)
    {
        // ...
    }

    public IEnumerable<Product> Get(Expression<Func<Product, bool>> predicate)
    {
        // ...
    }

    public Product Get(int id)
    {
        // ...
    }

    public IEnumerable<Product> GetAvailableProducts()
    {
        // ...
    }

    public Product GetProductBySomething(string somemething)
    {
        // ...
    }

    public void Update(Product entity)
    {
        // ...
    }
}

    public Product Get(int id)
    {
        // ...
    }

    public void Update(Product entity)
    {
        // ...
    }
}

使用这种方法,您的方法不应该有命名问题,因为它们将遵循接口约定。

【讨论】:

  • 不幸的是,您错过了我的问题...我查询与任何 ORM 托管实体具有不同签名的数据并且不知道代表此数据的类的名称...查看select new com.evil.db.XXX(...) ... 语法以上...
  • @gavenkoa,现在我明白了!它通常被称为“模型”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 2019-04-09
相关资源
最近更新 更多