0

I'm trying to make a base class for crud ops but can't quite figure out how to get this part wired up or if it's possible. I'm using an EDMX w/ generated dbcontexts and pocos, so, ideally, I'd like to create a base class from where I can derive all my crud methods.

Interface:

public interface IGenericCrud<T> where T : class
{
    void Add(T entity);
}

Implementation:

public abstract class MyImplementation : IGenericCrud<KnownModel>
{
    protected myEntities context;

    public MyImplementation()
    {
        context = new myEntities();
    }

    void Add(KnownModel entity)
    {
        // This doesn't work, but it's what I'd like to accomplish
        // I'd like to know if this possible without using ObjectContexts
        context.KnownModel(add entity);
    }
}
Lexi Marie
  • 49
  • 2
  • 5

1 Answers1

1

I believe you should look into the repository pattern. That seems to be what you are looking for.

RBZ
  • 2,034
  • 17
  • 34
  • You can try context.Set().Add(entity). You can also keep the class generic. – divega Oct 09 '13 at 03:21
  • Thanks for your advice. I was attempting to use the repository/UoW pattern with an existing EDMX and auto-generated T4 contexts and models. I've no need to create context classes or pocos for entity as I'm using a very simple database. The issue I'm having is answered better by divega than by a link to a design pattern. – Lexi Marie Oct 09 '13 at 04:14
  • @LexiMarie Your question is a description of the generic repository pattern. You don't need to create context classes or pocos, but you do need to put a context into the repository. Here is another example with more links that may help: http://stackoverflow.com/a/19180319/150342. – Colin Oct 09 '13 at 08:24