Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在模块化应用程序中使用structuremap 3使用装饰器装饰接口_C#_Module_Decorator_Ioc Container_Structuremap - Fatal编程技术网

C# 在模块化应用程序中使用structuremap 3使用装饰器装饰接口

C# 在模块化应用程序中使用structuremap 3使用装饰器装饰接口,c#,module,decorator,ioc-container,structuremap,C#,Module,Decorator,Ioc Container,Structuremap,我正在使用structuremap3,在我的WinForm项目中有3个模块(每个模块都是一个类库项目): 核心模块包括: public class ICommandHandler<T> { } public interface IModule1UOW: IUnitOfWork { } public class Module1UOW:IModule1UOW { } public class Module1UOWDecorator<TC

我正在使用
structuremap3
,在我的
WinForm
项目中有3个模块(每个模块都是一个类库项目):

核心模块包括:

  public class ICommandHandler<T>
  {
  }
  public interface IModule1UOW: IUnitOfWork
  {
  }

  public class Module1UOW:IModule1UOW
  {
  }

  public class Module1UOWDecorator<TCommand> :UnitOfWorkCommitCommandHandlerDecorator<IModule1UOW,TCommand>
  {
      public Module1UOWDecorator(ICommandHandler<TCommand> decorated, IModule1UOW uow)
      {
        ...
      }
  } 

  public Command1
  {
  } 
  public interface IModule2UOW: IUnitOfWork
  {
  }

  public class Module2UOW:IModule2UOW
  {
  }

  public class Module2UOWDecorator<TCommand> :UnitOfWorkCommitCommandHandlerDecorator<IModule2UOW,TCommand>
      public Module2UOWDecorator(ICommandHandler<TCommand> decorated, IModule2UOW uow)
      {
        ...
      }
  } 

  public Command2
  {
  } 
公共类ICommandHandler
{
}
[更新]

  public abstract class UnitOfWorkCommitCommandHandlerDecorator<TUnitOfWork, TCommand> :   ICommandHandler<TCommand>
    where TUnitOfWork : IUnitOfWork
  {
  }
公共抽象类UnitOfWorkCommitCommandHandlerDecorator:ICommandHandler
工作地点:I工作地点
{
}

模块1包含:

  public class ICommandHandler<T>
  {
  }
  public interface IModule1UOW: IUnitOfWork
  {
  }

  public class Module1UOW:IModule1UOW
  {
  }

  public class Module1UOWDecorator<TCommand> :UnitOfWorkCommitCommandHandlerDecorator<IModule1UOW,TCommand>
  {
      public Module1UOWDecorator(ICommandHandler<TCommand> decorated, IModule1UOW uow)
      {
        ...
      }
  } 

  public Command1
  {
  } 
  public interface IModule2UOW: IUnitOfWork
  {
  }

  public class Module2UOW:IModule2UOW
  {
  }

  public class Module2UOWDecorator<TCommand> :UnitOfWorkCommitCommandHandlerDecorator<IModule2UOW,TCommand>
      public Module2UOWDecorator(ICommandHandler<TCommand> decorated, IModule2UOW uow)
      {
        ...
      }
  } 

  public Command2
  {
  } 
公共接口IModule1ow:IUnitOfWork
{
}
公共类模块1UOW:IModule1OW
{
}
公共类Module1UOWDecorator:UnitOfWorkCommitteCommand和HandlerDecorator
{
公共模块1UOWDecorator(ICommandHandler装饰,IModule1OW uow)
{
...
}
} 
公共命令1
{
} 

模块2包含:

  public class ICommandHandler<T>
  {
  }
  public interface IModule1UOW: IUnitOfWork
  {
  }

  public class Module1UOW:IModule1UOW
  {
  }

  public class Module1UOWDecorator<TCommand> :UnitOfWorkCommitCommandHandlerDecorator<IModule1UOW,TCommand>
  {
      public Module1UOWDecorator(ICommandHandler<TCommand> decorated, IModule1UOW uow)
      {
        ...
      }
  } 

  public Command1
  {
  } 
  public interface IModule2UOW: IUnitOfWork
  {
  }

  public class Module2UOW:IModule2UOW
  {
  }

  public class Module2UOWDecorator<TCommand> :UnitOfWorkCommitCommandHandlerDecorator<IModule2UOW,TCommand>
      public Module2UOWDecorator(ICommandHandler<TCommand> decorated, IModule2UOW uow)
      {
        ...
      }
  } 

  public Command2
  {
  } 
公共接口IModule2UOW:IUnitOfWork
{
}
公共类Module2UOW:IModule2UOW
{
}
公共类Module2UOWDecorator:UnitOfWorkCommitteCommand和HandlerDecorator
公共模块2UOWDecorator(ICommandHandler装饰,IModule2UOW uow)
{
...
}
} 
公共命令2
{
} 
并定义了以下类来配置每个模块注册:

