C# 如何使我的数据访问层更好?

C# 如何使我的数据访问层更好?,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我已经使用实体框架和存储库模式玩了一段时间,但我想知道我目前所做的是否有任何错误,或者如何改进它 我的场景通常要求我仅从HR systems Oracle数据库读取数据,然后对于我正在处理的应用程序,我倾向于将该应用程序的数据存储在其自己的MS Sql数据库中,偶尔也会存储在我在HR Oracle数据库中创建的一些表中 我的目标是获得一种可重用、健壮的数据访问方法,并确保数据能够以高效可靠的方式使用。我还需要找出为某些实体创建一些更具体数据访问方法的最佳方法,例如,我的通用存储库没有创建、编辑或

我已经使用实体框架和存储库模式玩了一段时间,但我想知道我目前所做的是否有任何错误,或者如何改进它

我的场景通常要求我仅从HR systems Oracle数据库读取数据,然后对于我正在处理的应用程序,我倾向于将该应用程序的数据存储在其自己的MS Sql数据库中,偶尔也会存储在我在HR Oracle数据库中创建的一些表中

我的目标是获得一种可重用、健壮的数据访问方法,并确保数据能够以高效可靠的方式使用。我还需要找出为某些实体创建一些更具体数据访问方法的最佳方法,例如,我的通用存储库没有创建、编辑或删除方法,因为我想消除在人力资源数据库中编辑数据的风险,但正如我所说,我可能有自己的表,我确实想在人力资源数据库中编辑、创建和删除数据。同样地,我可能希望使用我的通用存储库和unitofwork来访问多个数据源,而我目前不知道如何处理这些数据源

此外,我目前没有使用任何接口,我想知道是否可以或应该实现这些接口,如IRepository、IUnitOfWork、IContext等

目前,我针对DbContext使用通用存储库模式来提供一些处理数据的基本方法。然后我有一个unitofwork实现,这样我就可以轻松地访问实体模型的各种存储库

e、 g

以下是我迄今为止的实施情况:

通用存储库

public class GenericRepository<T> where T : class
{
    internal CHRISCSEntities c21context;
    internal DbSet<T> dbSet;

    public GenericRepository(CHRISCSEntities c21context)
    {
        this.c21context = c21context;
        this.dbSet = c21context.Set<T>();
    }

    public virtual IEnumerable<T> Get(
        Expression<Func<T, bool>> filter = null,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<T> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }

