Introduction to repositories

We have been performing CRUD operations in the controller directly with the data context; we could incorporate a repository in our application first, and later, query objects could be implemented in the repository.

Let us start with creating the interface required for the repository. It includes basic retrieval, business-specific retrievals, and the remaining CRUD operations. The following code contains the methods required for the IPostRepository:

    public interface IPostRepository
{
IEnumerable<Post> GetAllPosts();
Task<IEnumerable<Post>> GetAllPostsAsync();
Post GetPostById(int? id, bool includeData = true);
Task<Post> GetPostByIdAsync(int? id, bool includeData = true);
IEnumerable<Post> FindPostByTitle(string title);
IEnumerable<Post> FindPostByAuthor(string author);
IEnumerable<Post> FindPostByPublishedYear(int year);
IEnumerable<Post> FindPostByHighestVisitors();
IEnumerable<Post> FindPostByCategory(string category);
IEnumerable<Post> FindPost(string keyword, int pageCount,
int pageSize);
int AddPost(Post item);
Task<int> AddPostAsync(Post item);
int UpdatePost(Post item);
Task<int> UpdatePostAsync(Post item);
int DeletePost(int? id);
Task<int> DeletePostAsync(int? id);
}

The repository implementation starts with the database context injection, BlogContext in our case. Then the interface implementation starts with FindPost, which takes keyword, pageCount, and pageSize, which were consumed by the LINQ query in filtering posts using those parameters. The keyword was used as a wildcard search across all fields available in the Post entity:

    public class PostRepository : IPostRepository
{
private readonly BlogContext _context;
public PostRepository(BlogContext context)
{
_context = context;
}

public IEnumerable<Post> FindPost(string keyword, int pageCount,
int pageSize)
{
return _context.Posts
.Where(x =>
x.Title.ToLower().Contains(keyword.ToLower())
|| x.Blog.Title.ToLower().Contains(keyword.ToLower())
|| x.Blog.Subtitle.ToLower().Contains(keyword.ToLower())
|| x.Category.Name.ToLower().Contains(keyword.ToLower())
|| x.Content.ToLower().Contains(keyword.ToLower())
|| x.Summary.ToLower().Contains(keyword.ToLower())
|| x.Author.Username.ToLower().Contains(keyword.ToLower())
|| x.Url.ToLower().Contains(keyword.ToLower()))
.Skip(pageCount-1).Take(pageSize);
}

We could have the assignment to cover the remaining implementations (as well as new actions required to filter posts by category, author, and so on) required in the controller, as we mostly port the controller implementations into a repository method; for instance, the action implementation of the Get action gets into the GetAllPosts method of the repository. In this chapter, since we are working on an assignment that is required to proceed to the next section, we will have the solutions captured as well, just to ensure we have everything implemented before we move on to the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset