mcinnes01
mcinnes01

Reputation: 165

How can I make my data access layer better?

I have been playing for a while with entity framework and repository pattern, but I wanted to know if what I am doing currently is in any way wrong or how I could improve it.

My scenario often requires me to ReadOnly from our HR systems Oracle database, and then for the application I am working on I tend to store data for that application in its own MS Sql database and occasionally in some of my own tables that I have created in the HR Oracle database.

I aim to get a reusable, robust approach to access data and one that ensures data can be used in an efficient and reliable way. I also need to work out the best way for creating some more specific data access methods for certain entities e.g. my generic repository has no create, edit or delete methods as I want to remove the risk of edit data in the HR database, but as I said I may have my own tables that I do want to edit, create and delete data from in the HR database. Likewise I may want to use my generic repository and unitofwork to access multiple datasources which I don't know how to go about currently.

Additionally I don't use any Interfaces currently and wondered if I can or should implement these such as, IRepository, IUnitOfWork, IContext etc?

Currently I use a generic repository pattern against DbContext to provide some basic methods for handling data. I then have a unitofwork implementation so that I can easily access my various repositories for my entity models.

e.g.

UnitOfWork uow = new UnitOfWork();
var data = uow.StaffRepository.Get();

Below is my implementation so far:

Generic Repository

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

This is my UnitOfWork:

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

Any help would be much appreciated,

Andy

Upvotes: 1

Views: 1293

Answers (2)

Jalpesh Vadgama
Jalpesh Vadgama

Reputation: 14216

Use dependency Injection with your dal and also use interface so that you can don't have to depend on particular class.

see following link. http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

Which is a repository and IUnitOfWork demo.

Upvotes: 1

L-Four
L-Four

Reputation: 13531

You can have a look at the code in the series about architecture here. It uses interfaces heavily together with IOC.

Upvotes: 1

Related Questions