1

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Thanks there is some interesting reading @L-Three & @jalpesh! Should a caching strategy be implemented with this and are there any patterns or methodologies that work well with ef, unitofwork and repository pattern? Also is there any way of gaining SqlDependancy style behaviour with an Oracle back end?
Nope oracle does not support that. You need to do that manually.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.