Tag: decorator

如何在SimpleInjector 2.6.1+中unit testing开放的通用装饰器链

给定以下使用SimpleInjector的开放式通用deocrator链: container.RegisterManyForOpenGeneric(typeof(IHandleQuery), assemblies); container.RegisterDecorator( typeof(IHandleQuery), typeof(ValidateQueryDecorator) ); container.RegisterSingleDecorator( typeof(IHandleQuery), typeof(QueryLifetimeScopeDecorator) ); container.RegisterSingleDecorator( typeof(IHandleQuery), typeof(QueryNotNullDecorator) ); 使用SimpleInjector 2.4.0,我能够使用以下代码对此进行unit testing以断言装饰链: [Fact] public void RegistersIHandleQuery_UsingOpenGenerics_WithDecorationChain() { var instance = Container .GetInstance<IHandleQuery>(); InstanceProducer registration = Container.GetRegistration( typeof(IHandleQuery)); instance.ShouldNotBeNull(); registration.Registration.ImplementationType .ShouldEqual(typeof(HandleFakeQueryWithoutValidator)); registration.Registration.Lifestyle.ShouldEqual(Lifestyle.Transient); var decoratorChain = registration.GetRelationships() .Select(x => new { x.ImplementationType, x.Lifestyle, }) .Reverse().Distinct().ToArray(); decoratorChain.Length.ShouldEqual(3); decoratorChain[0].ImplementationType.ShouldEqual( typeof(QueryNotNullDecorator)); decoratorChain[0].Lifestyle.ShouldEqual(Lifestyle.Singleton); decoratorChain[1].ImplementationType.ShouldEqual( typeof(QueryLifetimeScopeDecorator)); […]

在structure-map 3中为DecorateAllWith()方法定义filter

我使用以下语句来装饰我的所有ICommandHandlers与Decorator1 : ObjectFactory.Configure(x => { x.For(typeof(ICommandHandler)).DecorateAllWith(typeof(Decorator1)); }); 但是因为Decorator1实现了ICommandHandlers ,所以Decorator1类也会自行修饰。 所以,问题是当我注册所有ICommandHandler时, Decorator1无意中注册。 我怎样才能过滤DecorateWithAll()以装饰除Decorator1之外的所有ICommandHandler Decorator1 ?

类装饰器声明静态成员(例如,用于log4net)?

我正在使用log4net,我们的代码中有很多这样的东西: public class Foo { private static readonly ILog log = LogManager.GetLogger(typeof(Foo)); …. } 一个缺点是,这意味着我们将这个10字的部分粘贴在一起,并且偶尔会有人忘记更改类名。 log4net常见问题解答还提到了这种替代可能性,这种可能性更加冗长: public class Foo { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); … } 是否可以编写装饰器来定义它? 我真的很想说: [LogMe] // or perhaps: [LogMe(“log”)] public class Foo { … } 我在其他语言中做过类似的事情,但从来没有像C#这样的静态编译语言。 我可以从装饰器定义类成员吗? 编辑 :嘿。 我是Lisp程序员。 我很欣赏切换语言的建议,但实际上,如果我要切换语言以获得更好的元编程function,我会一直到Lisp而不是半途而废。 不幸的是,使用不同的语言不是这个项目的选择。

你能删除一个装饰者吗?

是否可以从对象中删除装饰器? 说我有以下代码: abstract class Item { decimal cost(); } class Coffee : Item { decimal cost() { // some stuff } } abstract class CoffeeDecorator : Item { Item decoratedItem; } class Mocha : CoffeeDecorator { Item decoratedItem; public Mocha(Coffee coffee) { decoratedItem = coffee; } } public void Main(string[] args) { Item coffeeDrink = […]

装饰ASP.NET Web API IHttpController

我正在尝试使用装饰器来包装Web API控制器( IHttpController实现),但是当我这样做时,Web API会抛出exception,因为不知何故它会期待实际的实现。 将装饰器应用于控制器是我成功应用于MVC控制器的一个技巧,我显然希望在Web API中也这样做。 我创建了一个自定义的IHttpControllerActivator ,它允许解析修饰的IHttpController实现。 这是一个剥离的实现: public class CrossCuttingConcernHttpControllerActivator : IHttpControllerActivator { private readonly Container container; public CrossCuttingConcernHttpControllerActivator(Container container) { this.container = container; } public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) { var controller = (IHttpController)this.container.GetInstance(controllerType); // Wrap the instance in one or multiple decorators. Note that in reality, the // […]

为通用接口配置装饰器,并使用Simple Injector中的非通用接口参数将所有实例注入构造函数

我一直在使用与这篇优秀文章中描述的模式非常相似的模式,将命令和查询作为对象。 我也使用SimpleInjector作为DI容器。 唯一显着的区别是,控制器对某些ICommandHandler采取显式依赖关系。我希望控制器依赖于一个对象(一个Dispatcher ),该对象将接受一个ICommand实例并解析该命令的正确处理程序。 这将减少构造函数需要采用的参数数量,并使整个事物更容易使用。 所以我的Dispatcher对象构造函数如下所示: public CommandAndQueryDispatcher(IEnumerable commandHandlers, IEnumerable queryHandlers) { } 我的CommandHandler接口如下所示: public interface ICommandHandler : ICommandHandler where TCommand : ICommand { void Execute(TCommand command, ICommandAndQueryDispatcher dispatcher); } public interface ICommandHandler { void Execute(object command, ICommandAndQueryDispatcher dispatcher); } 典型的命令处理程序如下所示: public abstract class CommandHandlerBase : ICommandHandler where TCommand : ICommand { public abstract void […]

如何使用方法参数属性

我一直在努力寻找如何编写自定义属性来validation方法参数的示例,即转换此表单: public void DoSomething(Client client) { if (client.HasAction(“do_something”)) { // … } else { throw new RequiredActionException(client, “do_something”); } } 进入这个: public void DoSomething([RequiredAction(Action=”some_action”)] Client client) { // … } 据我所知,我需要将此属性添加到我的自定义属性,但我对如何访问装饰参数Client感到茫然: [AttributeUsageAttribute(AttributeTargets.Parameter)] public class RequireActionAttribute : System.Attribute { public Type Action {get; set;} public RequireActionAttribute() { // .. How do you access the decorated parameter? […]

装饰者和IDisposable

我有一个DbContext的子类 public class MyContext : DbContext { } 我在IUnitOfWork周围有一个IUnitOfWork抽象,它实现了IDisposable以确保在适当的时候处理诸如MyContext类的引用 public interface IUnitOfWork : IDisposable { } public class UnitOfWork : IUnitOfWork { private readonly MyContext _context; public UnitOfWork() { _context = new MyContext(); } ~UnitOfWork() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private bool _disposed; protected virtual void Dispose(bool disposing) { if […]

如何使用Castle Windsor注册通用装饰器?

我需要使用相应的DeadlockRetryCommandHandlerDecorator类型基于ICommandHandler类型来装饰所有类型 我试过这个解决方案,但遗憾的是它不起作用。 container.Register( Component.For(typeof(ICommandHandler)) .ImplementedBy(typeof(DeadlockRetryCommandHandlerDecorator))); container.Register( AllTypes.FromThisAssembly() .BasedOn(typeof(ICommandHandler)) .WithService.Base()); 如何注册通用装饰器( DeadlockRetryCommandHandlerDecorator )来包装所有通用ICommandHandler实现?