Created
September 24, 2014 22:51
-
-
Save trailmax/7a952d616d42d5a3df7b to your computer and use it in GitHub Desktop.
Trying to fight circular dependency with injections of functions
This file contains 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
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Func<IBar> barFunc = () => | |
{ | |
var barr = new Bar(); | |
return barr; | |
}; | |
Func<IFoo> fooFunc = () => | |
{ | |
var fooo = new Foo(barFunc); | |
return fooo; | |
}; | |
var foo = fooFunc.Invoke(); | |
var bar = new Bar() | |
{ | |
FooFunc = fooFunc, | |
}; | |
bar.DoBar(); | |
} | |
} | |
public interface IFoo | |
{ | |
void DoFoo(); | |
} | |
public class Foo : IFoo | |
{ | |
private readonly Func<IBar> barFunc; | |
public Foo(Func<IBar> barFunc) | |
{ | |
this.barFunc = barFunc; | |
} | |
public void DoFoo() | |
{ | |
var bar = barFunc.Invoke(); | |
bar.DoBar(); | |
Thread.Sleep(1000); | |
Console.WriteLine("Executing Foo.DoFoo"); | |
} | |
} | |
public interface IBar | |
{ | |
void DoBar(); | |
} | |
public class Bar : IBar | |
{ | |
public Func<IFoo> FooFunc { get; set; } | |
public Bar() | |
{ | |
} | |
public void DoBar() | |
{ | |
var foo = FooFunc.Invoke(); | |
foo.DoFoo(); | |
Thread.Sleep(1000); | |
Console.WriteLine("Executing Foo.DoFoo"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment