使用存储库模式,使用什么 lambda 表达式来访问子 class?

Using repository pattern, what lamda expression to use to access sub class?

我正在使用存储库模式,但我正在努力解决如何使用 lamda 表达式连接两个 classes 的问题。使用 GenericRepository class 的最佳方法是什么?我是这种设计模式的新手。我在一些网站上看到建议不要在存储库 class This suggest not to expose LINQ in repository class 中公开 LINQ 如果我们不在存储库 class 中使用 LINQ,那么连接两个表的最佳选择是什么(classes)?

我有以下表格,如 ER 图中所示 ER diagram for playlist

1)播放列表

        //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated from a template.
    //
    //     Manual changes to this file may cause unexpected behavior in your application.
    //     Manual changes to this file will be overwritten if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------

    namespace MusicCloud.Model.EFModel
    {
        using System;
        using System.Collections.Generic;

        public partial class PlayList
        {
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
            public PlayList()
            {
                this.PlayListSongs = new HashSet<PlayListSong>();
            }

            public int Id { get; set; }
            public string Name { get; set; }
            public Nullable<System.DateTimeOffset> CreatedDate { get; set; }
            public Nullable<System.DateTimeOffset> ModifiedDate { get; set; }

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<PlayListSong> PlayListSongs { get; set; }
        }
    }

2) 播放列表歌曲

            //------------------------------------------------------------------------------
        // <auto-generated>
        //     This code was generated from a template.
        //
        //     Manual changes to this file may cause unexpected behavior in your application.
        //     Manual changes to this file will be overwritten if the code is regenerated.
        // </auto-generated>
        //------------------------------------------------------------------------------

        namespace MusicCloud.Model.EFModel
        {
            using System;
            using System.Collections.Generic;

            public partial class PlayListSong
            {
                public int PUSongId { get; set; }
                public int PlistId { get; set; }
                public int SongId { get; set; }
                public string UserId { get; set; }

                public virtual AspNetUser AspNetUser { get; set; }
                public virtual PlayList PlayList { get; set; }
                public virtual Song Song { get; set; }
            }
        }

在 UnitofWork class 中,我有

 /// <summary>
    /// 
    /// </summary>
    public GenericRepository<PlayList> PlayListRepository
    {
        get
        {
            if (this._playListRepository == null)
                this._playListRepository = new GenericRepository<PlayList>(_context);
            return _playListRepository;
        }
    }


    /// <summary>
    /// 
    /// </summary>
    public GenericRepository<PlayListSong> PlayListSongRepository
    {
        get
        {
            if (this._playListSongRepository == null)
                this._playListSongRepository = new GenericRepository<PlayListSong>(_context);
            return _playListSongRepository;
        }
    }

我如何实现这样的东西

    namespace MusicCloud.Services
            {
                public class PlayListServices : IPlayListServices
                {
                    private readonly UnitOfWork _unitOfWork;


                    public PlayListServices(UnitOfWork unitOfWork)
                    {
                        _unitOfWork = unitOfWork;
                    }

     public IEnumerable<PlayListEntity> GetAllPlayLists(string userId)
        {      

        string[] includes = { "PlayList", "PlayListSong" };
        var playLists =_unitOfWork.PlayListSongRepository.GetWithMultipleInclude(
                                 e => e.UserId==userId,includes
                                ).Select(x=>x.PlayList).ToList();

                   if (playLists.Any())
                        {

                            Mapper.Initialize(cfg =>
                            {
                                cfg.CreateMap<PlayList, PlayListEntity>();                                });

                             var playListsModel = Mapper.Map<List<PlayList>, List<PlayListEntity>>(playLists);
                            return playListsModel;
                        }
                        return null;
                    }
                   }
                   }

我想获取用户创建的所有播放列表(播放列表名称)。 如何加入 Playlist 和 PlayListSong 以按 userId 过滤?下面的代码给出了错误。

string[] includes = { "PlayList", "PlayListSong" };
        var playLists =_unitOfWork.PlayListSongRepository.GetWithMultipleInclude(
                                 e => e.UserId==userId,includes
                                ).Select(x=>x.PlayList).ToList();




         /// <summary>
            /// Inclue multiple
            /// </summary>
            /// <param name="predicate"></param>
            /// <param name="include"></param>
            /// <returns></returns>
            public IEnumerable<TEntity> GetWithMultipleInclude(
                System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate, string[] include)
            {
                IQueryable<TEntity> query = this.DbSet;
                foreach (string inc in include)
                {
                    query = query.Include(inc);
                }

                return query.Where(predicate).ToList<TEntity>() ;
            }

或者是否有更好的方法仅使用 PlayListRepository 和 PlayListSongRepository 而不使用 lamda 表达式来实现此目的?什么是最合适的方法?同样,如果我需要更新多个相关表,我该如何执行更新,使用 lamda 还是仅使用 PlayListRepository 和 PlayListSongRepository?

谢谢。

在 PlayListSong 你有

public virtual PlayList PlayList { get; set; }

获取播放列表的最简单方法是启用延迟加载,并从播放列表歌曲中获取所有播放列表。

我不知道你的回购看起来如何,但如果你有这样的东西:

public IEnumerable<T>`GetAll()
    {
        return db.Set<T>();
    }

那就这样搞吧:

_unitOfWork.PlayListSongRepository.GetAll()
    .Where(x => x.UserId == userId).Select(x => x.PlayList).Distinct();

记得启用延迟加载。