返回

优化你的新闻系统:使用领域驱动设计,20分钟搞定!

见解分享

你想构建一个高效而简单的新闻系统,领域驱动设计(DDD)正是你的最佳选择。让我们用20分钟的时间,快速打造一个满足你的需求的新闻系统。

需求

你的新闻系统需要满足以下需求:

  • 分页查找给定类别的新闻
  • 禁用的新闻不可见

工期估算

这个项目的工期约为20分钟。

起航

项目准备

首先,你需要准备好项目所需的依赖和配置信息。

添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
</dependencies>

添加配置信息

spring.datasource.url=jdbc:h2:mem:newsdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create

添加入口类

@SpringBootApplication
public class NewsApplication {

    public static void main(String[] args) {
        SpringApplication.run(NewsApplication.class, args);
    }
}

构建领域模型

领域模型是DDD的核心,它了系统中涉及的实体及其之间的关系。对于新闻系统,我们主要需要定义以下实体:

  • 新闻
  • 类别
  • 禁用新闻
@Entity
public class News {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String title;

    private String content;

    private Category category;

    private boolean disabled;

}

@Entity
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

}

@Entity
public class DisabledNews {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private News news;

}

实现服务层

服务层负责处理业务逻辑。对于新闻系统,我们主要需要实现以下服务:

  • 新闻服务
  • 类别服务
  • 禁用新闻服务
public interface NewsService {

    List<News> findAllByCategory(Category category, Pageable pageable);

    List<News> findAllByDisabled(boolean disabled, Pageable pageable);

}

public interface CategoryService {

    List<Category> findAll();

}

public interface DisabledNewsService {

    List<DisabledNews> findAllByNews(News news);

}

实现控制器

控制器负责处理HTTP请求。对于新闻系统,我们主要需要实现以下控制器:

  • 新闻控制器
  • 类别控制器
  • 禁用新闻控制器
@RestController
public class NewsController {

    private final NewsService newsService;

    public NewsController(NewsService newsService) {
        this.newsService = newsService;
    }

    @GetMapping("/news")
    public List<News> findAllByCategory(@RequestParam Long categoryId, Pageable pageable) {
        return newsService.findAllByCategory(categoryId, pageable);
    }

    @GetMapping("/news/disabled")
    public List<News> findAllByDisabled(@RequestParam boolean disabled, Pageable pageable) {
        return newsService.findAllByDisabled(disabled, pageable);
    }

}

@RestController
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("/categories")
    public List<Category> findAll() {
        return categoryService.findAll();
    }

}

@RestController
public class DisabledNewsController {

    private final DisabledNewsService disabledNewsService;

    public DisabledNewsController(DisabledNewsService disabledNewsService) {
        this.disabledNewsService = disabledNewsService;
    }

    @GetMapping("/news/disabled")
    public List<DisabledNews> findAllByNews(@RequestParam Long newsId) {
        return disabledNewsService.findAllByNews(newsId);
    }

}

测试系统

现在,你可以运行系统并进行测试了。

mvn clean install
mvn spring-boot:run

你可以使用Postman或其他HTTP客户端来测试系统。

结论

你已经成功地使用领域驱动设计构建了一个简单的新闻系统。这个系统能够满足你的需求,并且易于维护和扩展。