Skip to content

Instantly share code, notes, and snippets.

@sakapon
Last active July 20, 2017 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakapon/54ef60b0d084e0f259a973fc119f5425 to your computer and use it in GitHub Desktop.
Save sakapon/54ef60b0d084e0f259a973fc119f5425 to your computer and use it in GitHub Desktop.
ProxySample/CrossCuttingConsole/NorthwindBusiness
using System;
using System.Transactions;
namespace CrossCuttingConsole
{
public class NorthwindBusiness : MarshalByRefObject
{
[TraceLog]
[TransactionScope]
public short SelectUnitsInStock()
{
Console.WriteLine("SelectUnitsInStock");
// cf. https://sakapon.wordpress.com/2011/10/02/dirtyread2/
using (var context = new NorthwindEntities())
{
var product = context.Products.Single(p => p.ProductID == 1);
return product.UnitsInStock;
}
}
[TraceLog]
[TransactionScope(IsolationLevel.Serializable)]
public void InsertCategory(string name)
{
Console.WriteLine("InsertCategory");
// cf. https://sakapon.wordpress.com/2011/12/14/phantomread2/
using (var context = new NorthwindEntities())
{
context.AddToCategories(Category.CreateCategory(0, name));
context.SaveChanges();
}
}
public int PropertyTest { get; [TraceLog]set; }
[TraceLog]
public void ErrorTest()
{
throw new InvalidOperationException("This is an error test.");
}
}
}
using System;
namespace CrossCuttingConsole
{
class Program
{
static void Main(string[] args)
{
var nw = CrossCuttingProxy.CreateProxy<NorthwindBusiness>();
var units = nw.SelectUnitsInStock();
nw.InsertCategory("Books");
nw.PropertyTest = 123;
try
{
nw.ErrorTest();
}
catch (Exception)
{
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment