Question

im sorry if the title isnt enlightening enough after a while i couldnt come with a phrase to explain my problem.

Its like this: I'm using EF6, Database first approach. All my entities have an ID and an Enabled property. I have the EDMX file on my DAL and now i need to design the Business Layer (BLL for me).

Since i will need to validate what data comes from the DAL and is visible to the rest of the app i decided to create an interface for the common operators, create a base class that implements that and have all my custom database-tables\entities providers inherit that.

If that doesnt make sense, here is what i'm doing: (All of this is in BLL)

public interface IEntityManager<T> where T : class {
        void Add(T e, bool commit=false);
        void Delete(T e);
        void DeleteByID(int id);
        void Update(T e);
        IQueryable<T> Search(Expression<Func<T, bool>> predicate);
        IQueryable<T> GetAll();
        T Get(int id);
    }

Now, the base class that implements the common operations is this: (edited for readability and brevity)

 public class EntityManager<TEntity> : IDisposable, IEntityManager<TEntity> where TEntity : class {
        private readonly DbContext _ctx;
        private readonly DbSet<TEntity> _set;

        public EntityManager() {
            _ctx = new DomainModelFactory();
            _set = _ctx.Set<TEntity>();
        }

        public void Add(TEntity e) {
            _set.AddOrUpdate(e);
        }            

//other methods here

        public TEntity Get(int id) {
            return _set.Find(id);
        }


    }

To create managers that will actually access the data and make it usable across the application, i'm planning on doing something like:

public class VenueManager: EntityManager<Venue> {
        public VenueManager():base()  {
        }
    }

For the class that will manage my Venue entity. I am creating classes instead of using something like

var venueManager = new EntityManager<Venue>();

because these classes will have some specialized methods. I hope you could understand what i have done so far (and it works)

Now, my problem is that every entity has two common fields: ID (int) and Enabled (bool) I am to use these when deleting by id and when coding Get and GetAll because i must only get items that have Enabled=true. Now, i know that since i'm create Manager classes i could just use c&p the code, but i was wondering, is there a way that i can code this in the EntityManager class and have all the managers inherit the method but using the concrete classes ?

something like this: In EntityManager class:

 public TEntity Get(int id) {
            return _set.Where(T.ID==id);
        }

Thats basically what i want: a way to use these properties at the base class (EntityManager) where they matter (methods deletebyid, get and getall) in a way that all Manager classes (ManagerVenue in the example) inherit these methods so that i wont have to write them all over and over? (got some 100 entities)

Was it helpful?

Solution

You should define a base class or interface for all your entities, something like:

public class Entity {
    public int Id { get; set; }
    public bool Enabled { get; set; }
}

And in your EntityManager class add a generic constraint to TEntity like where TEntity : Entity

Then you will be able to use the common properties Id and Enabled inside your base class EntityManager.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top