Loading...
Blog-post Thumbnail
My Blog Building Journey - pt2

Now that I have a functioning Blog, I want to step away from the Command Pattern toward a Repository Pattern the original author eluded to but didn't implement.

First Step, Understand Repository Pattern

This took a good deal of Googling but I think I nailed it down by following this great post:

Code Project - Repository pattern, done right

I also brought in some referenced classes from here and placed them here: 

SolutionExplorer_EFCore.png

These are required to run my repository engine. Now I need to create concrete classes for my model library.  So the architecture goes like this:

UI <--ViewModel--> Controller <--ViewModel--> Service <--DomainModel--> RepositoryObject <----> dbContext <-- DomainModel--> dbSets <----> SourceData

I start easy with the Author\ManageTopics page, just return a list of all Topics.

TopicRepository.cs

namespace AcreeBlog.Data.EFCore
{
    public class TopicRepository : IRepository<Topic>
    {
        private readonly BlogDbContext _dbContext;
    public TopicRepository(BlogDbContext dbContext)
    {
        _dbContext = dbContext;
    }
public Topic GetById(long id)
{
    return _dbContext.Topics.FirstOrDefault(x =&amp;gt; x.Id == id);
}

public IEnumerable&amp;lt;Topic&amp;gt; GetAll()
{
    return _dbContext.Topics.ToList();
}

public void Update(Topic topic)
{
    _dbContext.Topics.Attach(topic);
}

public void Delete(Topic topic)
{
    _dbContext.Topics.Remove(topic);
}

}

}

Topic.cs

namespace Bagombo.Models
{
  public class Topic
  {
    public long Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public bool ShowOnHomePage { get; set; }
    public ICollection<BlogPostTopic> BlogPosts{ get; set; }
  }
}

TopicService.cs

namespace AcreeBlog.Services
{
    public class TopicService
    {
        private readonly BlogDbContext _dbContext;
        private TopicRepository _topicRepository;
        private readonly IMapper _mapper;
    public TopicService(BlogDbContext dbContext, 
        TopicRepository topicRepository,
        IMapper mapper)
    {
        _dbContext = dbContext;
        _topicRepository = topicRepository;
        _mapper = mapper;
    }
public ManageTopicsViewModel GetAll()
{
    ManageTopicsViewModel mfvm = new ManageTopicsViewModel()
    {
        Topics = _topicRepository.GetAll()
    };
    return mfvm;
}

}

}

AdminController.cs

Continuing the Dependency Injection pattern, I added the TopicService _topicService private attribute, passed it along the Controller's constructor and initialized it with the rest of the DI objects.

    [HttpGet]
    public async Task<IActionResult> ManageTopics()
    {
        var mfvm = _topicService.GetAll();
        return View(mfvm);
    }

A quick test proved this works. Yes!

There are 3 comments

avatar

The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The European languages are members of the same family. Their separate existence is a myth.

Today, 14:55 - Reply
avatar

Europe uses the same vocabulary. The European languages are members of the same family. Their separate existence is a myth.

Today, 15:34 - Reply
avatar

The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The European languages are members of the same family. Their separate existence is a myth.

Today, 14:59 - Reply

Add your comment

About Chris Acree

The languages only differ in their grammar, their pronunciation and their most common words.

Phone: +1 234 567 89 10

Fax: +1 234 567 89 10

Email:somecompany@example.com

Popular Posts