Skip to content

Instantly share code, notes, and snippets.

@trailmax
Created September 24, 2014 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trailmax/7a952d616d42d5a3df7b to your computer and use it in GitHub Desktop.
Save trailmax/7a952d616d42d5a3df7b to your computer and use it in GitHub Desktop.
Trying to fight circular dependency with injections of functions
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