gpt4 book ai didi

java - 基于多个参数返回结果的模式

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:03 25 4
gpt4 key购买 nike

我正在处理遗留代码,其中一个方法应该根据多个参数返回结果。代码中使用的模式有效,但我在想是否可以进一步改进。我举个例子

假设我有一个 JobPosting 类,它具有各种属性

class JobPosting {
int jobPostingId;
String description;
Dept dept; // A posting ca belong to 1 department at a time
Location loc; // A job posting can belong to 1 location at a time
String created By;
int headcount; // number of people required to fill the job posting
List<User> assignedUsers; // number of people who have applied for the posting already
List<User> reservedUsers; // users who are eligible to apply for the posting, if this is empty, then anyone can apply to posting
DateTime visibleDate; // date from when posting is visible to users
Date endDate; // posting's deadline
...
}

这些发布的详细信息存储在数据库中的一个表中

现在我有各种用例来获取帖子:

- Get job postings for a loc & dept- I only care about description and headcount.
- Get job posintgs for a loc & dept with users who have filled the postings
- Get job postings for a loc & dept with users who have filled and users who are eligible for the postings
- Get job postings for a loc & dept which are visibleToUsers (visibleDate < curdate)

依此类推,我可以根据场景有多种获取帖子的可能性。

当前的实现是我们有参数的构建器模式

class postingsParams {
boolean excludeFilledPostings;
boolean excludeNonVisiblePostings;
boolean returnAssignedUsers;
boolean returnEligibleUsers;
}

以及一种方法,该方法接受参数并确定要填写 JobPostings 中的哪些字段以及要排除哪些帖子

getPostingsByLocDept(loc, dept, postingsParamas) { ...// code calls dynamic SQL queries to return results and if conditions to filter }

这似乎工作正常,但我不喜欢的是

- Each caller must be aware of all the possible crtireia to search
- If in future I want to add another criteria like excludePostingsWithPassedDeadline, I need to make changes to this method and need to test all the places where it is called to make sure nothing breaks

一种方法是为用例创建特定的 get 方法,但通过这种方法,我们最终可能会得到多个 get 方法,这可能是不可取的。

有没有办法改进这种设计,或者当前的方法是处理这种情况的最佳方法吗?

最佳答案

我建议使用 JPA,使用 spring 实现(例如使用依赖项 org.springframework.data:spring-data-jpa:1.7.2.RELEASE)。

然后你只需定义扩展 CrudRepository 的接口(interface),例如:

public interface MyEntityRepo extends CrudRepository<MyEntity, Long>
{
public Collection<MyEntity> findByAccountIdAndProductId(
Long aAccountId,
Long aProductId );

MyEntity findByDeviceHwIdAndLicenseLicenseLicenseeName( String aHwId, String aLicenseeName );

etc.

其中 MyEntity 是带注释的 POJO bean,例如:

@Entity
@Table( name = "my_entity" )
public class MyEntity implements java.io.Serializable
{
private static final long serialVersionUID = 0L;

@Id
@GeneratedValue( strategy = IDENTITY )
@Column( name = "id", unique = true, nullable = false )
private Long Id;

@Column( name = "account_id", nullable = false )
private long accountId;

@Column( name = "product_id", nullable = false )
private long productId;

@ManyToOne( fetch = FetchType.LAZY )
@JoinColumn( name = "license_id", nullable = false )
private License license;

etc.

其中 License 是一个带有字符串成员licenseeName 的联合实体。

JPA 框架自动解析此类接口(interface)方法,无需手动定义查询。

关于java - 基于多个参数返回结果的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34423297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com