Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF: Entity Dependency injection

Does EF 6 allow dependency injection in my entities? Below is an example of what is needed.

class User
{
    private IPasswordEncryptor _passwordEncryptor;

    public User(IPasswordEncryptor passwordEncryptor)
    {
       _passwordEncryptor = passwordEncryptor;
    }

    ...
}

So, I need to find a way to inject the IPasswordEncryptor into User in Entity Framework. It's a general question, I just provided an example above.

like image 391
Markus Avatar asked Nov 03 '22 17:11

Markus


1 Answers

For those using a DI container, you might try to inject the dependencies into the aggregate root. That leads to a whole host of problems, which are so numerous I won’t derail a perfectly good post by getting into it. Instead, there’s another, more intention-revealing option: the double dispatch pattern.

Quoted from a post by Jimmy Bogard. Find that post (which includes an example) here: http://lostechies.com/jimmybogard/2010/03/30/strengthening-your-domain-the-double-dispatch-pattern/.

like image 137
JefClaes Avatar answered Nov 09 '22 07:11

JefClaes