    public virtual T GetByID(object id)
    {
        return dbSet.Find(id);
    }
}
公共类GenericRepository,其中T:class
{
内部chriscentities c21上下文;
内部数据库集;
公共一般报告(chriscentities c21上下文)
{
this.c21context=c21context;
this.dbSet=c21context.Set();
}
公共虚拟IEnumerable Get(
表达式筛选器=空,
Func orderBy=null,
字符串includeProperties=“”)
{
IQueryable query=dbSet;
if(过滤器!=null)
{
query=query.Where(过滤器);
}
foreach(includeProperty.Split中的var includeProperty
(新字符[]{',},StringSplitOptions.RemoveEmptyEntries)
{
query=query.Include(includeProperty);
}
if(orderBy!=null)
{
returnorderby(query.ToList();
}
其他的
{
返回query.ToList();
}
}
公共虚拟T GetByID(对象id)
{
返回dbSet.Find(id);
}
}
这是我的工作单元:

public class UnitOfWork : IDisposable
{
    private CHRISCSEntities c21context = new CHRISCSEntities();

    private GenericRepository<EMPOS> emposRepository;
    private GenericRepository<PSLDW> psldwRepository;
    private GenericRepository<UPZ88> upz88Repository;
    private GenericRepository<EMLVE> emlveRepository;
    private GenericRepository<EMDET> emdetRepository;
    private GenericRepository<PSDET> psdetRepository;
    private GenericRepository<DASH_PYLVR> pylvrRepository;

    public GenericRepository<DASH_PYLVR> PylvrRepository
    {
        get
        {

            if (this.pylvrRepository == null)
            {
                this.pylvrRepository = new GenericRepository<DASH_PYLVR>(c21context);
            }
            return pylvrRepository;
        }
    }

    public GenericRepository<PSDET> PsdetRepository
    {
        get
        {

            if (this.psdetRepository == null)
            {
                this.psdetRepository = new GenericRepository<PSDET>(c21context);
            }
            return psdetRepository;
        }
    }

    public GenericRepository<EMDET> EmdetRepository
    {
        get
        {

            if (this.emdetRepository == null)
            {
                this.emdetRepository = new GenericRepository<EMDET>(c21context);
            }
            return emdetRepository;
        }
    }

    public GenericRepository<EMLVE> EmlveRepository
    {
        get
        {

            if (this.emlveRepository == null)
            {
                this.emlveRepository = new GenericRepository<EMLVE>(c21context);
            }
            return emlveRepository;
        }
    }

    public GenericRepository<EMPOS> EmposRepository
    {
        get
        {

            if (this.emposRepository == null)
            {
                this.emposRepository = new GenericRepository<EMPOS>(c21context);
            }
            return emposRepository;
        }
    }

    public GenericRepository<PSLDW> PsldwRepository
    {
        get
        {

            if (this.psldwRepository == null)
            {
                this.psldwRepository = new GenericRepository<PSLDW>(c21context);
            }
            return psldwRepository;
        }
    }

    public GenericRepository<UPZ88> Upz88Repository
    {
        get
        {

            if (this.upz88Repository == null)
            {
                this.upz88Repository = new GenericRepository<UPZ88>(c21context);
            }
            return upz88Repository;
        }
    }

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                c21context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}
公共类UnitOfWork:IDisposable
{
私有CHRISCSEntities c21context=新CHRISCSEntities();
私有通用存储库;
私人一般储蓄;
专用通用存储库UPZ88;
专用通用存储库;
专用通用存储库;
专用通用存储库;
私人通用存储库;
公共通用存储库
{
得到
{
if(this.pylvrRepository==null)
{
this.pylvrRepository=新的通用存储库(c21context);
}
返回存储库;
}
}
公共通用存储库
{
得到
{
if(this.psdetRepository==null)
{
this.psdetRepository=新的通用存储库(c21context);
}
返回psdet存储库;
}
}
公共通用存储库
{
得到
{
if(this.emdetRepository==null)
{
this.emdetRepository=新的通用存储库(c21context);
}
返回存储库;
}
}
公共通用存储库
{
得到
{
if(this.emlveRepository==null)
{
this.emlveRepository=新的通用存储库(c21context);
}
返回存储库;
}
}
公共通用存储库
{
得到
{
if(this.emposRepository==null)
{
this.emposRepository=新的GenericRepository(c21context);
}
返回emposRepository;
}
}
公共通用存储PsldwRepository
{
得到
{
if(this.psldwRepository==null)
{
this.psldwRepository=新的通用存储(C21上下文);
}
返回psldwRepository;
}
}
公共通用存储库UPZ88
{
得到
{
if(this.upz88Repository==null)
{
this.upz88Repository=新的通用存储库(c21上下文);
}
返回UPZ880存储库;
}
}
私有布尔=假;
受保护的虚拟void Dispose(bool disposing)
{
如果(!this.disposed)
{
如果(处置)
{
c21context.Dispose();
}
}
这是真的;
}
公共空间处置()
{
处置(真实);
总干事(本);
}
}
任何帮助都将不胜感激


Andy

您可以查看有关体系结构的系列文章中的代码。它与IOC一起大量使用接口。

在dal中使用依赖注入,还使用接口,这样您就不必依赖于特定的类

请参阅以下链接。


这是一个存储库和IUnitOfWork演示。

谢谢这里有一些有趣的阅读@L-Three&@jalpesh!缓存策略是否应该用它来实现?是否有任何模式或方法可以很好地与ef、unitofwork和repository模式配合使用?还有没有办法获得SqlDepen
public class UnitOfWork : IDisposable
{
    private CHRISCSEntities c21context = new CHRISCSEntities();

    private GenericRepository<EMPOS> emposRepository;
    private GenericRepository<PSLDW> psldwRepository;
    private GenericRepository<UPZ88> upz88Repository;
    private GenericRepository<EMLVE> emlveRepository;
    private GenericRepository<EMDET> emdetRepository;
    private GenericRepository<PSDET> psdetRepository;
    private GenericRepository<DASH_PYLVR> pylvrRepository;

    public GenericRepository<DASH_PYLVR> PylvrRepository
    {
        get
        {

            if (this.pylvrRepository == null)
            {
                this.pylvrRepository = new GenericRepository<DASH_PYLVR>(c21context);
            }
            return pylvrRepository;
        }
    }

    public GenericRepository<PSDET> PsdetRepository
    {
        get
        {

            if (this.psdetRepository == null)
            {
                this.psdetRepository = new GenericRepository<PSDET>(c21context);
            }
            return psdetRepository;
        }
    }

    public GenericRepository<EMDET> EmdetRepository
    {
        get
        {

            if (this.emdetRepository == null)
            {
                this.emdetRepository = new GenericRepository<EMDET>(c21context);
            }
            return emdetRepository;
        }
    }

    public GenericRepository<EMLVE> EmlveRepository
    {
        get
        {

            if (this.emlveRepository == null)
            {
                this.emlveRepository = new GenericRepository<EMLVE>(c21context);
            }
            return emlveRepository;
        }
    }

    public GenericRepository<EMPOS> EmposRepository
    {
        get
        {

            if (this.emposRepository == null)
            {
                this.emposRepository = new GenericRepository<EMPOS>(c21context);
            }
            return emposRepository;
        }
    }

    public GenericRepository<PSLDW> PsldwRepository
    {
        get
        {

            if (this.psldwRepository == null)
            {
                this.psldwRepository = new GenericRepository<PSLDW>(c21context);
            }
            return psldwRepository;
        }
    }

    public GenericRepository<UPZ88> Upz88Repository
    {
        get
        {

            if (this.upz88Repository == null)
            {
                this.upz88Repository = new GenericRepository<UPZ88>(c21context);
            }
            return upz88Repository;
        }
    }

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                c21context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}