2016-06-28 91 views
0

我有兩個存儲庫(PostRepository和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() 
          }); 

正如你可以看到,有很多重複的代碼在這裏。有沒有辦法將所有常見投影移動到基礎存儲庫並只保留特定存儲庫中的特定投影?

回答

1

請考慮以下結構:

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()); 
+0

我想在查詢x應是實體(發佈或專輯),而不是倉庫。 –