0

So I'm making a business data layer in my app so I obviously need API for accessing my data.

How do I evade choosing between:

  • Implementing ICreatable, IReadable, IUpdatable, IDeletable which are practically identical for each class which makes me write the same code a lot

  • Extending an existing generic data class which has all 4 CRUD which I do not like since I want some data to be read only

What I'd really like is multiple inheritance so that I can simply extend with Creatable, Removable, Updatable or Removable abstract classes when I need em

I was thinking about making abstract CRUDable and Readable classes which seems to be the least amount of repetitive coding while avoiding multiple inheritance and maintenance issues

ditoslav
  • 4,563
  • 10
  • 47
  • 79
  • You don't need multiple inheritance, just inheritance. Have a read-only class, then an update-able class which inherits from read-only and a delete-able class which inherits from update-able. (it's unlikely you *need* write-only DB and delete tends to imply update). – freedomn-m Jul 28 '15 at 08:16

2 Answers2

1

If I understand your question correctly, I think what you are looking for is to make use of composition. For example:

public class OrderRepository<Order> : ICreateable<Order>, IReadable<Order>, IUpdateable<Order>, IDeleteable<Order>
{
    private ICreateable<Order> _createable;
    private IReadable<Order> _readable;
    private IUpdateable<Order> _updateable;
    private IDeleteable<Order> _deleable;

    public void Add(Order item)
    {
        _createable.Add(item);
    }

    public Order GetById(int id)
    {
        return _readable.GetById(id);
    }
}

public class Creatable<T> : ICreateable<T>
{
    public void Add(T item)
    {
        //Add to entity framework?
    }
}

public class Readable<T> : IReadable<T>
{
    public T GetById(int id)
    {
        //Get from entity framework?
    }
}

This way you only write the generic components once, and chose which repositories support them via implementing their corresponding interface

David Masters
  • 8,069
  • 2
  • 44
  • 75
0

You can use Unit-Of-Work, to have a practical repository for all your classes. Take a look at my implementation here

If you want to add more models, you simply have to put 1 line in IUowData interface, and one small method in UowData class

I hope this helps.

stann1
  • 576
  • 3
  • 11
  • But the UOW pattern is already implemented into entity framework which I'm using on my data access layer so I would have 2 UOW layers, right? – ditoslav Jul 28 '15 at 07:53