<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2558941299384166926</id><updated>2011-07-07T21:39:24.616-07:00</updated><title type='text'>Xusheng Xiao's Research</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>14</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-4465703351595281233</id><published>2010-02-18T19:27:00.000-08:00</published><updated>2010-02-18T19:27:32.065-08:00</updated><title type='text'>Evaluation</title><content type='html'>It is good that my evaluation was not so bad.&lt;br /&gt;&lt;br /&gt;I got some new ideas that can be integrated into my paper. :)&lt;br /&gt;&lt;br /&gt;For the object creation issue, there is another kind of branch - implicit branch. It is created wherever we access an object field. For example, "obj.Name" is trying to access the Name property of obj and it requires that "obj" cannot be null, which is an implicit branch.&lt;br /&gt;&lt;br /&gt;If we find DSE fail to satisfy such field accessing result and&amp;nbsp;after all the test executions, the parts of the program that are dominated by the statement of&amp;nbsp;field accessing&amp;nbsp;&amp;nbsp;are not covered, we are sure that there is an object creation issue at that location.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-4465703351595281233?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/4465703351595281233/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2010/02/evaluation.html#comment-form' title='32 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/4465703351595281233'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/4465703351595281233'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2010/02/evaluation.html' title='Evaluation'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>32</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-8630484340889158831</id><published>2010-01-18T18:20:00.000-08:00</published><updated>2010-01-18T18:20:00.298-08:00</updated><title type='text'>Comment on my paper</title><content type='html'>After the discussion about my draft with Dr. Xie, I got lots of excellent feedback. Thus, I want write the most important ones down, so that I won't forget. :)&lt;br /&gt;&lt;br /&gt;1. Formally define DSE and find a formal definition of issue. (Refer his Fitness paper)&lt;br /&gt;&lt;br /&gt;2. Problem should be to minimize the issues. (Refer his Rostra paper)&lt;br /&gt;&lt;br /&gt;3. Related work should add paper of&amp;nbsp;Explaining Failures of Program Analyses&lt;br /&gt;&lt;br /&gt;4. The contribution points should have links to research questions and metrics.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-8630484340889158831?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/8630484340889158831/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2010/01/comment-on-my-paper.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/8630484340889158831'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/8630484340889158831'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2010/01/comment-on-my-paper.html' title='Comment on my paper'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-6028725517444738163</id><published>2009-11-30T17:43:00.000-08:00</published><updated>2009-11-30T17:43:49.479-08:00</updated><title type='text'>Meeting with Justin Hunter</title><content type='html'>Today our class had a guest lecturer, Justin Hunter, founder of Hexawise. I expected his lecture to be technical, but it turned out to be a lecture of experience sharing. :) He is not the geek type, but his MBA way of thinking is interesting. When some classmate asked questions about new features of Hexawise, he started to draw a cost and value chart and used it to explain the feature that is low cost and high value would be first considered. This is similar to Dr. Xie' engineering thinking, but more MBA and management style, hehe&lt;br /&gt;&lt;br /&gt;After lecture, Justin went to lunch with Rahul and me. Along the way, we discussed what new features of Hexawise could benefit clients more. Since in his PPT, he used google map as an example to show Hexawise, I suggest him integrate Hexawise and Selenium, a web application automatic UI testing framework. But here comes the problem: What should we output to clients? At first, I think of something big and complete: we can use Hexawise to generate the test value and transform into Selenium test scripts; then we can write Ant script to start the execution of these tests in different browsers. To make it more fancy, we can even execute them in parallel: we can set up a central server and execute the scripts there, and the central server will pick up different computers to execute the tests, each computer running a different version of browser, such as firefox in computer A and IE in computer B. If this can be done, then we have a complete solution for client and we can start to sell it!! However, this is not so practical. So I started to think of something more practical and I proposes another small solution: since by using Hexawise, testers need to model the input space for different parameters, we can let the testers or developers from clients to submit their test interface files and use this information to generate the test cases for them.&amp;nbsp;By test interfaces, here I mean JUnit test classes that contains parameterized unit tests&amp;nbsp;for Java, so that the input values generated by Hexawise can be used as the parameters for the PUTs. For C#, it is similar. For web application, we can ask user to upload the web UI page (a html file), so that we can extract the control information and fill the generated input values into different web controls for testing web UI. To know which parameter is mapped to which argument or web control, we let the clients specify the mapping rules, which describe&amp;nbsp;how the parameters are mapped to the arguments in the uploaded test interfaces. This would not increase much burden since in the test design phase, testers have already designed the&amp;nbsp;parameters&amp;nbsp;and values in order to use Hexawise. This sounds like working but not so novel, but in industry practical is more important. :) Rahul also proposed some thoughts to him, but I forgot what exactly they were due to the bad memory caused by not enough sleep....&lt;br /&gt;&lt;br /&gt;Though we&amp;nbsp;continuously proposed one and one thoughts about how to improve Hexawise using different techniques, like taint analysis and fault localization, in the end I am not sure whether he would even adopt one thought from us.&amp;nbsp;But one thing I am sure is that, our enthusiastic discussion already made Justin too exciting to eat...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-6028725517444738163?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/6028725517444738163/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/meeting-with-justin-hunter.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/6028725517444738163'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/6028725517444738163'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/meeting-with-justin-hunter.html' title='Meeting with Justin Hunter'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-2964387607986232607</id><published>2009-11-19T23:01:00.000-08:00</published><updated>2009-11-19T23:01:03.239-08:00</updated><title type='text'>Problem Definition of my project</title><content type='html'>After discussion with Dr. Xie, I realized that I lacked of a formal definition of my project, which was why my description was not so&amp;nbsp;convincing. Thus, the top priority thing of my project before next&amp;nbsp;Monday&amp;nbsp;is to come up with a comprehensive and formal definition of the problem I am trying to solve in my project.&lt;br /&gt;&lt;br /&gt;Suresh's ICSE 09 paper is a good example to inspire me. My initial idea is to borrow the concepts from shortest path definition from Algorithm Course.&lt;br /&gt;&lt;br /&gt;The rough idea is : First I should propose a definition of issues. Then the first condition is to satisfy a constraint of a branch. Then I should propose a definition for human cost and machine cost. When we have a issue reported, we say that machine cost is infinite and we need human's help. For the cost, we always try to&amp;nbsp;minimize Human cost first and then try to minimize&amp;nbsp;Machine cost.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-2964387607986232607?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/2964387607986232607/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/problem-definition-of-my-project.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/2964387607986232607'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/2964387607986232607'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/problem-definition-of-my-project.html' title='Problem Definition of my project'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-7036744895444957511</id><published>2009-11-17T20:40:00.000-08:00</published><updated>2009-11-17T20:40:45.172-08:00</updated><title type='text'>Finally complete the first step of my empirical study......</title><content type='html'>After my midterm of algorithm and many other stuffs (taking courses, homework...), finally I force myself to carry out the first step of my empirical study of two subjects: xUnit and MacroScope.&lt;br /&gt;&lt;br /&gt;xUnit is the open source project subject used in my course study, so using it is pretty straightforward for my study.&lt;br /&gt;&lt;br /&gt;MacroScope is a .NET SQL parser. I got this subject when I was asking Kunal in my lab about how to carry out empirical study. It turned out that this subject was really for the object creation issues as in some methods it reported 20+ object creation issues.&lt;br /&gt;&lt;br /&gt;To make my study easier, I use Pex to symbolically execute the public methods of the projects and collect the classes that have non-covered blocks after executions. These classes are good subjects for my study.&lt;br /&gt;&lt;br /&gt;The first discovery I find is that for an application, if it is written as a library, it normally would have many object creation issues but not so many uninstrumented method issues. But if it is a user application that develop its functionalities on top of other libraries, then the uninstrumented method issues would be more. I also find that usually in OO application, we need to solve the object creation issue first so that other parts of the program can be reached and uninstrumented method issues would come into play....&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-7036744895444957511?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/7036744895444957511/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/finally-complete-first-step-of-my.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/7036744895444957511'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/7036744895444957511'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/finally-complete-first-step-of-my.html' title='Finally complete the first step of my empirical study......'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-5415931646950041434</id><published>2009-11-15T18:36:00.000-08:00</published><updated>2009-11-15T18:36:23.131-08:00</updated><title type='text'>Busy weekend</title><content type='html'>Since Dr. Xie's feedback for my last draft is that he still did not understand what was the input and output of my approach, I decided to rewrite part of Introduction again to specify the intention of my approach more directly.&lt;br /&gt;&lt;br /&gt;For the survey of how many problems of DSE are caused by object creation issue and external method call issue, I only get a good result of xUnit.Assert method. Still need more try on different methods...&lt;br /&gt;&lt;br /&gt;For the object creation issue, it seems my previous description is not so clear since I did not think it through. Instead of relying on Pex's report of object creation issues, I tried to report it by myself and camp up with a motivating example that can show the advantage of my new way. Hopefully it sounds reasonable for other people in my group......&lt;br /&gt;&lt;br /&gt;For the external method call issue, after I add the symbolic value check, the result turns out to be good. I can get two important external method calls that really affect the coverage out of the 55 reported uninstrumented issues.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-5415931646950041434?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/5415931646950041434/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/busy-weekend.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/5415931646950041434'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/5415931646950041434'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/busy-weekend.html' title='Busy weekend'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-4801212459965013849</id><published>2009-11-12T20:34:00.000-08:00</published><updated>2009-11-12T20:34:21.068-08:00</updated><title type='text'>A busy but fruitful day......</title><content type='html'>This morning I added the example section into my draft and refined the uninstrumented issue example. I also had several interesting finding:&lt;br /&gt;1. I found that now Pex reported testability issue when encountering File operations in the program under test, but I still could get uninstrumented method issues reported for File operation functions.&lt;br /&gt;2. If I use "Console.WriteLine", Pex will not report this uninstrumented method, but if I use "Console.Out.WriteLine" Pex will report it. It seems that Pex currently has some mechanism to deal with the reporting of .NET system libraries.&lt;br /&gt;&lt;br /&gt;Based on Dr. Xie's comment, I need to justify my&amp;nbsp;justify my word of "two major problems". I had some rough idea about how to do it, but to make it better, I decided to consult the senior students in my group. :) After&amp;nbsp;asking Suresh, I knew I needed to carry out a survey on some large application (like NHibernate) and put the result in the introduction section. This finally points a direction for me to improve. :)&lt;br /&gt;&lt;br /&gt;Around 4:30pm, we had a group discussion about two papers about Input Partition in symbolic execution and Runtime Monitoring of SQL Injection Attack. Though the meeting was a little bit too long (more than 1 hour), we discussed about several ideas and one of them was very promising.&lt;br /&gt;&lt;br /&gt;This was really a busy but fruitful day, though I did not get many concrete things done. Hopefully tomorrow morning I can get the survey done and improve my introduction section.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-4801212459965013849?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/4801212459965013849/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/busy-but-fruitful-day.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/4801212459965013849'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/4801212459965013849'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/busy-but-fruitful-day.html' title='A busy but fruitful day......'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-4805875570408614969</id><published>2009-11-12T19:02:00.002-08:00</published><updated>2009-11-12T19:02:34.285-08:00</updated><title type='text'>Using T-SQL to assign sequencial number to legacy DB serecords</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Arial, Verdana, sans-serif; font-size: 13px;"&gt;To integrate with other system, sometimes we may need to add a column to the old database table and assign some simple sequencial number for every record in that table just like another "ID".&lt;br /&gt;&lt;br /&gt;For example, we have a table called "Person" and now we need to add a new column "outsideID" to every record in the table. Using T-SQL, we can do it like this:&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;T-SQL to generate sequential ID&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;br /&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;declare&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@id&amp;nbsp;&lt;span style="color: blue;"&gt;uniqueidentifier&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;declare&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@name&amp;nbsp;&lt;span style="color: blue;"&gt;nvarchar&lt;/span&gt;&lt;span style="color: grey;"&gt;(&lt;/span&gt;64&lt;span style="color: grey;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;declare&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@row&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;declare&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@message&amp;nbsp;&lt;span style="color: blue;"&gt;varchar&lt;/span&gt;&lt;span style="color: grey;"&gt;(&lt;/span&gt;80&lt;span style="color: grey;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;set&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@row&lt;span style="color: grey;"&gt;=&lt;/span&gt;1&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;DECLARE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;cur&amp;nbsp;&lt;span style="color: blue;"&gt;CURSOR&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;FOR&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;SELECT&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;id, name&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;FROM&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;Person&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;OPEN&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;cur&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;FETCH&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;NEXT&amp;nbsp;&lt;span style="color: blue;"&gt;FROM&lt;/span&gt;&amp;nbsp;cur&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;INTO&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@id&lt;span style="color: grey;"&gt;,&lt;/span&gt;&amp;nbsp;@name&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;WHILE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&lt;span style="color: magenta;"&gt;@@FETCH_STATUS&lt;/span&gt;&amp;nbsp;&lt;span style="color: grey;"&gt;=&lt;/span&gt;&amp;nbsp;0&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;SELECT&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@message&amp;nbsp;&lt;span style="color: grey;"&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style="color: red;"&gt;'id: '&lt;/span&gt;&amp;nbsp;&lt;span style="color: grey;"&gt;+&lt;/span&gt;&amp;nbsp;&lt;span style="color: magenta;"&gt;CONVERT&lt;/span&gt;&lt;span style="color: grey;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;varchar&lt;/span&gt;&lt;span style="color: grey;"&gt;(&lt;/span&gt;255&lt;span style="color: grey;"&gt;),&lt;/span&gt;&amp;nbsp;@id&lt;span style="color: grey;"&gt;)&lt;/span&gt;&amp;nbsp;&lt;span style="color: grey;"&gt;+&lt;/span&gt;&amp;nbsp;&lt;span style="color: red;"&gt;' name: '&lt;/span&gt;&amp;nbsp;&lt;span style="color: grey;"&gt;+&lt;/span&gt;&amp;nbsp;&lt;span style="color: magenta;"&gt;CONVERT&lt;/span&gt;&lt;span style="color: grey;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;varchar&lt;/span&gt;&lt;span style="color: grey;"&gt;(&lt;/span&gt;255&lt;span style="color: grey;"&gt;),&lt;/span&gt;&amp;nbsp;@name&lt;span style="color: grey;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;PRINT&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@message&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;UPDATE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;Person&amp;nbsp;&lt;span style="color: blue;"&gt;SET&lt;/span&gt;&amp;nbsp;outsideId&amp;nbsp;&lt;span style="color: grey;"&gt;=&lt;/span&gt;&amp;nbsp;@row&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;WHERE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;CURRENT&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;OF&lt;/span&gt;&amp;nbsp;cur&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;SELECT&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@message&amp;nbsp;&lt;span style="color: grey;"&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style="color: red;"&gt;'set outsideId to '&lt;/span&gt;&amp;nbsp;&lt;span style="color: grey;"&gt;+&lt;/span&gt;&amp;nbsp;&lt;span style="color: magenta;"&gt;CONVERT&lt;/span&gt;&lt;span style="color: grey;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;varchar&lt;/span&gt;&lt;span style="color: grey;"&gt;(&lt;/span&gt;255&lt;span style="color: grey;"&gt;),&lt;/span&gt;&amp;nbsp;@row&lt;span style="color: grey;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;PRINT&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@message&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;set&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@row&amp;nbsp;&lt;span style="color: grey;"&gt;=&lt;/span&gt;&amp;nbsp;@row&amp;nbsp;&lt;span style="color: grey;"&gt;+&lt;/span&gt;&amp;nbsp;1&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;FETCH&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;NEXT&amp;nbsp;&lt;span style="color: blue;"&gt;FROM&lt;/span&gt;&amp;nbsp;cur&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;INTO&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;@id&lt;span style="color: grey;"&gt;,&lt;/span&gt;&amp;nbsp;@name&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;END&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;CLOSE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;cur&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 36pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New'; font-size: 10pt;"&gt;DEALLOCATE&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;cur&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-4805875570408614969?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/4805875570408614969/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/using-t-sql-to-assign-sequencial-number.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/4805875570408614969'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/4805875570408614969'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/using-t-sql-to-assign-sequencial-number.html' title='Using T-SQL to assign sequencial number to legacy DB serecords'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-1635037719873984193</id><published>2009-11-12T19:02:00.000-08:00</published><updated>2009-11-12T19:02:03.021-08:00</updated><title type='text'>Handling the close event correctly using Outlook Object Model</title><content type='html'>&lt;span class="Apple-style-span" style="color: #333333; font-family: Tahoma; font-size: 14px; line-height: 21px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;In my current project, we need to add a customized toolbar to outlook and attach our event handlers to the close event of the window. Our implementation is like this: when the outlook new mail item window is opened, we attach our event handlers to the mail item and when it is closed, we need to detach the event handlers for preventing memory leak issues. The code we used is like this:&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Register Handler to Item Event&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;events = item&amp;nbsp;&lt;span style="color: blue;"&gt;as&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ItemEvents_10_Event&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(events !=&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;events.Close += OnItemClose;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;It is straightforward that we attach to the close event of the item and unregister the handler inside the "OnItemClose" function.&amp;nbsp;However, the handling of the close event is kind of tricky: When a user enters some text in the outlook window and click the close button on the top-right corner or press "Esc", the system will pop up a dialog and ask whether you want to save the changes or not. Before pop up the window, "Close" event is raised and our code gets executed, which unregisters the handlers. Here comes the problem: If user choose "Cancel" in the pop up dialog and return to the mail item, our system works abnormally as the event handlers are gone! To solve this this problem, I consulted many technical articles from the web and finally got the answer: In outlook mail item, there are two "close" event: one is raised when user click close button or press "Esc", the other is a truely close event raised when the outlook window is closed. The first close event is easy to find as it is located on the mailItem object which represents the outlook new mail item. But the second close event is difficult to find as it resides on the inspector object, which represents the window object of the mail item. Thus, the correct code to handle this situation would be:&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Register Handler to Inspector Event&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;inspector =&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.item.GetInspector&amp;nbsp;&lt;span style="color: blue;"&gt;as&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;InspectorEvents_10_Event&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(inspector !=&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;inspector.Close += OnItemClose;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;As this event will only be raised when the window of the mail item is "truely" closed, when user chooses "Yes" or "Not" to close the pop up but not the "Cancel", everything works fine and no memory issues need to be handled.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-1635037719873984193?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/1635037719873984193/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/handling-close-event-correctly-using.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/1635037719873984193'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/1635037719873984193'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/handling-close-event-correctly-using.html' title='Handling the close event correctly using Outlook Object Model'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-3780497970540173152</id><published>2009-11-12T19:01:00.003-08:00</published><updated>2009-11-12T19:01:33.301-08:00</updated><title type='text'>Some thought of MorphJ--an extension of Java</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Arial, Verdana, sans-serif; font-size: 13px;"&gt;Currently, I found an interesting idea of programming language, Class morphing. Class morphing is an abstraction mechanism that allows a class to abstract over the structure of other types. And the main reference morphing language is MorphJ--an extension of Java.&lt;br /&gt;&lt;br /&gt;I think MorphJ is so powerful and innovative. In my last project, we adopted NHibernate, an Object Relational Mapping Tool for .NET, for the database development.&lt;br /&gt;With its help, developers need not be aware of the existence of database but just manipulate database records like use normal C# object. To implement this, the "trick" played by NHibernate is that it will create a proxy class for every domain class. When the user read the property of the proxied class, the proxied class will check whether it is necessary to read the data from database. If yes, it will issue a corresponding SQL query and get the requested data. However, in C#, if we map the property to the field of table, we need to declare every property as virtual property, otherwise NHibernate could not override the property due to the language contraints of C#. To change all the property and method into virtual ones is tedious and error-prone. However, after reading your introduction of MorphJ, Ii think this problem could be easily solved by using MorphJ. Assuming that it could support C#, the pseudo code could be like this:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="sites-embed-align-center-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: auto; margin-right: auto; margin-top: 5px; text-align: center;"&gt;&lt;div class="sites-embed-border-on sites-embed" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; color: black; font-size: 12px; margin-bottom: 0px; margin-left: auto; margin-right: auto; margin-top: 5px; width: 600px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Pseudo Code&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;class C&lt;domain&gt; {&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;DOMAIN d = new DOMAIN ();&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-left: 40px;"&gt;bool hasValue;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&amp;lt; Return *&amp;gt;[Property] for (public Return&amp;nbsp; Property: DOMAIN.properties)&lt;br /&gt;Return&amp;nbsp; Property&lt;br /&gt;{&lt;br /&gt;get&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("Calling property"+ Property.name + " on "+ DOMAIN.toString());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(!hasValue)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //fetch the data from database&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ......&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hasValue = true;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return d. Property;&lt;br /&gt;}&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/domain&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;This new implementation is not only concise, but also easier to maintain and extend.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-3780497970540173152?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/3780497970540173152/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/some-thought-of-morphj-extension-of.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/3780497970540173152'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/3780497970540173152'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/some-thought-of-morphj-extension-of.html' title='Some thought of MorphJ--an extension of Java'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-7019758588664214448</id><published>2009-11-12T19:01:00.001-08:00</published><updated>2009-11-12T19:01:11.255-08:00</updated><title type='text'>Make Asynchronous Callback Testable</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Arial, Verdana, sans-serif; font-size: 13px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;Generally, to test an asynchronous callback is not so easy, as you need a way to wait for the execution of the "returned" callback. Some method I found adopts timer and records whether the callback has been executed by using a boolean value. However, this way will make the tests "randomly" fail, since the timer waiting is not stable and suitable for test condition. After consulting some papers from websites, I found an elegant way to test it! Let me explore this idea by starting with a code to be tested:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;The code to be tested&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public&lt;/span&gt;&lt;span style="font-family: 'Courier New';"&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;HandleResult(&lt;span style="color: #2b91af;"&gt;EvolveResponse&lt;/span&gt;&amp;nbsp;response,&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;&amp;nbsp;exception)&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(exception ==&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;serviceCoverageItemsResponse = (&lt;span style="color: #2b91af;"&gt;ServiceCoverageItemsResponse&lt;/span&gt;)response;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;dispatcherWrapper.Invoke(&lt;span style="color: #2b91af;"&gt;DispatcherPriority&lt;/span&gt;.Normal,&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;NoArgsDelegate&lt;/span&gt;(() =&amp;gt;panel.SetItemsSource(serviceCoverageItemsResponse.ServiceCoverageItems)));&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;else&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&amp;nbsp;(!(exception&amp;nbsp;&lt;span style="color: blue;"&gt;is&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;UnsupportedOfflineOperationException&lt;/span&gt;))&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;LOGGER.ErrorFormat(&lt;span style="color: #a31515;"&gt;"Error occured in handling the operation - {0}"&lt;/span&gt;, exception);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;To test this code, we need the help of mock testing framework as it can do the "trick" of replacement of method execution if the expected call meets the conditions. By using method replacement, we can inject the code to store the callback method when the asynchronous method is invoked, and then execute it by hand to simulate the "callback" situation. The callback delegate which should be replaced would be:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Callback Delegate to be replaced&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;dispatcherWrapper.Invoke(&lt;span style="color: #2b91af;"&gt;DispatcherPriority&lt;/span&gt;.Normal,&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="line-height: normal; margin-bottom: 0.0001pt;"&gt;&lt;span style="font-family: 'Courier New';"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;NoArgsDelegate&lt;/span&gt;(() =&amp;gt;panel.SetItemsSource(serviceCoverageItemsResponse.ServiceCoverageItems)));&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The trick played by Rhino Mock would be:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Trick of replacement of callback delegate&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="color: #2b91af; font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DispactherCallback&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;callback =&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;delegate&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;DispatcherPriority&lt;/span&gt;&amp;nbsp;priority,&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Delegate&lt;/span&gt;&amp;nbsp;createdDelegate)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{ noArgsDelegate = (&lt;span style="color: #2b91af;"&gt;NoArgsDelegate&lt;/span&gt;) createdDelegate; };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;dispatcher.Expect(x =&amp;gt; x.Invoke(&lt;span style="color: #2b91af;"&gt;DispatcherPriority&lt;/span&gt;.Normal,&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;)).&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;IgnoreArguments().Constraints(&lt;span style="color: #2b91af;"&gt;Is&lt;/span&gt;.Equal(&lt;span style="color: #2b91af;"&gt;DispatcherPriority&lt;/span&gt;.Normal),&lt;span style="color: #2b91af;"&gt;Is&lt;/span&gt;.TypeOf(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;NoArgsDelegate&lt;/span&gt;))).&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;Do(callback);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;By using this part of code, when the actually code is executed, the "&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;noArgsDelegate&lt;/span&gt;" variable will store the actual generated delegate, here is "&lt;span style="color: #2b91af; font-family: 'Courier New'; font-size: 10pt;"&gt;NoArgsDelegate&lt;/span&gt;". So if we invoke this&amp;nbsp;&lt;span style="font-family: 'Courier New';"&gt;noArgsDelegate&amp;nbsp;&lt;span style="font-family: Arial;"&gt;later by hand, we could successfully simulate the invocation of "asynchronous callback", which is supposed to be invoked in other thread. The full test code would be:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Complete test code&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[&lt;span style="color: #2b91af;"&gt;Test&lt;/span&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;should_use_dispatcher_to_invoke_the_update_of_service_coverage()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;panel =&amp;nbsp;&lt;span style="color: #2b91af;"&gt;MockRepository&lt;/span&gt;.GenerateMock&amp;lt;&lt;span style="color: #2b91af;"&gt;IServiceDetailPanelView&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;dispatcher =&amp;nbsp;&lt;span style="color: #2b91af;"&gt;MockRepository&lt;/span&gt;.GenerateMock&amp;lt;&lt;span style="color: #2b91af;"&gt;IDispatcherWrapper&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;eventController =&amp;nbsp;&lt;span style="color: #2b91af;"&gt;MockRepository&lt;/span&gt;.GenerateMock&amp;lt;&lt;span style="color: #2b91af;"&gt;IEventController&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;NoArgsDelegate&lt;/span&gt;&amp;nbsp;noArgsDelegate =&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;Guid&lt;/span&gt;&amp;nbsp;userId =&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Guid&lt;/span&gt;.NewGuid();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;handler =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;GetServiceCoverageItemsForUserResponseHandler&lt;/span&gt;(panel, dispatcher, eventController, userId);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;items =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;[]{&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ServiceCoverageItemDTO&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;Guid&lt;/span&gt;.NewGuid(),&lt;span style="color: #2b91af;"&gt;Guid&lt;/span&gt;.NewGuid(),10), };&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: #2b91af;"&gt;DispactherCallback&lt;/span&gt;&amp;nbsp;callback =&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;delegate&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;DispatcherPriority&lt;/span&gt;&amp;nbsp;priority,&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Delegate&lt;/span&gt;&amp;nbsp;createdDelegate)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{ noArgsDelegate = (&lt;span style="color: #2b91af;"&gt;NoArgsDelegate&lt;/span&gt;) createdDelegate; };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;dispatcher.Expect(x =&amp;gt; x.Invoke(&lt;span style="color: #2b91af;"&gt;DispatcherPriority&lt;/span&gt;.Normal,&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;)).&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;IgnoreArguments().Constraints(&lt;span style="color: #2b91af;"&gt;Is&lt;/span&gt;.Equal(&lt;span style="color: #2b91af;"&gt;DispatcherPriority&lt;/span&gt;.Normal),&lt;span style="color: #2b91af;"&gt;Is&lt;/span&gt;.TypeOf(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;NoArgsDelegate&lt;/span&gt;))).&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;Do(callback);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;panel.Expect(x =&amp;gt; x.SetItemsSource(items));&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;handler.HandleResult(&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ServiceCoverageItemsResponse&lt;/span&gt;(items),&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;noArgsDelegate.Invoke();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;dispatcher.VerifyAllExpectations();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;panel.VerifyAllExpectations();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If after the manual invocation of the saved delegate,&amp;nbsp;&lt;span style="font-family: 'Courier New';"&gt;noArgsDelegate&lt;span style="font-family: Arial;"&gt;, our verification of "panel.Expect(x =&amp;gt; x.SetItemsSource(items));" still could not be meet, then we know that the functional code in the asynchronous callback has bugs!&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-7019758588664214448?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/7019758588664214448/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/make-asynchronous-callback-testable.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/7019758588664214448'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/7019758588664214448'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/make-asynchronous-callback-testable.html' title='Make Asynchronous Callback Testable'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-8743524616648329284</id><published>2009-11-12T19:00:00.001-08:00</published><updated>2009-11-12T19:00:34.397-08:00</updated><title type='text'>Hibernate Tips: Efficient Usage of Bag for Association</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Arial, Verdana, sans-serif; font-size: 13px;"&gt;&lt;span style="font-size: 13px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;More and more people adopted Hibernate as their database development tool, as its implementation of ORM enables developers to manipulate tuples in relational database in object-oriented way. However, using Hibernate is not as easy as it seems like. Let’s have a look at a simple example(NHibernate), adding two new children to the father loaded from hibernate session :&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Sample Hibernate Code&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var&lt;/span&gt;&lt;span&gt;&amp;nbsp;fatherId =&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Guid&lt;/span&gt;.NewGuid();&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;father =&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Session&lt;/span&gt;.Load&amp;lt;&lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;&amp;gt;(fatherId);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;boy =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"terry"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;girl =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Person&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"mary"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;father.Children.Add(boy);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;father.Children.Add(girl);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Session&lt;/span&gt;.Save(father);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&amp;nbsp;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;And the mapping file for the class Person is as follows:&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&amp;nbsp;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Sample Mapping File&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="border-collapse: collapse;"&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;default-access&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;field&lt;/span&gt;"&amp;nbsp;&lt;span style="color: red;"&gt;assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Domain.Test&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;namespace&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Domain.Test&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;class&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;Person&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;table&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;person&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;id&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;id&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;column&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;personId&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Guid&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;nbsp;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;generator&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;guid&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;id&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;bag&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;children&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;inverse&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;false&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;key&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;column&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;id&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;one-to-many&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;Domain.Test.Person, Domain.Test&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;bag&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;class&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Execute it and it works fine! However, if you attach a sql profiler to your database, you will find that hibernate will first issue delete statement to delete all the children first and then add them back to the father one by one, which means that when you need to add two children, hibernate will issue one delete sql statement and two insert statement! In this case, if the size of father’s children keeps growing, the number of sql issued for a simple add will also increase, which is intolerable.&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&amp;nbsp;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;The reason why hibernate need to delete and add them all back is that the association mapping between father and children is&amp;nbsp;&lt;span style="color: blue;"&gt;bag&lt;/span&gt;&amp;nbsp;and the&amp;nbsp;&lt;span style="color: blue;"&gt;inverse&lt;/span&gt;&amp;nbsp;attribute of it is false!&lt;span&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;Using bag for association mapping, the domain objects in the collection could be duplicate and without order. However, it is these duplicate and unordered features make hibernate no way to know which domain objects are newly added and which domain objects have been updated or deleted. Thus, the only correct way for hibernate to deal with the change is to remove all the old children and save all the new ones in the collection, which hurts the performance a lot!&amp;nbsp;&lt;span&gt;&amp;nbsp;&lt;/span&gt;What’s worse, bag mapping also could not support fetch-join, which could be adopted to resolve N+1 problem.&lt;span&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;The recommended collection mapping for this usage is to use&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&amp;nbsp;instead of&amp;nbsp;&lt;span style="color: blue;"&gt;bag&lt;/span&gt;. Duplicate objects are not allowed in set, so hibernate could differentiate which domain objects are newly added and only issue the necessary add sql statement.&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&amp;nbsp;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;After the analysis, it seems that using bag is really a bad practice in hibernate, but why does it still exist? Actually, if you use bag correctly, it will improve also your performance. The change should be made like this:&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&amp;nbsp;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Change inverse to true&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="border-collapse: collapse;"&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;bag&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;children&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;span style="color: red; font-size: 14pt;"&gt;inverse&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style="color: blue; font-size: 14pt;"&gt;=&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style="font-size: 14pt;"&gt;"&lt;span style="color: blue;"&gt;true&lt;/span&gt;"&lt;/span&gt;&lt;/b&gt;&lt;span style="color: blue; font-size: 11pt;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;key&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;column&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;id&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;one-to-many&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;Domain.Test.Person, Domain.Test&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;bag&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&amp;nbsp;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Changing&amp;nbsp;&lt;span style="color: blue;"&gt;inverse&lt;/span&gt;&amp;nbsp;to true means that the modification of father’ children collection will not handled by hibernate. In other words, the association between father and children is maintained by children. Let’s look at the code of adding children to father:&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Adding Children to Father&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;father.Children.Add(boy);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;father.Children.Add(girl);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&amp;nbsp;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;The execution of&amp;nbsp;&lt;span&gt;&amp;nbsp;&lt;/span&gt;“&lt;span&gt;father.Children.Add(boy)&lt;/span&gt;&lt;span&gt;”&lt;/span&gt;&amp;nbsp;will no longer require hibernate to fetch the content of the Children collection from database before the add action, which of course would improve the performance.&amp;nbsp;&lt;span&gt;&amp;nbsp;&lt;/span&gt;This efficient way to use bag is actually to make the children colletion of father “read only”: After children data is loaded from database and assigned to father, further change will not be handled by hibernate. Then, if you want to add the children, you should use the assignment of children’s parent property:&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Assign Father to Children&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="border-collapse: collapse;"&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;boy.Parent = father;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;girl.Parent = father;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&amp;nbsp;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;The summary of using bag is listed as follows:&lt;br /&gt;&lt;/div&gt;&lt;ol style="list-style-type: decimal; margin-top: 0cm;"&gt;&lt;li style="padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;Always try set for association mapping first&lt;/li&gt;&lt;li style="padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;Using bag only when the collection is expected to be “read only” for hibernate&lt;/li&gt;&lt;li style="padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;Do remember to set “inverse = true”&lt;/li&gt;&lt;li style="padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;Let the objects in the other side to maintain the association&lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-8743524616648329284?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/8743524616648329284/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/hibernate-tips-efficient-usage-of-bag.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/8743524616648329284'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/8743524616648329284'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/hibernate-tips-efficient-usage-of-bag.html' title='Hibernate Tips: Efficient Usage of Bag for Association'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-4659886710821391459</id><published>2009-11-12T18:58:00.000-08:00</published><updated>2009-11-12T18:58:31.981-08:00</updated><title type='text'>Make UI Composition Testable (Migrate from older blog)</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Arial, Verdana, sans-serif; font-size: 13px;"&gt;In General, it is difficult to test the UI composition, like how many buttons and what will the button look like. It may not have so much value that we test what are the exactly height, width and position of the compositions, but it makes sense that we can verify how many controls added to the UI and what are the content of the control. With the help of Rhino Mock and “Factory Pattern”, I can do it like this using C#:&lt;br /&gt;&lt;br /&gt;Step 1 – Extract Interface and Introduce a UI Factory&lt;br /&gt;&lt;br /&gt;Suppose we are going to create a commandbar for outlook, we have the code like this:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Code To Be Tested&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="border-collapse: collapse;"&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;AddCommandBar(&lt;span style="color: #2b91af;"&gt;Outlook&lt;/span&gt;&amp;nbsp;outlook)&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;commandbar =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;OutlookCommandBar&lt;/span&gt;(&lt;/span&gt;&lt;span&gt;…&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//adding buttons to commandbar&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;commandbar.Add(&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Button&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"1"&lt;/span&gt;));&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;commandbar.Add(&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Button&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"100"&lt;/span&gt;));&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;outlook.Add(commandbar);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 10px; margin-left: 0px; margin-right: 0px; margin-top: 10px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;Introduce a factory and Interfaces:&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Introduce Interfaces&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="color: #2b91af;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ICommandBarFactoryImpl&lt;/span&gt;&lt;span&gt;&amp;nbsp;impl =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CommandBarFactoryImpl&lt;/span&gt;();&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;commandbarFactory =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CommandBarFactory&lt;/span&gt;(impl);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;commandbar = commandbarFactory.CreateCommandBar();&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;Step 2 – Mock ICommandBarFactoryImpl for CommandBarFactory:&lt;br /&gt;&lt;br /&gt;After step1’s change, we can mock ICommandBarFactoryImpl and test the commandbarFactory. I use Rhino Mock to do this:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Create Mock For FactoryImpl&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&lt;span&gt;&amp;nbsp;mockImpl =&amp;nbsp;&lt;span style="color: #2b91af;"&gt;MockRepository&lt;/span&gt;.GenerateMock&amp;lt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ICommandBarFactoryImpl&lt;/span&gt;&amp;nbsp;&amp;gt;();&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;Then I pass the mock impl to the factory:&lt;br /&gt;&lt;br /&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Pass mock Impl to Factory&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&lt;span&gt;&amp;nbsp;commandbarFactory =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CommandBarFactory&lt;/span&gt;(mockImpl);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;Now, I can execute the CreateCommandBar() method and verify what I want!&lt;br /&gt;&lt;br /&gt;Step 3 – Move all the add method to the Interface&lt;br /&gt;&lt;br /&gt;To test how many buttons are added, we need to verify how many times the Add method has been called. A simple way to test it is to extract all the add methods into parent Interface and transformed them into create methods. In my example, it should be:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;ICommandBarFactoryImpl&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public&lt;/span&gt;&lt;span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;interface&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;ICommandBarFactoryImpl&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CommandBar&lt;/span&gt;&amp;nbsp;CreateCommandBar();&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Button&lt;/span&gt;&amp;nbsp;CreateButton(&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;text);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;Step 4 – Writing Mock Test to Verify&lt;br /&gt;&lt;br /&gt;Everything has been prepared and I can start the testing now!&lt;br /&gt;&lt;br /&gt;The code would be:&lt;br /&gt;&lt;br /&gt;&lt;div class="sites-embed-align-left-wrapping-off" style="display: block; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 5px; text-align: left;"&gt;&lt;div class="sites-embed-border-on sites-embed sites-embed-full-width" style="border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(209, 204, 193); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(209, 204, 193); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(209, 204, 193); border-top-style: solid; border-top-width: 1px; clear: both; color: black; display: block !important; font-size: 12px; margin-bottom: 5px; margin-left: 0px; margin-right: auto; margin-top: 0px; width: 1201px;"&gt;&lt;h4 class="sites-embed-title" style="background-color: #f3f1ec; border-bottom-color: rgb(209, 204, 193); border-bottom-style: solid; border-bottom-width: 1px; color: #605e59; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 3px; padding-left: 5px; padding-right: 5px; padding-top: 3px; text-align: left;"&gt;Final Test Code&lt;/h4&gt;&lt;div class="sites-embed-content sites-embed-type-text" style="background-color: white; color: black; overflow-x: hidden; overflow-y: hidden; text-align: left;"&gt;&lt;div class="sites-embed-content-textbox" style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; text-align: left;"&gt;&lt;div dir="ltr"&gt;&lt;span style="color: green;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//prepare&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;mockImpl =&amp;nbsp;&lt;span style="color: #2b91af;"&gt;MockRepository&lt;/span&gt;.GenerateMock&amp;lt;&lt;span style="color: #2b91af;"&gt;ICommandBarFactoryImpl&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;commandBar =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CommandBar&lt;/span&gt;();&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;button1 =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Button&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"1"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var button100 =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Button&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"100"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//expect&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mockImpl.Expect(x =&amp;gt; x.CreateCommandBar()).Return(commandBar);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mockImpl.Expect(x =&amp;gt; x.CreateButton(&lt;span style="color: #a31515;"&gt;"1"&lt;/span&gt;)).Return(button1);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//repeat as many times as you need&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mockImpl.Expect(x =&amp;gt; x.CreateButton(&lt;span style="color: #a31515;"&gt;"100"&lt;/span&gt;)).Return(button100);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//execute&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;commandbarFactory =&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;CommandBarFactory&lt;/span&gt;(mockImpl);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;commandbar = commandbarFactory.CreateCommandBar();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: green;"&gt;//verify&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mockImpl.VerifyAllExpections();&amp;nbsp;&lt;span style="color: green;"&gt;//mock verify&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;.IsTrue(commandBar.Controls.Contains(button1));&lt;/span&gt;&lt;br /&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Assert&lt;/span&gt;.IsTrue(commandBar.Controls.Contains(button100));&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;In this case, we not only could verify how many times the create method has been called, which means how many buttons created, but also verify they all are added to the commandbar!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-4659886710821391459?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/4659886710821391459/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/make-ui-composition-testable-migrate.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/4659886710821391459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/4659886710821391459'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/make-ui-composition-testable-migrate.html' title='Make UI Composition Testable (Migrate from older blog)'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2558941299384166926.post-6581074618999260956</id><published>2009-11-12T08:50:00.000-08:00</published><updated>2009-11-12T08:53:46.279-08:00</updated><title type='text'>Decided to start blogging of my research progress</title><content type='html'>To keep track of my research progress and new thinkings, I decided to start my blog here!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2558941299384166926-6581074618999260956?l=xushengxiaosresearch.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://xushengxiaosresearch.blogspot.com/feeds/6581074618999260956/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/decided-to-start-blogging-of-my.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/6581074618999260956'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2558941299384166926/posts/default/6581074618999260956'/><link rel='alternate' type='text/html' href='http://xushengxiaosresearch.blogspot.com/2009/11/decided-to-start-blogging-of-my.html' title='Decided to start blogging of my research progress'/><author><name>Xusheng Xiao</name><uri>http://www.blogger.com/profile/12933814236981978546</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
