Robiłem zgodnie z manualem przeczytałem kilka artykułów i nie znalazłem odpowiedzi.
Powinienem otrzymać 2 obiekty z dekoratorem.
Znajdzie się ktoś kto rzuci na to swoim okiem.?
using System;
using System.Collections.Generic;
using Autofac;
using System.Reflection;
namespace test
{
public interface ICommand
{
string Execute();
}
public class SaveCommand : ICommand
{
public string Execute()
{
return "Saving the image...";
}
}
public class OpenCommand : ICommand
{
public string Execute()
{
return "Opening an image...";
}
}
public class ToolbarButton : ICommand
{
readonly ICommand _command;
public ToolbarButton(ICommand inner)
{
_command = inner;
}
public string Execute()
{
return _command.Execute() + "DECORATOR";
}
}
public class EditorWindow
{
readonly IEnumerable<ToolbarButton> _toolbarButtons;
public EditorWindow(IEnumerable<ToolbarButton> toolbarButtons)
{
_toolbarButtons = toolbarButtons;
}
public void Show()
{
foreach (var tb in _toolbarButtons)
Console.WriteLine(tb.Execute());
}
}
class Program
{
static void Main()
{
var builder = new ContainerBuilder();
builder.RegisterType<SaveCommand>().Named<ICommand>("command");
builder.RegisterType<OpenCommand>().Named<ICommand>("command");
builder.RegisterType<ToolbarButton>();
builder.RegisterType<EditorWindow>();
builder.RegisterDecorator<ICommand>(
(c, inner) => new ToolbarButton(inner),
fromKey: "command");
using (var container = builder.Build())
{
var window = container.Resolve<EditorWindow>();
window.Show();
}
Console.WriteLine("Done. Press any key.");
Console.ReadKey(true);
}
}
}