存储库继承重构

本文关键字:重构 继承 存储 | 更新日期: 2023-09-27 17:52:33

我有两个存储库(postrerepository和AlbumRepository),它们都继承自另一个PublicationRepository。

PostRepository中有这些投影

return queryBase.Select(x => new NewsFeed
                             {
                                 Id = x.PublicationID,
                                 Title = x.Title,
                                 Content = x.Content,
                                 CreatedDate = x.CreatedDate,
                                 Link = x.Link,
                                 Type = NewsFeedType.Post,
                                 PostType = x.PostType,
                                 OwnerId = x.UserID.HasValue ? x.UserID.Value : 0,
                                 OwnerFirstName = x.User != null ? x.User.FirstName : "",
                                 OwnerLastName = x.User != null ? x.User.LastName : "",
                                 OwnerProfilePicture = x.User != null ? x.User.PhotoPath : "",
                                 CommentsCount = x.PublicationComment.Count(),
                                 ILike = x.Consultation.Any(c => c.ViewedByID == userId && c.ILike == true && c.DeletedFlag != true),
                                 IsSignaled = x.Consultation.Any(c => c.ViewedByID == userId && c.IsSignaled == true && c.DeletedFlag != true),
                                 RecommendationCount = x.Consultation.Count(c => c.DeletedFlag != true && c.ILike == true),
                                 TargetPopulations = x.Access.Select(a => a.Population.PopulationName).ToList(),
                                 OwnerIsMyManager = promoManagerIds.Contains(x.UserID)
                             });

在AlbumRepository中我有这些

return queryBase.Select(x => new NewsFeed
                             {
                                 Id = x.PublicationID,
                                 Title = x.AlbumName,
                                 CreatedDate = x.CreatedDate,
                                 Type = NewsFeedType.Album,
                                 OwnerId = x.UserID.HasValue ? x.UserID.Value : 0,
                                 OwnerFirstName = x.User != null ? x.User.FirstName : "",
                                 OwnerLastName = x.User != null ? x.User.LastName : "",
                                 OwnerProfilePicture = x.User != null ? x.User.PhotoPath : "",
                                 CommentsCount = x.PublicationComment.Count(),
                                 ILike = x.Consultation.Any(c => c.ViewedByID == userId && c.ILike == true && c.DeletedFlag != true),
                                 IsSignaled = x.Consultation.Any(c => c.ViewedByID == userId && c.IsSignaled == true && c.DeletedFlag != true),
                                 RecommendationCount = x.Consultation.Count(c => c.DeletedFlag != true && c.ILike == true),
                                 TargetPopulations = x.Access.Select(a => a.Population.PopulationName).ToList(),
                                 AlbumPhotoPaths = x.AlbumPhoto.Where(a => a.DeletedFlag != true).Select(a => a.AlbumElementPath).ToList()
                             });

可以看到,这里有很多重复的代码。是否有一种方法可以将所有的公共投影移动到基本存储库中,而只将特定的投影保留在特定的存储库中?

存储库继承重构

请考虑以下结构:

public class PublicationRepository
{
    string PublicationID;
    string Title;
    public virtual NewsFeed GetNewsFeed()
    {
        return new NewsFeed { Id = PublicationID, Title = Title };
    }
}
public class PostRepository : PublicationRepository
{
    string UserID;
    public virtual NewsFeed GetNewsFeed()
    {
        NewsFeed newsFeed = base.GetNewsFeed();
        newsFeed.OwnerIsMyManager = promoManagerIds.Contains(UserID);
        return newsFeed;
    }
}
public class AlbumRepository : PublicationRepository
{
    AlbumPhoto[] AlbumPhoto;
    public override NewsFeed GetNewsFeed()
    {
        NewsFeed newsFeed = base.GetNewsFeed();
        newsFeed.AlbumPhotoPath = AlbumPhoto.Where(...);
        return newsFeed;
    }
}

然后:

return queryBase.Select(x => x.GetNewsFeed());