首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于存储结果集中的行的类的名称是什么?

用于存储结果集中的行的类的名称是什么?
EN

Stack Overflow用户
提问于 2017-02-08 20:23:59
回答 1查看 289关注 0票数 1

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

为了存储结果,我使用one-per-query类来保存DB访问方法和服务之间的数据行。

例如在Java/Hibernate/Spring中:

代码语言:javascript
复制
@Repository
public interface DataRepository extends JpaRepository<DataDAO, Long> {

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

XXX的名称是什么?是DTODAO还是Business object?在模式书和框架参考手册中,中间存储类是如何命名的?它是一个POJO,但是这个术语太通用了.

我通常将这些类作为内部静态类,并且不会为包含数据指定合适的名称。

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

EN

回答 1

Stack Overflow用户

发布于 2017-02-08 21:58:58

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

代码语言:javascript
复制
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)的特定存储库。

代码语言:javascript
复制
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的具体类。

代码语言:javascript
复制
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)
    {
        // ...
    }
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42113050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档