I tried the following but doesn't work (ActivationException).
public interface IOp { object Execute(object value); }
public class SimpleOp : IOp { public object Execute(object value) { return value; } }
public class CompressOp : IOp {
private readonly IOp nextOp;
public CompressOp(IOp nextOp) { this.nextOp = nextOp; }
public object Execute(object value)
{
return this.Compress(this.nextOp.Execute(value));
}
}
var container = new Container();
container.RegisterAll<IOp>(typeof(SimpleOp), typeof(CompressOp));
Container.RegisterDecorator(typeof(IOp), typeof(CompressOp));
var op = container.GetInstance<IOp>(); <-- ActivationException
The IOp interface really doesn't need to be a generic type because it's designed to do same processing for any object. Do I have to force it to be a generic then always use object
as type parameter, just so I can use the decorator pattern?
Try this
var container = new Container();
container.Register<IOp, SimpleOp>();
Container.RegisterDecorator(typeof(IOp), typeof(CompressOp));
var op = container.GetInstance<IOp>();