Last active
August 29, 2015 14:25
-
-
Save SuperJMN/94add1be566403a018b3 to your computer and use it in GitHub Desktop.
Breaking down cyclic dependency
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
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