public class Module1Config
{
    public static void Config()
    {
        ObjectFactory.Configure(x =>
        {
            x.Scan(s =>
            {
                s.AssemblyContainingType(typeof(Command1));
                s.WithDefaultConventions();
                x.For(typeof(IModule1UOW))
                    .Use(typeof(Module1UOW))
                    .SetLifecycleTo((Lifecycles.Singleton));
                x.For(typeof(ICommandHandler<>))
                    .DecorateAllWith(typeof(Module1UOWDecorator<>));
            });
        });
    }
}
public class Module2Config
{
    public static void Config()
    {
        ObjectFactory.Configure(x =>
        {
            x.Scan(s =>
            {
                s.AssemblyContainingType(typeof(Command2));
                s.WithDefaultConventions();
                x.For(typeof(IModule2UOW))
                    .Use(typeof(Module2UOW))
                    .SetLifecycleTo((Lifecycles.Singleton));
                x.For(typeof(ICommandHandler<>))
                    .DecorateAllWith(typeof(Module2UOWDecorator<>));
            });
        });
    }
}
public类Module1Config
{
公共静态void Config()
{
ObjectFactory.Configure(x=>
{
x、 扫描(s=>
{
s、 AssemblyContainingType(typeof(Command1));
s、 使用默认约定();
x、 用于(类型(IModule1OW))
.用途(类型(模块1UOW))
.SetLifecycleTo((Lifecycles.Singleton));
x、 用于(类型(ICommandHandler))
.带(模块1 UOWDECOCTOR)的装饰件;
});
});
}
}
公共类Module2Config
{
公共静态void Config()
{
ObjectFactory.Configure(x=>
{
x、 扫描(s=>
{
s、 AssemblyContainingType(typeof(Command2));
s、 使用默认约定();
x、 用于(类型(IModule2UOW))
.用途(类型(模块2UOW))
.SetLifecycleTo((Lifecycles.Singleton));
x、 用于(类型(ICommandHandler))
.带(模块2UOWDecorator)的装饰件;
});
});
}
}
在我的应用程序的起点(我在program.cs上使用WinForms),我调用
Module1Confog.Config()
,然后调用
Module2Config.Config()

当我从
ObjectFactory
获取
ICommandHandler
实例时,我期望得到一个
ICommandHandler
对象,该对象使用
Module2UOWDecorator
进行装饰,但我得到的
ICommandHandler
对象使用
Module2UOWDecorator
进行装饰
模块1uwdecorator

换句话说,我希望:

Module2UOWDecorator[ICommandHandler]

但我明白了

Module1UOWDecorator[Module2UOWDecorator[ICommandHandler]]

有人知道,我如何解决这个问题吗?

容器将处理在各个模块中定义的配置,并将它们全部合并到单个工作配置中。容器不会将不同的模块视为单独的配置

但是您仍然可以做您想做的事情,因为
decoreallwith
方法有一个可选的
filter
参数,用于决定哪些内容将被装饰,哪些不被装饰

根据上述示例,您可以尝试以下方法:

x.For(typeof(ICommandHandler<>))
    .DecorateAllWith(
        typeof(Module2UOWDecorator<>),
        instance => instance.ReturnedType.Assembly == typeof(Class2).Assembly));
x.For(typeof(ICommandHandler))
.装饰(
类型(模块2UOWDecorator),
instance=>instance.ReturnedType.Assembly==typeof(Class2.Assembly));
容器将处理各个模块中定义的配置,并将它们全部合并到单个工作配置中。容器不会将不同的模块视为单独的配置

但是您仍然可以做您想做的事情,因为
decoreallwith
方法有一个可选的
filter
参数,用于决定哪些内容将被装饰,哪些不被装饰

根据上述示例,您可以尝试以下方法:

x.For(typeof(ICommandHandler<>))
    .DecorateAllWith(
        typeof(Module2UOWDecorator<>),
        instance => instance.ReturnedType.Assembly == typeof(Class2).Assembly));
x.For(typeof(ICommandHandler))
.装饰(
类型(模块2UOWDecorator),
instance=>instance.ReturnedType.Assembly==typeof(Class2.Assembly));
容器将处理各个模块中定义的配置,并将它们全部合并到单个工作配置中。容器不会将不同的模块视为单独的配置

但是您仍然可以做您想做的事情,因为
decoreallwith
方法有一个可选的
filter
参数,用于决定哪些内容将被装饰,哪些不被装饰

根据上述示例,您可以尝试以下方法:

x.For(typeof(ICommandHandler<>))
    .DecorateAllWith(
        typeof(Module2UOWDecorator<>),
        instance => instance.ReturnedType.Assembly == typeof(Class2).Assembly));
x.For(typeof(ICommandHandler))
.装饰(
类型(模块2UOWDecorator),
instance=>instance.ReturnedType.Assembly==typeof(Class2.Assembly));
容器将处理各个模块中定义的配置,并将它们全部合并到单个工作配置中。容器不会将不同的模块视为单独的配置

但是您仍然可以做您想做的事情,因为
decoreallwith
方法有一个可选的
filter
参数,用于决定哪些内容将被装饰,哪些不被装饰

根据上述示例,您可以尝试以下方法:

x.For(typeof(ICommandHandler<>))
    .DecorateAllWith(
        typeof(Module2UOWDecorator<>),
        instance => instance.ReturnedType.Assembly == typeof(Class2).Assembly));
x.For(typeof(ICommandHandler))
.装饰(
类型(模块2UOWDecorator),
instance=>instance.ReturnedType.Assembly==typeof(Class2.Assembly));

我使用了您的代码,但这导致我的
ICommandHandler
没有使用任何装饰器进行装饰。如何查找运行时中的
实例
是什么?Hi@Masoud