Last active
April 1, 2019 01:20
-
-
Save ovicrisan/ffd460a0606b4596daccb5e8a10dcc99 to your computer and use it in GitHub Desktop.
.NET Core decorator pattern with Lamar
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
// dotnet add package Lamar | |
using System; | |
using Lamar; | |
namespace LamarDecorator | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var container = new Container(x => | |
{ | |
x.For<IThing>().Use<ThingDecorated>().Ctor<IThing>().Is<Thing>(); | |
}); | |
var thing = container.GetInstance<IThing>(); | |
thing.DoSomething(5); | |
thing.DoSomethingElse("me"); | |
Console.WriteLine("Press any key to continue...."); | |
Console.ReadKey(); | |
} | |
} | |
public interface IThing | |
{ | |
void DoSomething(int n); | |
void DoSomethingElse(string name); | |
} | |
public class Thing : IThing | |
{ | |
public void DoSomething(int n) | |
{ | |
Console.WriteLine("DoSomething(): {0} + 1 = {1}", n, n + 1); | |
} | |
public void DoSomethingElse(string name) | |
{ | |
Console.WriteLine("DoSomethingElse() called by {0}", name); | |
} | |
} | |
public class ThingDecorated : IThing | |
{ | |
private readonly IThing _thing; | |
public ThingDecorated(IThing thing) | |
{ | |
_thing = thing; | |
} | |
public void DoSomething(int n) | |
{ | |
_thing.DoSomething(n); | |
Console.WriteLine("DoSomething() finished."); | |
} | |
public void DoSomethingElse(string name) | |
{ | |
_thing.DoSomethingElse(name); | |
Console.WriteLine("DoSomethingElse() finished"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment