Skip to content

Instantly share code, notes, and snippets.

@SuperJMN
Last active August 29, 2015 14:25
Show Gist options
  • Save SuperJMN/94add1be566403a018b3 to your computer and use it in GitHub Desktop.
Save SuperJMN/94add1be566403a018b3 to your computer and use it in GitHub Desktop.
Breaking down cyclic dependency
namespace SampleForMark
{
using System;
class Program
{
static void Main(string[] args)
{
var factory = new Factory(injectedFactory => new Reader(injectedFactory));
factory.ExecuteSomething();
}
}
internal class Factory
{
private readonly Func<Factory, Reader> readerProvider;
public Factory(Func<Factory, Reader> readerProvider)
{
this.readerProvider = readerProvider;
}
public void ExecuteSomething()
{
var reader = readerProvider(this);
var readContents = reader.Read();
Console.WriteLine(readContents);
}
}
internal class Reader
{
private readonly Factory factory;
public Reader(Factory factory)
{
this.factory = factory;
}
public object Read()
{
return "Hey, I got my factory! " + factory;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment