Dependency injection in domain object extension methods

I am creating a node based CMS web system in asp.net mvc.

After reading several books on dependency injection, I divided my solution into several projects and used abstractions (abstract classes and interfaces).

I cannot figure out how to solve the following type of code in my web project:

myDomainObjectNode.GetChildNodes<SomeSubNodeClass>();

I could do this if the domain object had an INodeRepository reference, but that would mean that I have to carry this dependency in all the domain objects, which is a problem, especially when I have to create new instances. Does the dependency (mostly repository) in your domain object have a bad thing?

My other idea is to achieve this using extension methods. However, extension methods are static in a static class, which in itself cannot be built using IoC. I could solve this with Singleton for an INodeRepository and set it using IoC.

This doesn't seem like an elegant solution ... do you have any other ideas or input?

Thank!

+3
source share
2 answers

I decided to solve this using MVC DependencyResolver to get the INodeService inside the extension method in the MVC web project. It works like a charm, and I don’t think I had serious OO earrings.

0
source

I usually avoid giving the object access to the repositories and hide any problems with saving as much as possible.

myDomainObjectNode, , . factory.

, , Linq, childnodes IEnumerable<BaseNode>.

, , ORM.

, . , .

+1

Source: https://habr.com/ru/post/1793041/


All Articles