2016-06-28 2 views
0

私は2つのリポジトリ(PostRepositoryとAlbumRepository)を持っていて、どちらも別のPublicationRepositoryから継承しています。あなたが見ることができるように、私はこれらの予測リポジトリの継承リファクタリング

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() 
          }); 

を持っているPostRepositoryで

、繰り返しのコードの多くはここにあります。すべての一般的な予測をベースリポジトリに移動し、特定のリポジトリ内の特定のもののみを保持する方法はありますか?

答えて

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; 
    } 
} 

そして:私は、クエリxに考える

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

は、エンティティ(郵便またはアルバム)、ではないのリポジトリである必要があります。 –