【问题标题】:Simple Injector Transaction Handling简单的注入器事务处理
【发布时间】:2018-07-17 21:57:36
【问题描述】:

我正在将我的项目从 Spring.Net 迁移到 Simple Injector。在 Spring.Net 中,有一个使用事务装饰器将多个函数嵌套在单个事务下的概念。 Transaction 装饰器的功能是跨嵌套函数/服务函数回滚事务(包括数据库插入/更新),这些函数将单独调用数据库。

Simple Injector 中是否有任何等价物?

下面是Spring.NET的示例代码

用户服务

  [Transaction(TransactionPropagation.Required)]
    public bool RegisterUser(UserModel)
    {
    //Insert User DB Call
    int result = _Userrepository.UserInsert(UserModel);
    _subscriberservice.InsertSubscriber(UserModel);

}

订户服务

    public bool InsertSubscriber(UserModel);
    {
    //Insert Subscriber DB Call
    int result = _subscriberrepository.SubscriberInsert(UserModel);

}

如果插入订阅者服务插入失败,用户插入也必须回滚

【问题讨论】:

  • 看看this关于Simple Injector中的装饰器。
  • 请显示您当前使用 Spring.NET 实现此目的的代码。
  • 添加示例代码..谢谢

标签: c# simple-injector


【解决方案1】:

使用 Simple Injector 执行此操作的首选方法是在您的类周围应用装饰器,如 here 所述。

然而,Spring.NET 使用了一种不同的方法,称为动态拦截。它在运行时为您动态生成装饰器,这些装饰器将注入您创建的拦截器类。

Simple Injector 没有对动态拦截的内置支持,但可以添加它,如 here 所述。这个Stack Overflow answer 描述了如何将 Simple Injector 与 Castle Dynamic Proxy 集成,Castle Dynamic Proxy 也是 Spring.NET 在幕后使用的框架。

【讨论】:

    猜你喜欢
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    相关资源
    最近更新 更多