Dependency injection and Entity Framework

前端 未结 1 1650
忘掉有多难
忘掉有多难 2021-01-07 05:57

I\'m doing a wpf application using MVVM light with its Ioc SimpleIoc.

I implemented the repository pattern like this :

 public interfac         


        
相关标签:
1条回答
  • 2021-01-07 06:12

    You shouldn't inject entities. Entities are not services. The object graphs that you build using dependency injection, should solely consist of services. Anything that contains runtime data (entities, messages, DTOs) should be passed through the built object graph using method calls.

    Take a look for instance at this and this answer and this blog post.

    Mixing data and behavior in a single class makes it much harder to test you DI configuration and makes it hard to apply cross-cutting concerns. But besides that, injecting runtime data (such as entities) into a service's constructor results in ambiguity, because it is unclear which exact entity to inject into the constructor. Take for instance some ICustomerService that depends on a Customer entity in its constructor. Which entity should we inject here, since we might have thousands. Although this can be solved by implementing the selection criteria in the location where we create the ICustomerService implementation (our Composition Root), this makes the DI configuration very complex, makes it really hard to verify the configuration and results in business logic in a part of the application that should not contain any business logic.

    0 讨论(0)
提交回复
热议问题