在ScopedLifestyle.Context中如何在Decoratee和Decorator之间共享实例 [英] How to share instance between decoratee and decorator in the context of ScopedLifestyle.Flowing
问题描述
我不明白如何使用DI容器在decoratee和decorator之间共享实例.
I don't understand how to to share instance between decoratee and decorator by using a DI container.
以下示例说明了我的问题. context
实例在TransactionCommandDecorator
和Command
服务之间共享.
The following example illustrates my problem. The context
instance is shared between the TransactionCommandDecorator
and the Command
service.
var context = UowFactory.GetUnitOfWork();
var command = new TransactionCommandDecorator(
context,
new Command(context));
command.Execute(new CommandParams { });
context.dispose();
基本上,我需要有许多与数据库交互并调用存储库的命令.然后,我想通过使用包装命令服务的装饰器来应用事务.
Basically I need to have a lot of commands that interact with the database and make some call to a repository. I want then to apply a transaction by making use of a decorator that wraps the command service.
问题是我不知道如何在装饰者和被装饰者之间共享上下文(例如在示例中),因为我需要为命令的每次执行添加一个新的DbContext
实例.
The problem is that i don't know how to share the context between the decorator and the decoratee (like in the example) because i need to having a new DbContext
instance for every execution of the command.
您是否解释了如何在范围流动(ScopedLifestyle.Flowing
)的情况下使用Simple Injector使其工作.
Do you explain how i can make this to work by using Simple Injector in the context of Scope flowing (ScopedLifestyle.Flowing
).
这是装饰器和被装饰者实现的一个可能示例
This one possible example of implementation of decorator and decoratee
命令示例:
public Command(IUnitOfWork uow) => _uow = uow;
public DbResult Execute(CommandParams args)
{
_uow.Repo1.Add(args.Item);
_uow.Repo1.Add(args.Item2);
_uow.Repo1.Remove(args.Item3);
}
事务命令装饰器:
public class TransactionCommandDecorator : ICommand
{
public TransactionCommandDecorator(IUnitOfWork uow, ICommand command)
{
_uow = uow;
_command = command;
}
public DbResult Execute(commandParams args)
{
try
{
var transaction = _uow.GetContext().Database.GetTransaction();
var ret = _command.Execute(args);
if (!ret.Success)
{
transaction.Discard();
return;
}
transaction.Commit();
}
catch
{
transaction.Discard();
}
finally
{
transaction.Dispose();
}
}
}
推荐答案
通过将IUnitOfWork
注册为Lifestyle.Scoped
,可以在具有相同Scope
的类之间共享IUnitOfWork
,如以下示例所示:
The IUnitOfWork
can be shared between classes with in the same Scope
, by registering it as Lifestyle.Scoped
, as shown in the following example:
container.Register<IUnitOfWork>(() => UowFactory.GetUnitOfWork(), Lifestyle.Scoped);
container.Register<ICommand, Command>();
container.RegisterDecorator<ICommand, TransactionCommandDecorator>();
用法(使用ScopedLifestyle.Flowing):
Usage (using ScopedLifestyle.Flowing):
using (var scope = new Scope(container))
{
ICommand command = scope.GetInstance<ICommand>();
command.Execute(new CommandParams { });
}
用法(使用环境范围界定):
Usage (using ambient scoping):
using (AsyncScopedLifestyle.BeginScope(container))
{
ICommand command = container.GetInstance<ICommand>();
command.Execute(new CommandParams { });
}
这篇关于在ScopedLifestyle.Context中如何在Decoratee和Decorator之间共享实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!