How to understand the rule: “Store in IoC container only services. Do not store any entities.”?

Short answer: entities should not depend on services! This is a huge part of the "inversion" part of dependency inversion. As stated in the post you linked to: I found the design of the question example is wrong in a sense that an entity (which MyClass is) should NOT have dependencies on infrastructure (or any other) services MyClass in your example, depends on three cross-cutting concerns, plus IApplicationSettings The cross-cutting concerns should be introduced using AOP/dynamic proxy techniques, or pubsub-style events if you prefer.

And MyClass should be injected with settings specific to its functionality by whatever service is in charge of creating instances of it. For example PersonFactory could be injected with IApplicationSettings then create Person objects, passing appSettings. SomePersonRelatedSetting into the Person constructor.

Short answer: entities should not depend on services! This is a huge part of the "inversion" part of dependency inversion. As stated in the post you linked to: I found the design of the question example is wrong in a sense that an entity (which MyClass is) should NOT have dependencies on infrastructure (or any other) services.

MyClass, in your example, depends on three cross-cutting concerns, plus IApplicationSettings. The cross-cutting concerns should be introduced using AOP/dynamic proxy techniques, or pubsub-style events if you prefer. And MyClass should be injected with settings specific to its functionality by whatever service is in charge of creating instances of it.

For example, PersonFactory could be injected with IApplicationSettings, then create Person objects, passing appSettings. SomePersonRelatedSetting into the Person constructor.

I understand it that way there are services (with contracts), there are entities (business), and there are infrastructure stuff LogWriter, AuthenticationProvider - creating/hosting service I initiate it with some infrastructure stuff (publish dependencies on infrastructure). What I still do not understand am I right in that or not. – Roman Pokrovskij Aug 24 at 8:57 There's nothing wrong with application services depending on infrastructure services; I didn't see anything in the linked article arguing against URL1 may still be cleaner and reduce your number of constructor parameters to use AOP/dynamic proxy/pubsub techniques, the former two of which are IoC container features anyway.

But it's not as crucial as avoiding entity to service dependencies. – Domenic Aug 24 at 11:56 That solves my problems with understanding. Thank you.

– Roman Pokrovskij Aug 24 at 13:15.

The rule "Store in IoC container only services. Do not store any entities. " means that you should register in IoC container pair interace - implementation.

This rule is a result of dependency inversion principle. According to your example this means that you should extract public interface from your MyClass and use it instead of MyClass implementation.

I think you missed the point; MyClass is an entity, and so extracting an interface for DIP purposes is not necessary. (It might still be useful e.g. For mocking, but that's a separate issue.) – Domenic Aug 24 at 4:57 @Domenic than infrastructure logic should be moved from MyClass into MyClassService, which actually should implement IMyClassService interface – hazzik Aug 24 at 5:05.

The rule that says "Store in IoC container only services. Do not store any entities. " is talking from conceptual perspective and not from implementation perspective.

Conceptually, entity represents data (or state) and perhaps data related rules (as methods). Service represents functionality exposed via published contract and generally, state-full services is not a good idea. So although C# class construct would be used to represent both service implementation and entity implementation - what this rule is saying is that only services should be put into IoC.

IoC really acts as a configurable super-flexible factory that provides correct service implementation for the required service contract. Again, one can have pattern such as ActiveRecord where perhaps data and persistence functionality are merged - I as such don't like the pattern but there are many followers and typical usage would put such entities into IoC container. However, important difference would be probably the entity would be registered against some contract (and not implementation) Again note that your code has dependencies on service contracts and not on specific service implementation.

Now, dependencies can be exposed and resolved in multiple ways. You can mark dependencies by putting them as constructor parameters OR as public property getter/setter OR using declarative syntax such as attributes. These are typically resolved automatically by DI framework.

However, you can always get/resolve some service by making explicit call such as ILog log = DIContainer.Get();. So as far as Infrastructure services goes, I think that what the blog author is saying is that don't mark infrastructure services as explicit dependencies (by constructor args or properties etc). You can resolve them using code (as quoted above) or perhaps using simple singleton helpers etc.In a way, he is right because one can always assumes that every service/class depends on infrastructure and so dependencies need not be stated using public API.

Good example with the active record pattern. It is very non-IoC compliant, because you end up with an entity (e.g. Person) depending on a service (e.g. IPersistenceService). If you wish to fully embrace IoC, your entities should not depend on services active record-style.

– Domenic Aug 24 at 4:59 Sorry, may be I still asking wrong question, but .. according this rule, could service realization publish it dependencies on infrastructure stuff such LogWriter, AuhtenticationProvider? This stuff is not entity-related, at least in the service conceptual layer. Possibly I could agree, publishing dependencies on infrastructure have a little common with dependency injection principle.

But I publish them because such dependencies exist and something should resolve them.. why not IoC container? – Roman Pokrovskij Aug 24 at 9:25.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions