分离存储库和服务层

发布于 2025-02-08 17:53:36 字数 605 浏览 2 评论 0原文

我正在使用EF Core在ASP.NET核心Web API项目进行工作,并希望将存储库和服务层添加到我的应用程序中,以实现关注点的分离。 但是,我仍然无法弄清楚在存储库层和服务层之间绘制界限的位置。

我从进行一些研究中了解到的是,存储库层负责简单的CRUD操作,例如简单的选择语句,而服务层负责业务逻辑和更复杂的查询。

现在假设例如,我需要查询一组由某个作者撰写并在一年中发表的书籍,假设我有几个实体,那么我应该如何在存储库和服务层之间分解此查询的逻辑除了书籍吗?

我正在考虑创建一个书籍服务类,在其中我将创建linq表达式

books.Where(b => b.Author == SomeAuthor && b.Year == SomeYear)

并创建一个通用存储库类repository< t>,并且一种接受函数表达式get< t> t>(expression< func< lt; t>,list< t>>>>)并使用EF核心从数据库中获取数据。

您如何看待我的解决方案?如果您分享任何更好的想法,我会认可它

I am working on an ASP.NET Core Web API project using EF core and would like to add Repository and Service Layers to my application to achieve Separation of Concerns.
However, I still can not figure out where to draw the line between the repository layer and service layer.

What I understood from doing some research is that the repository layer is responsible to simple crud operations, such as simple SELECT statements, while the service layer is responsible for business logic and more complex queries.

Now assume that for example I need to query a set of Books written by a certain author and published in a certain year, how should I go about splitting the logic of this query between the the repository and the service layer, assuming I have several entities other than books?

I was thinking of creating a BookService Class where I would create the linq expression

books.Where(b => b.Author == SomeAuthor && b.Year == SomeYear)

and creating a generic Repository Class Repository<T> and a method that accepts a function expression Get<T>(Expression<Func<List<T>, List<T>>>) and use EF core to get the data from the database.

What do you think of my solution? I appreacitate it if you share any better ideas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2025-02-15 17:53:36

您要查询数据的所有过滤器。
我相信这也是Crud,这将帮助您,这不是商业逻辑。
继承您的通用存储库类并实现ibookrepository 接口可以注入您的业务逻辑类,然后在您的存储库中创建一个方法,> getBookSbyFilterAsync,然后将过滤器传递给它。
另外,您可以创建模型bookFilter将数据传递给存储库。
然后创建这样的扩展方法

public static IQueryable<Book> ApplyFilter(this IQueryable<Book> query, BookFilter filter)
{
// Filter by Author
            if (!string.IsNullOrEmpty(filter.Author))
                query = query.Where(x => x.Author.ToUpper().Contains(filter.Author.ToUpper()));

            // Filter by Year
            if (filter.Year.HasValue)
                query = query.Where(x => x.Year == filter.Year.Value);
.
.
.
}

,然后将过滤器应用于您的存储库中的iQueryable。

It is all filters that you want to Query the data.
It would help you that I believe it is Crud, too, and it's not business logic.
It can be nice to inherit your generic Repository Class and implement the IBookRepository interface that can be injected into your business logic class, then create a method in your repository, GetBooksByFilterAsync, and pass your filters to it.
Also, you can create a model BookFilter to pass the data to your repository.
and then create an extension method like this

public static IQueryable<Book> ApplyFilter(this IQueryable<Book> query, BookFilter filter)
{
// Filter by Author
            if (!string.IsNullOrEmpty(filter.Author))
                query = query.Where(x => x.Author.ToUpper().Contains(filter.Author.ToUpper()));

            // Filter by Year
            if (filter.Year.HasValue)
                query = query.Where(x => x.Year == filter.Year.Value);
.
.
.
}

And then apply the filter to your IQueryable in your repository.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文