dependency injection - IoC and dynamic objects -


I'm having problems finding out how to solve objects that are dependent on objects that are scheduled to be set at runtime Are not I the best way?

Imagine something like a word processor for every document that you open, you probably want to create large objects that depend on the document object.

As an example, you want to get an example of a DocumentEditor:

  public class DocumentEditor {public document editor (ID document, ISPEL checker spell checker, IWordCounter wordCounter) {...}}  

So far, I have considered two approaches, but do not look like a good fit:

Injected The use of factories

The problem with this approach is that you can use every type of factory Could end up with areas which need to create. IE

Public Interface ISPL Checker Factor {Create ISPellChecker (IDDEN document); } Public Interface IWordCounterFactory {Build IWordCounter (IDDEN Document); } Public class DocumentEditorFactory {Public DocumentEditorFactory {ISPellCheckerFactory, spellCheckerFactory, IWordCounterFactory wordCounterFactory} {...} Create Public DocumentEditor {IDocument Document} {...}}

Add another 50 class and you See the problem ...

Use of Nested Containers

The need to create a district factories using nested containers has been removed. It is actually very compact (using Example Integration):

  var child = container.CreateChildContainer (); Child.RegisterInstance & LT; IDocument & gt; (TheDocument); Child.Resolve & LT; DocumentEditor & gt; ();  

For the sake of this approach, the container leaks at all places.

(On one hand, implementation in unity is a bit difficult.) Example:

Hybrid approach

It is possible to add two by implementing a document editor , Which creates nested containers and uses hair containers to solve the dependency.

Paralysis on the best of analysis ...

The approach used in autofacks It's something similar to your first choice, though it eliminates the heavy amount of hand coding.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -