我想对一些方法应用一个自定义属性,表明它们应该作为事务处理。一般来说,我知道如何通过继承system.attribute来创建自定义属性,但是我不知道如何做我需要的事情。
我的目标是这样的:

[TransactionalMethod()]
public void SaveData(object someObject)
{
    // ... maybe some processing here
    // ... then the object gets saved to a database
    someObject.Save();
}

transactionalMethod()属性将使该方法的行为类似于它包装在transactionScope中…
 try
 {
     using(TransactionScope scope = new TransactionScope())
     {
         SaveData(someObject);
         scope.Complete();
     }
 }
 catch
 {
     // handle the exception here
 }

如果saveData()方法引发异常,则我们将不在using范围内,并且不会调用作用域.complete。
我不想找到调用saveData()的每个实例,并在其周围使用语句手动添加代码。我怎样才能做出导致这种行为的属性?

最佳答案

aspect.net是另一个服务于相同目的的项目

07-24 17:50
查看更多