Last active
September 16, 2023 03:57
-
-
Save vhenzl/4537030 to your computer and use it in GitHub Desktop.
DI, IoC, DIP
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
Dependency Injection a Inversion of Control | |
https://twitter.com/geekovo/status/290061513300000768 | |
http://rarous.net/weblog/431-dip-ioc-a-di-dil-prvni.aspx | |
http://rarous.net/weblog/432-dip-ioc-a-di-dil-druhy.aspx |
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 ArticleRepository | |
{} | |
public class PublishingManager | |
{ | |
ArticleRepository _repository; | |
public PublishingManager() | |
{ | |
_repository = new ArticleRepository(); | |
} | |
} |
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 ArticleRepository | |
{} | |
// používám DI, ale nemám IoC = injektnu závislost z venku, ale PublishingManager1 si stále kontroluje, | |
// jakou konkrétní třídu použije | |
public class PublishingManager1 | |
{ | |
ArticleRepository _repository; | |
public PublishingManager1(ArticleRepository repository) | |
{ | |
_repository = repository; | |
} | |
} | |
// pak to někde zavolám | |
var manager1 = new PublishingManager1(new ArticleRepository()); | |
// a nebo si ušetřím práci a použiji přetížení konstruktoru k vytvoření ArticleRepository, | |
// protože stejně jinou implementaci bez použití abstrakce (tedy bez IoC) použít nemůžu (de facto) | |
public class PublishingManager2 | |
{ | |
ArticleRepository _repository; | |
public PublishingManager2() | |
:this(new ArticleRepository()) | |
{} | |
public PublishingManager2(ArticleRepository repository) | |
{ | |
_repository = repository; | |
} | |
} | |
// a pak opět někde zavolám | |
var manager2 = new PublishingManager2(); |
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
// mám abstrakci, v tomto případě rozhraní | |
public interface IRepository | |
{} | |
// nějaká konkrétní implementace | |
public class ArticleRepository : IArticleRepository | |
{} | |
// stále používám DI a závislost předám z venku | |
// zároveň jsem zavedl i IoC, protože PublishingManager neříká (nekontroluje), | |
// jaká konkrétní implementace abstrakce IArticleRepository má být použita a nezajímá se o to. | |
// Zodpovědnost za to byla předána ven (zinvertována) | |
public class PublishingManager | |
{ | |
IArticleRepository _repository; | |
public PublishingManager(IArticleRepository repository) | |
{ | |
_repository = repository; | |
} | |
} | |
// pak to někde zavolám | |
var manager = new PublishingManager(new ArticleRepository()); | |
// ale nic mi nebrání použít třeba návrhový vzor dekorátor, implementovat třeba kešování a inicializovat | |
// PublishingManager následujícím způsobem, protože on nepožaduje konkrétní třídu | |
var manager = new PublishingManager(new CacheRepository(new ArticleRepository())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment