Created
August 20, 2020 19:52
-
-
Save dcomartin/a631b002ce761ac5d64d77ab38c705e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity; | |
using Unity.Lifetime; | |
namespace UnityFactory | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var container = new UnityContainer(); | |
container.RegisterFactory<IFoo>(x => | |
{ | |
// This fails because x is not the container/child that is trying to resolve of IFoo, but rather the parent container that did the RegisterFactory | |
return new Foo(x.Resolve<IBar>()); | |
}, new ContainerControlledLifetimeManager()); | |
var child = (container as IUnityContainer).CreateChildContainer(); | |
child.RegisterType<IBar, Bar>(new ContainerControlledLifetimeManager()); | |
// This fails | |
var bar = child.Resolve<IFoo>(); | |
} | |
} | |
public interface IFoo { } | |
public class Foo : IFoo | |
{ | |
public Foo(IBar bar) | |
{ | |
} | |
} | |
public interface IBar {} | |
public class Bar : IBar {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Search issues in DI repo, you'll find reason for that