我使用Spring.Net 1.3.1與MVVM Foundation一起對我的viewmodels應用交叉切割。我注意到,如果我在將對象轉換爲交叉代理之前分配屬性更改的處理程序,則代理引擎不會將屬性更改的處理程序應用於代理。有誰知道這是否是預期的行爲,如果有,是否有解決方法?Spring AOP + MVVM Foundation + PropertyChanged
我廠是這樣
public static class AopProxyFactory {
public static object GetProxy(object target) {
var factory = new ProxyFactory(target);
factory.AddAdvisor(new Spring.Aop.Support.DefaultPointcutAdvisor(
new AttributeMatchMethodPointcut(typeof(AttributeStoringMethod)),
new UnitValidationBeforeAdvice())
);
factory.AddAdvice(new NotifyPropertyChangedAdvice());
factory.ProxyTargetType = true;
return factory.GetProxy();
}
}
的建議是這樣的
public class UnitValidationBeforeAdvice : IMethodBeforeAdvice {
public UnitValidationBeforeAdvice() {
}
public void Before(MethodInfo method, object[] args, object target) {
if (args.Length != 1) {
throw new ArgumentException("The args collection is not valid!");
}
var canConvertTo = true;
if (!canConvertTo) {
throw new ArgumentException("The '{0}' cannot be converted.");
}
}
}
public class NotifyPropertyChangedAdvice : IAfterReturningAdvice, INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
public void AfterReturning(object ReturnValue, MethodInfo Method, object[] Args, object Target) {
if (Method.Name.StartsWith("set_")) {
RaisePropertyChanged(Target, Method.Name.Substring("set_".Length));
}
}
private void RaisePropertyChanged(Object Target, String PropertyName) {
if (PropertyChanged != null)
PropertyChanged(Target, new PropertyChangedEventArgs(PropertyName));
}
}
的對象,我進行代理這個樣子的
public class ProxyTypeObject : ObservableObject {
private string whoCaresItsBroke;
public string WhoCaresItsBroke {
get { return whoCaresItsBroke; }
set {
whoCaresItsBroke = value;
RaisePropertyChanged("WhoCaresItsBroke");
}
}
}
而且調用代碼
var pto = new ProxyTypeObject();
pto.WhoCaresItsBroke = "BooHoo";
pto.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) => {
return;
};
var proxy = AopProxyFactory.GetProxy(pto);
(proxy as ProxyTypeObject).WhoCaresItsBroke = "BooHoo2";
您會注意到,當我設置「WhoCaresItsBroke」屬性時,我以前連接的屬性更改處理程序從未被擊中。 (我嘗試使用Spring.net論壇中提供的NotifyPropertyChangedAdvice,但看起來不起作用。)
您正在定義一個「AttributeMatchMethodPointcut(typeof(AttributeStoringMethod)」,但您並未使用該「AttributeStoringMethod」-Attribut來標記哪些方法是切入點。是否可以將實際使用該屬性的代碼部分發布到其他位置? – tobsen 2011-04-29 22:02:21