다음을 통해 공유


Reason behind AdapterContextChange()'s failure

I know very few people are working in CCF currently. But here is an undocumented life saver.

The Context that is passed in the NotifyContextChange event is a reference to the object and not a value. Most of you might already know this as it is a reference type.
If you have a requirement to update the context based on some user activity and notify the context change to all the applications, you simply invoke the ContextChange or AdapterContextChange methods with appropriate values.

However, there is a catch here. You cannot use the same context reference and trigger/fire the change across all the applications.
Let us take a look at this sample code for clarification.

The following code will *NOT* work:

 
private Context _context;<br>public void CustomerUpdated(string firstName, string lastName)<br>{<br>    _context["CustomerFirstName"] = firstName;<br>    _context["CustomerLastName"] = lastName;                   this.AdapterContextChange(_context);    base.DocumentComplete(url);<br>}<br>public override bool NotifyContextChange(Context context)<br>{<br>    _context = context;    return base.NotifyContextChange(context);<br>} 





This is because, when you invoke the context change method internally current context and the passed context are checked for their reference instead of the value. Since both of them share the same reference.

To rectify the problem, you should explicitly copy the context into a different object and update that.

This code will work:

 
private Context _context;<br>public void CustomerUpdated(string firstName, string lastName)<br>{<br>    _context["CustomerFirstName"] = firstName;<br>    _context["CustomerLastName"] = lastName;                   this.AdapterContextChange(_context);    base.DocumentComplete(url);<br>}<br>public override bool NotifyContextChange(Context context)<br>{<br>    _context = new Context(context.ContextInformation);    return base.NotifyContextChange(context);<br>} 




 I was not comfortable with this design decision at the beginning as we end up duplicating the context in every adapter we develop. 
But, after some thought, this seems to be chosen to gain performance - the tradeoff, however, was memory.

Comments

  • Anonymous
    December 20, 2007
    I know very few people are working in CCF currently. But here is an undocumented life saver. The Context