diff --git a/15-design-patterns-in-csharp-slides.html b/15-design-patterns-in-csharp-slides.html index 190ffa0..cf8a800 100644 --- a/15-design-patterns-in-csharp-slides.html +++ b/15-design-patterns-in-csharp-slides.html @@ -13,10 +13,10 @@ /* buutti.css */ /* @theme buutti */div#\:\$p>svg>foreignObject>section .columns{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns12{display:grid;grid-template-columns:1fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns21{display:grid;grid-template-columns:2fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns32{display:grid;grid-template-columns:3fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns23{display:grid;grid-template-columns:2fr 3fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns111{display:grid;grid-template-columns:1fr 1fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .centered{display:flex;flex-direction:column;justify-content:center;text-align:center}div#\:\$p>svg>foreignObject>section .tableborderless td,div#\:\$p>svg>foreignObject>section th{border:none!important;border-collapse:collapse}div#\:\$p>svg>foreignObject>section.extra{background-color:#5d275d;background-image:linear-gradient(to bottom,#401a40,#1d0c1d);color:white}div#\:\$p>svg>foreignObject>section.extra a{color:rgb(145,255,209)}div#\:\$p>svg>foreignObject>section.exercise{background-color:#29366f;background-image:linear-gradient(to bottom,#20636a,#173742);color:white}div#\:\$p>svg>foreignObject>section.exercise a{color:rgb(211,173,255)} -/* @theme l88lgdm6g50jnf1zbzajbvpkez0urw5hs65fwufujn */div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]{columns:initial!important;display:block!important;padding:0!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:before,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:before{display:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]{all:initial;display:flex;flex-direction:row;height:100%;overflow:hidden;width:100%}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container][data-marpit-advanced-background-direction=vertical]{flex-direction:column}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split]>div[data-marpit-advanced-background-container]{width:var(--marpit-advanced-background-split,50%)}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split=right]>div[data-marpit-advanced-background-container]{margin-left:calc(100% - var(--marpit-advanced-background-split, 50%))}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure{all:initial;background-position:center;background-repeat:no-repeat;background-size:cover;flex:auto;margin:0}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure>figcaption{position:absolute;border:0;clip:rect(0,0,0,0);height:1px;margin:-1px;overflow:hidden;padding:0;white-space:nowrap;width:1px}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content],div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo]{background:transparent!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo],div#\:\$p>svg[data-marpit-svg]>foreignObject[data-marpit-advanced-background=pseudo]{pointer-events:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background-split]{width:100%;height:100%}
+/* @theme xun2jiy1c7sz4e4w7x1xbtx95u4cs3ssdsjcjf4v */div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]{columns:initial!important;display:block!important;padding:0!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:before,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:before{display:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]{all:initial;display:flex;flex-direction:row;height:100%;overflow:hidden;width:100%}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container][data-marpit-advanced-background-direction=vertical]{flex-direction:column}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split]>div[data-marpit-advanced-background-container]{width:var(--marpit-advanced-background-split,50%)}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split=right]>div[data-marpit-advanced-background-container]{margin-left:calc(100% - var(--marpit-advanced-background-split, 50%))}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure{all:initial;background-position:center;background-repeat:no-repeat;background-size:cover;flex:auto;margin:0}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure>figcaption{position:absolute;border:0;clip:rect(0,0,0,0);height:1px;margin:-1px;overflow:hidden;padding:0;white-space:nowrap;width:1px}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content],div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo]{background:transparent!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo],div#\:\$p>svg[data-marpit-svg]>foreignObject[data-marpit-advanced-background=pseudo]{pointer-events:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background-split]{width:100%;height:100%}

Design Patterns in C#

-
+

Overview

  • The Singleton Pattern
  • @@ -24,10 +24,10 @@
  • Dependency Injection
-
+

The Singleton Pattern

-
+

The problem

  • In most cases, it makes no sense to create an instance of a class every time its members need to be accessed @@ -45,7 +45,7 @@
-
+

The solution

  • The singleton class is a class that benefits from all the perks of a non-static class (non-static members, inheritance, referencing…), but only one (or zero) instances of it ever exists during the lifetime of your application
  • @@ -57,7 +57,7 @@
-
+

A singleton implementation could look something like this:

@@ -98,14 +98,14 @@
-
+

Implementing a singleton pattern

  • The exact implementation of the singleton is out of the scope of this course, but it is important to understand that it exists and what its purpose is
  • Multitude of examples for different use cases are available and can be found by googling
-
+

Reflection

  • Reflective programming or reflection is the ability for the program to examine or modify its own structure and behaviour
  • @@ -138,7 +138,7 @@ method.Invoke(foo, null);
-
+

Attributes

  • Attributes can be used to extend methods, classes, or even entire programs with new metadata @@ -157,7 +157,7 @@ method.Invoke(foo, null);
-
+
  • Attributes can also have parameters:

    @@ -192,7 +192,7 @@ method.Invoke(foo, null);
-
+

Custom attributes

  • You can write your own custom attributes by inheriting from the Attribute class
    // This defaults to Inherited = true.
    @@ -214,10 +214,10 @@ method.Invoke(foo, null);
     
-
+

Dependency Injection

-
+

The problem

  • Traditionally, when new objects of classes are instantiated, the consuming class handles the creation of the objects
  • @@ -227,18 +227,19 @@ method.Invoke(foo, null);
  • This is called tight coupling
+
  • What we want instead is loose coupling, where components have little or no knowledge about separate components' definitions, and a change in one component doesn't necessitate a change in another
  • -
    +

    The solution

    • What if, instead of directly creating the objects, they were provided by some interface that takes care of the creation?
    • This way, even if the base class changes, the consuming classes won't care because they only know about the provider
    • This provider is called Container, and the functionality being injected is called Service
    • -
    • In ASP.NET, this container system is built in
    • +
    • In ASP.NET (Microsoft's framework for building web applications), this container system is built in
    -
    +

    Dependency injection in ASP.NET

    public class HomeController : Controller
     {
    @@ -252,7 +253,7 @@ method.Invoke(foo, null);
     }
     
    -
    +

    Design Patterns

    • If the concepts of a singleton and dependency injection flew over your head, don't worry about it
    • diff --git a/15-design-patterns-in-csharp.md b/15-design-patterns-in-csharp.md index 53d573a..74a2f8f 100644 --- a/15-design-patterns-in-csharp.md +++ b/15-design-patterns-in-csharp.md @@ -207,13 +207,14 @@ void TraceMethod() * Many classes change their functionality throughout the development of any project * This means that also _every single_ consuming class has to change * This is called *__tight coupling__* +* What we want instead is [***loose coupling***](https://en.wikipedia.org/wiki/Loose_coupling), where components have little or no knowledge about separate components' definitions, and a change in one component doesn't necessitate a change in another ### The solution * What if, instead of directly creating the objects, they were provided by some interface that takes care of the creation? * This way, even if the base class changes, the consuming classes won't care because they only know about the provider * This provider is called *__Container__*, and the functionality being injected is called *__Service__* -* In ASP.NET, this container system is built in +* In ASP.NET (Microsoft's framework for building web applications), this container system is built in ### Dependency injection in ASP.NET