Hi Eileen,
Thanks for the reply.
No, this code will not work for all purposes. When FirstName or LastName changes, no push (interceptor) of the FullName is happening. We can change the FirstName in the Mask but the Interceptor for the FullName is never fired. It would need a pull from the View on the ViewModel to retrieve the FullName in this case.
This is how we implemented it in our ViewModel:
On retrieval we create a VMPerson which wraps a Person Entity. In the Init of the VMPerson all properies are tied up with an Intercepter like this:
========================================================================
m_NicknameAction = new PropertyInterceptorAction<DataEntityPropertySetInterceptorArgs<DomainModel.Person, String>>( typeof(DomainModel.Person), DomainModel.Person.EntityPropertyNames.Nickname, PropertyInterceptorMode.AfterSet, NicknameSetter); PropertyInterceptorManager.CurrentInstance.AddAction(m_NicknameAction); m_Nickname.Value = m_Person.Nickname; m_Nickname.ValueChanged += NicknameOnValueChanged; ========================================================================
In the Init we call a partial method called InitPerson which adds the Intercepter for the FullName
========================================================================
/// <summary> /// Called before initializing all properties for VMPerson /// </summary> partial void InitPerson() { m_FullName = new KLIBCustomProperty<KLIBStringProperty, string, DomainModel.Person>("FullName", this.m_Person); }
========================================================================
In the Constructor of the KLIBCustomProperty the following happens:
========================================================================
/// <summary> /// Initializing interceptor from the Model and the properties value /// </summary> /// <param name="propertyName"></param> /// <param name="entity"></param> public KLIBCustomProperty(string propertyName, Entity entity) { m_Entity = entity;
m_Action = new PropertyInterceptorAction<DataEntityPropertySetInterceptorArgs<TInstance, TVariableType>>( typeof(TInstance), propertyName, PropertyInterceptorMode.AfterSet, Setter); PropertyInterceptorManager.CurrentInstance.AddAction(m_Action); DataEntityProperty dataEntityProperty = entity.EntityAspect.GetDataProperty(propertyName); m_Property.Value = (TVariableType)dataEntityProperty.GetValue(m_Entity); }
/// <summary> /// This setter is called when the Property in the Model changes /// </summary> /// <param name="args"></param> private void Setter(DataEntityPropertySetInterceptorArgs<TInstance, TVariableType> args) { if (args.Instance.Equals(m_Entity)) { if (!Object.Equals(args.Value, m_Property.Value)) { m_Property.Value = (TVariableType)args.Value; } } } ========================================================================
As you can see the FullName is using an Interceptor to track changes and uses a Setter to communicate with WPF.
All Code in the ViewModel is generated using a T4 script. The InitPerson is custom code.
Regards,
Paul Sinnema
Diartis AG
|