Kamil Będkowski
Kamil Będkowski

Reputation: 1092

How to correctly implement DI using EF (DB first)?

I'm trying to implement DI (using Ninject if it does matter)in my project. For now i have QuestionController,which is using QuestionRepository:

    private readonly IQuestionRepository _questionRepository;

    public QuestionsController(IQuestionRepository questionRepository)
    {
        _questionRepository = questionRepository;
    }

This is ok,but my QuestionRepository needs AskEntities object,where AskEntities is class generated by EF designer. So now in QuestionRepository i have :

 private AskEntities Db; 

How to implement there not class but interface (someting like IAskEntities) ?

Thanks for any help.

Upvotes: 0

Views: 103

Answers (1)

jgauffin
jgauffin

Reputation: 101150

but my QuestionRepository needs AskEntities object,where AskEntities is class generated by EF designer. So now in QuestionRepository i have private AskEntities Db; How to implement there not class but interface (someting like IAskEntities) ?

Well. I assume that your AskEntities object is your DbContext? Since your repository implementation is specific to Entity Framework I see no reason to abstract away that dependency from your repository class.

You could of course send in DbContext base class and use context.Set<Question> to access different sets. But what advantage would that give you? You typically only write integration tests for repository classes, hence no need to create another abstraction.

Upvotes: 2

Related Questions