Skip to content

Instantly share code, notes, and snippets.

@ovicrisan
Last active April 1, 2019 01:20
Show Gist options
  • Save ovicrisan/ffd460a0606b4596daccb5e8a10dcc99 to your computer and use it in GitHub Desktop.
Save ovicrisan/ffd460a0606b4596daccb5e8a10dcc99 to your computer and use it in GitHub Desktop.
.NET Core decorator pattern with Lamar
// 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