简体   繁体   English

分离存储库和服务层

[英]Seperating Repository and Service Layer

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.我正在使用 EF 核心开发一个 ASP.NET Core Web API 项目,并希望将存储库和服务层添加到我的应用程序以实现关注点分离。 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.我通过研究了解到,repository 层负责简单的 crud 操作,例如简单的 SELECT 语句,而 service 层负责业务逻辑和更复杂的查询。

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我正在考虑创建一个 BookService 类,我将在其中创建 linq 表达式

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.并创建一个通用存储库类Repository<T>和一个接受函数表达式Get<T>(Expression<Func<List<T>, List<T>>>)并使用 EF 核心从数据库中获取数据的方法.

What do you think of my solution?你觉得我的解决方案怎么样? I appreacitate it if you share any better ideas如果您分享任何更好的想法,我将不胜感激

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.我相信它也是 Crud,它不是业务逻辑,这将对您有所帮助。 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.继承您的通用 Repository 类并实现可以注入业务逻辑类的IBookRepository interface会很好,然后在您的存储库中创建一个方法GetBooksByFilterAsync ,并将您的过滤器传递给它。 Also, you can create a model BookFilter to pass the data to your repository.此外,您可以创建一个模型BookFilter以将数据传递到您的存储库。 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.然后将过滤器应用于存储库中的 IQueryable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM