What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 128 Stack Exchange communities.

What's wrong with the following question? I get this error:

Oops! Your question couldn't be submitted because:

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.


I'm wondering if there's a possibility to change Dependency Injection configuration file at a runtime and force DI container to re-resolve object graph created before according to this new configuration.

To clarify my idea, I'll get you a simple example:

Imagine that you've got the following project structure:

  • Common.dll library which contains IDoable interface:

    public interface IDoable
    {
        void Do();
    }
  • LibraryA.dll library which requires Common.dll and contains a realization of IDoable interface:

    public class A : IDoable
    {
        public void Do()
        {
            Console.WriteLine("A.Do()");
        }
    }
  • LibraryB.dll library which is quite similar to LibraryA.dll:

    public class B : IDoable
    {
        public void Do()
        {
            Console.WriteLine("B.Do()");
        }
    }
  • A Test.exe console application which references only to Common.dll library and uses DI container to resolve references to the IDoable interface:

    IDoable instance = _Container.Resolve<IDoable>();
    instance.Do();

DI configuration file contains the following section (currently I'm using Autofac, but it doesn't really matter):


    <autofac>
        <components>
            <component
                    type="LibraryA.A, LibraryA"
                    service="Common.IDoable, Common" />
        </components>
    </autofac>

So, when I run my Test.exe, I'll see the output from LibraryA.dll.

Everything is going OK so far. Now imagine that you want to switch your application from LibraryA.dll to LibraryB.dll in a runtime, without restarting the application. Any ideas? OK, you can use FileSystemWatcher to monitor changes in DI configuration file and reinitialize DI container every time the file is changed. So, if you'll change configuration file accordingly and call again


    IDoable instance = _Container.Resolve<IDoable>();
    instance.Do();
you'll see the output from LibraryB.dll.

OK, finally we're facing the problem. When using DI, you don't resolve references every time before using them. Normally, you resolve object graph in some initialization code. So, even after you changed the config file and DI container is reconfigured, your previously resolved objects stay the same until you re-resolve them. And there could be a lot of such objects, so calling Resolve<T>() every time before using object may be very painful.

So, how would you on-the-fly update previously resolved objects after changing DI configuration? Or maybe there is some DI container already able to do that automatically?

share|improve this question
4  
My best guess would be that the code-finder misinterprets your <pre><code> as being something you should have indented. Does it work if you get rid of the manual code blocks, and indent all code underneath the list items by 8 spaces from the left? –  Tim Stone Dec 23 '11 at 15:48
1  
In this post all your code blocks have 4 spaces too much of indentation and an empty first row. –  N.N. Dec 23 '11 at 15:49
1  
This post talks about some of the heuristics used to determine poorly formatted code on SO (which is, itself, linked to from this answer by Jeff Atwood. –  jadarnel27 Dec 23 '11 at 15:55

1 Answer 1

up vote 2 down vote accepted

If you want to keep the bulleted list, use 8 spaces to indent each line of a code block that's in a list item. Doing so rather than using <pre> tags also allows your code blocks to be highlighted by prettify.


I'm wondering if there's a possibility to change Dependency Injection configuration file at a runtime and force DI container to re-resolve object graph created before according to this new configuration.

To clarify my idea, I'll get you a simple example:

Imagine that you've got the following project structure:

  • Common.dll library which contains IDoable interface:

    public interface IDoable
    {
        void Do();
    }
    
  • LibraryA.dll library which requires Common.dll and contains a realization of IDoable interface:

    public class A : IDoable
    {
        public void Do()
        {
            Console.WriteLine("A.Do()");
        }
    }
    
  • LibraryB.dll library which is quite similar to LibraryA.dll:

    public class B : IDoable
    {
        public void Do()
        {
            Console.WriteLine("B.Do()");
        }
    }
    
  • A Test.exe console application which references only to Common.dll library and uses DI container to resolve references to the IDoable interface:

    IDoable instance = _Container.Resolve<IDoable>();
    instance.Do();
    

DI configuration file contains the following section (currently I'm using Autofac, but it doesn't really matter):

<autofac>
    <components>
        <component
                type="LibraryA.A, LibraryA"
                service="Common.IDoable, Common" />
    </components>
</autofac>

So, when I run my Test.exe, I'll see the output from LibraryA.dll.

Everything is going OK so far. Now imagine that you want to switch your application from LibraryA.dll to LibraryB.dll in a runtime, without restarting the application. Any ideas? OK, you can use FileSystemWatcher to monitor changes in DI configuration file and reinitialize DI container every time the file is changed. So, if you'll change configuration file accordingly and call again

IDoable instance = _Container.Resolve<IDoable>();
instance.Do();

you'll see the output from LibraryB.dll.

OK, finally we're facing the problem. When using DI, you don't resolve references every time before using them. Normally, you resolve object graph in some initialization code. So, even after you changed the config file and DI container is reconfigured, your previously resolved objects stay the same until you re-resolve them. And there could be a lot of such objects, so calling Resolve<T>() every time before using object may be very painful.

So, how would you on-the-fly update previously resolved objects after changing DI configuration? Or maybe there is some DI container already able to do that automatically?

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .