mardi 28 juin 2016

Repository inheritance refactoring

I have two repositories (PostRepository and AlbumRepository) and both inherit from another PublicationRepository.

In PostRepository I have these projections

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

And In AlbumRepository I have these

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

As you can see, there is a lot of repeated code here. Is there a way to move all the common projections to the base repository and keep only the specific ones in the specific repositories?

Aucun commentaire:

Enregistrer un commentaire