Skip to content

Instantly share code, notes, and snippets.

@jpolvora
Created May 19, 2012 00:34
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 jpolvora/2728325 to your computer and use it in GitHub Desktop.
Save jpolvora/2728325 to your computer and use it in GitHub Desktop.
InterceptWithAttributeEntity2
public interface INotifyPropertyChangedExtended : INotifyPropertyChanged
{
void OnPropertyChanged(string propertyName);
}
public interface IOrder : INotifyPropertyChangedExtended
{
int Id { get; set; }
ICustomer Customer { get; set; }
}
[Aspect(typeof(OrderBehavior))]
public class Order : IOrder
{
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void OnPropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region Implementation of IOrder
public int Id { get; set; }
public ICustomer Customer { get; set; }
#endregion
}
public class OrderBehavior : PropertyChangedBehavior2<IOrder>
{
}
//This aspect definition assumes that your POCO class implements INotifyPropertyChangedExtended
//That contains and method OnPropertyChanged that will invoke the PropertyChanged event handler.
//No reflection!
public abstract class PropertyChangedBehavior2<T> : IAspectBehavior<T>
where T : class, INotifyPropertyChangedExtended
{
protected PropertyChangedBehavior2()
{
//don't use Getters!!!
MethodsToIntercept = Helpers.GetMethodNamesFromPropertyInfo(false, true, typeof(T).GetProperties());
}
#region Implementation of IAspectBehavior<T>
public string[] MethodsToIntercept { get; private set; }
public virtual void PreAspect(AspectContext<T> ctx)
{
var currMethod = ctx.CallCtx.MethodName.Substring(4);
var propertyInfo = ctx.Target.GetType().GetProperty(currMethod);
ctx.Parameters = propertyInfo.GetValue(ctx.Target, null);
}
public virtual void PostAspect(AspectContext<T> ctx)
{
if (Equals(ctx.Parameters, ctx.CallCtx.Args[0])) return;
var pName = ctx.CallCtx.MethodName.Substring(4);
ctx.Target.OnPropertyChanged(pName);
}
#endregion
}
static void InterceptWithAttributeEntity2()
{
IMyContext proxyContext = ObjectProxyFactory.CreateUsingAttribute<IMyContext>(new MyContext());
var order = proxyContext.Orders.FirstOrDefault();
if (order == null)
{
System.Console.WriteLine("No orders in Db");
System.Console.ReadKey();
order = new Order() { Customer = new Customer() { Name = "any customer" } };
proxyContext.Orders.Add(order);
proxyContext.SaveChanges(); // intercepted
return;
}
IOrder proxyOrder = ObjectProxyFactory.CreateUsingAttribute<IOrder>(order);
proxyOrder.PropertyChanged += (s, e) =>
{
System.Console.WriteLine("Property Changed: {0} on {1}",
e.PropertyName, s);
};
System.Console.WriteLine(proxyOrder.Id);
proxyOrder.Id = 0;
System.Console.WriteLine(proxyOrder.Id);
proxyOrder.Id = 1; //property changed
System.Console.WriteLine(proxyOrder.Id);
proxyOrder.Id = 2; // changed
System.Console.WriteLine(proxyOrder.Id);
proxyOrder.Id = 2; //not changed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment