Joachim
Joachim

Reputation: 173

C# MVC4 EF Ninject - repository in domain class

I have a class:

Question

With the properties:

Bool IsTextAnswer
List<String> Answers

The answers to a question are: 2-4 answers stored in the list of answers OR 4 colors (stored in the db, for every question the same). The boolean decides if the answers are from the list or the colors.

The colors are just a table in the database with 4 rows, for every color a row. These colors aren't linked to anything so I made a new repository: colorRepository from which I can get all the colors.

I did this in my code: Constructor

public Question(IColorRepository colorRepository) {
   _colorRepository = colorRepository;
}

In the getter of the answers I try to do something like this:

if(IsTextAnswers) 
    return answers
return _colorRepository.FindAll

But ninject doesn't work because it's not a controller so I get the message that there is no parameterless constructor.

How can I retrieve my 4 colors from the database?

I only need the be able to read from the database, the questions + answers are made by an administrator in a java program.

Upvotes: 1

Views: 138

Answers (1)

Joe Ratzer
Joe Ratzer

Reputation: 18549

It looks like Question is an entity, and entity framework needs it to have a parameterless constructor.

Your repository should be a separate class, the domain objects should be POCOs and not have data access code in them.

Upvotes: 1

Related Questions