<?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-8985156687507385407</id><updated>2012-01-03T22:45:35.550-08:00</updated><category term='Objects in JavaScript'/><category term='AOP'/><category term='Continuous Integration'/><category term='XSLT'/><category term='XSL'/><category term='Ansyc method calls'/><category term='Declarative Programming'/><category term='Binding'/><category term='Javascript'/><category term='Profiling'/><category term='Unit Test'/><category term='Design Patterns By Metaphors'/><category term='Hirerachy Data Source'/><category term='MS Build'/><category term='Policy Injection'/><category term='VS 2010'/><category term='XML'/><category term='Delegate Command'/><category term='Aspect Oriented Programming'/><category term='TFS 2008'/><category term='WCF'/><category term='Design Patterns by Example'/><category term='Fluent API to tie multiple method calls'/><category term='Custom Behaviors'/><category term='Creation Patterns'/><category term='Visual Tree'/><category term='Property Synchronization'/><category term='Design Patterns'/><category term='Silverlight'/><title type='text'>Kiran Kumar</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-1718296054388855747</id><published>2011-02-08T03:50:00.000-08:00</published><updated>2011-02-08T04:36:02.990-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><category scheme='http://www.blogger.com/atom/ns#' term='Hirerachy Data Source'/><title type='text'>Hirerachy Data Source - Using Fluent API convert flat data structure to hirerachy structure</title><content type='html'>It is often required to represent data in hirerachy structure. Over period of time, for several projects I had this requirement. In process of building this structure I observed a following common pattern/trend:&lt;br /&gt;&lt;br /&gt;- Often times, to represent hirerachy structure, a custom class is created to hold original data.&lt;br /&gt;- We will have recursive functions to select nodes at various levels and build tree structure.&lt;br /&gt;&lt;br /&gt;Lets take a example of Employee. Employees are managed by Manager, who themselves are employees and also managed by some other employee.&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;span style="font-size:78%;color:#006600;"&gt;class Employee&lt;br /&gt;{&lt;br /&gt;public int ID { get; set; }&lt;br /&gt;public string Name { get; set; }&lt;br /&gt;public int ManagerID { get; set; }&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;And we have following class to represent Hirerachy structure&lt;br /&gt;Code:&lt;br /&gt;&lt;span style="font-size:78%;color:#006600;"&gt;&lt;br /&gt;public interface INode&lt;br /&gt;{&lt;br /&gt;List&lt;inode&gt; ChildNodes { get; set; }&lt;br /&gt;Object PayLoad { get; set; }&lt;br /&gt;bool IsSelected { get; set; }&lt;br /&gt;}&lt;br /&gt;public class Node:INode&lt;br /&gt;{&lt;br /&gt;public List&lt;node&gt; ChildNodes { get; set; }&lt;br /&gt;public Object PayLoad { get; set; }&lt;br /&gt;public Node()&lt;br /&gt;{&lt;br /&gt;ChildNodes = new List&lt;node&gt;();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Before going further lets think of nature of conditions which we will be applying to Employee class to arrange it in hierarchy structure:&lt;br /&gt;&lt;br /&gt;Employees who doesnt have any managerId are super employees like CEO who wont report to any other employee.&lt;br /&gt;Condition for root employees: &lt;span style="font-size:85%;color:#660000;"&gt;employee.ManagerID == default(int)&lt;br /&gt;&lt;/span&gt;Employee child nodes will be those employees whose manager id is current employee's Id.&lt;br /&gt;Condition which needs to be applied recursively is :&lt;span style="color:#660000;"&gt; &lt;span style="font-size:85%;"&gt;parent.ID == child.ManagerID &amp;amp;&amp;amp; child.ManagerID != default(int)&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#660000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Using HirerachyData Structure you can compose your nodes as below:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:78%;color:#006600;"&gt;List&lt;node&gt; nodes = HirerachyDataSource&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:78%;color:#006600;"&gt;&lt;employee&gt;.ComposeHirerachyUsingStructure(persons) //Pass list of employees classes&lt;br /&gt;.RootNodesFilter((p) =&gt; p.ManagerID == default(int))  //How root nodes to be selected.&lt;br /&gt;.ChildNodeFilter((parent, child) =&gt; parent.ID == child.ManagerID &amp;amp;&amp;amp; child.ManagerID != default(int))  //What condition to check recursively to build hirerachy&lt;br /&gt;.UsingObjectBuilder((d) =&gt; new Node() { PayLoad = d })  //And finally how to build node.&lt;br /&gt;.BuildHeirarchy();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Using this approach , you can compose how to select root, how to select nodes recursively, and even you can specify how to build final nodes (could be any type), due to which you can get the power to build any type of hirerachy structures with any properties (NOTE: code listing below is coupled to INode, but you can safely avoid that as Object builder will take care of instancing out of hirerachydatasource).&lt;br /&gt;&lt;br /&gt;HirerachyDataSource code listing is below:&lt;br /&gt;Code:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:78%;color:#006600;"&gt;public class HirerachyDataSource&lt;t&gt;&lt;br /&gt;{&lt;br /&gt;public static CompositionStructure ComposeHirerachyUsingStructure(List&lt;t&gt; NodesList)&lt;br /&gt;{&lt;br /&gt;CompositionStructure structure = new CompositionStructure(NodesList);&lt;br /&gt;return structure;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class CompositionStructure&lt;br /&gt;{&lt;br /&gt;private List&lt;t&gt; FlatStructure = new List&lt;t&gt;();&lt;br /&gt;Predicate&lt;t&gt; rootNodeSelector;&lt;br /&gt;Func&lt;t,&gt; ObjectBuilder;&lt;br /&gt;Func&lt;t,&gt; childNodeSelector;&lt;br /&gt;public CompositionStructure UsingObjectBuilder(Func&lt;t,&gt; nodeBuilder)&lt;br /&gt;{&lt;br /&gt;ObjectBuilder = nodeBuilder;&lt;br /&gt;return this;&lt;br /&gt;}&lt;br /&gt;public ObservableCollection&lt;inode&gt; BuildHeirarchy()&lt;br /&gt;{&lt;br /&gt;return BuildNode();&lt;br /&gt;}&lt;br /&gt;internal CompositionStructure()&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;internal CompositionStructure(List&lt;t&gt; nodes)&lt;br /&gt;{&lt;br /&gt;this.FlatStructure = nodes;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public CompositionStructure RootNodesFilter(Predicate&lt;t&gt; rootNodesFilter)&lt;br /&gt;{&lt;br /&gt;rootNodeSelector = rootNodesFilter;&lt;br /&gt;return this;&lt;br /&gt;}&lt;br /&gt;public CompositionStructure ChildNodeFilter(Func&lt;t,&gt; childNodeFilter)&lt;br /&gt;{&lt;br /&gt;childNodeSelector = childNodeFilter;&lt;br /&gt;return this;&lt;br /&gt;}&lt;br /&gt;protected ObservableCollection&lt;inode&gt; BuildNode()&lt;br /&gt;{&lt;br /&gt;//INode node = new Node();&lt;br /&gt;List&lt;inode&gt; nodes = (from a in FlatStructure&lt;br /&gt;where rootNodeSelector(a)&lt;br /&gt;select ObjectBuilder(a)).ToList();&lt;br /&gt;if (nodes != null)&lt;br /&gt;{&lt;br /&gt;foreach (var item in nodes)&lt;br /&gt;{&lt;br /&gt;BuildNode(item);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;return new ObservableCollection&lt;inode&gt;(nodes);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected void BuildNode(INode node)&lt;br /&gt;{&lt;br /&gt;if (node != null)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;List&lt;inode&gt; items= (from a in FlatStructure&lt;br /&gt;where childNodeSelector((T)node.PayLoad, a)&lt;br /&gt;select ObjectBuilder(a)).ToList();&lt;br /&gt;node.ChildNodes = new ObservableCollection&lt;inode&gt;(items);&lt;br /&gt;if (node.ChildNodes != null)&lt;br /&gt;{&lt;br /&gt;foreach (var item in node.ChildNodes)&lt;br /&gt;{&lt;br /&gt;BuildNode(item);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8985156687507385407-1718296054388855747?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/1718296054388855747/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=1718296054388855747' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/1718296054388855747'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/1718296054388855747'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2011/02/hirerachy-data-source-using-fluent-api.html' title='Hirerachy Data Source - Using Fluent API convert flat data structure to hirerachy structure'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-4834468504049468360</id><published>2010-09-13T22:41:00.000-07:00</published><updated>2010-09-14T00:05:42.416-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Fluent API to tie multiple method calls'/><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><category scheme='http://www.blogger.com/atom/ns#' term='Ansyc method calls'/><title type='text'>Silverlight - Using Fluent API to tie related async calls.</title><content type='html'>&lt;b&gt;&lt;span class="Apple-style-span"&gt;Introduction&lt;/span&gt;&lt;/b&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In Silverlight all service calls are by default async calls. With ansync calls, you will be making a webservice call using one method and expects a call back in another method once you get a response from service. If you are using repository pattern you will be using same type of mechanism for communication between your view model and repositories.&lt;/div&gt;&lt;div&gt;In Async pattern there will be two related functions to accomplish a single task. One will initiate the task and another function will complete. Both these methods should work in tandem to accomplish single task. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;And it will be highly error prone if you do not do proper event subscription &amp;amp; unsubscription. You also might want to take care of few common things like error handling, showing/hiding progress bar which will be common across all these calls. In this blog I will show you a interesting technique accomplishing same using elegant Fluent API solution. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Assume you have following repository class where you will define events once repositories webservice call complete it raises completed event, which will notify view model's completed event.&lt;/div&gt;&lt;div&gt;&lt;pre style="font-family: consolas"&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;CustomerRepository&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;br /&gt; {&lt;br /&gt;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;event&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;EventHandler&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;CustomEventArgs&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&gt; GetCustomerNameCompleted;&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;event&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;EventHandler&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;CustomEventArgs&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&gt; GetCustomerNameCompleted2;&lt;br /&gt; &lt;br /&gt;     &lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;   &lt;br /&gt;     &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; GetCustomerName(&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; studyID)&lt;br /&gt;     {&lt;br /&gt;         &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;WebServiceStub&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; serviceStub = &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;WebServiceStub&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;();&lt;br /&gt;         serviceStub.GetCustomerNameCompleted += (obj, args) =&gt;&lt;br /&gt;         {&lt;br /&gt;&lt;br /&gt;             &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;if&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; (&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;.GetCustomerNameCompleted != &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;)&lt;br /&gt;             {&lt;br /&gt;                 &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;.GetCustomerNameCompleted(&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;, args.GetCustomEventArgs());&lt;br /&gt;             }&lt;br /&gt;         };&lt;br /&gt;         serviceStub.GetCustomerNameByID(studyID);&lt;br /&gt;     }&lt;br /&gt;     &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; GetCustomerName(&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; studyID, &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; Param1)&lt;br /&gt;     {&lt;br /&gt;         &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;WebServiceStub&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; serviceStub = &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;WebServiceStub&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;();&lt;br /&gt;         serviceStub.GetCustomerNameCompleted2 += (obj, args) =&gt;&lt;br /&gt;         {&lt;br /&gt;             &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;if&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; (&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;.GetCustomerNameCompleted2 != &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;)&lt;br /&gt;             {&lt;br /&gt;                 &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;.GetCustomerNameCompleted2(&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;, args.GetCustomEventArgs());&lt;br /&gt;             }&lt;br /&gt;         };&lt;br /&gt;         serviceStub.GetCustomerName(studyID, Param1);&lt;br /&gt;     }&lt;br /&gt;     &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;And in you View model&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;public void GetCustomers(int customerID)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;pre style="font-family: consolas"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;repository.GetCustomerNameCompleted += repository_GetCustomerNameCompleted;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-family: consolas"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;repository.GetCustomers(customerID)&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-family: consolas"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-family: consolas"&gt;&lt;pre style="font-family: consolas"&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; repository_GetCustomerNameCompleted(&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;object&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; sender, &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;CustomEventArgs&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; e)&lt;br /&gt;{&lt;br /&gt;         repository.GetCustomerNameCompleted -= repository_GetCustomerNameCompleted;&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;You will be doing subscriptions and unsubscriptions throughout your silverlight ViewModels to repositories to fetch data. For each task you will have two methods in your view model.  &lt;/div&gt;&lt;div&gt;You might be tempted to write completed method as anonymous, but it wont work as you have to unsubscribe once you are done. Otherwise subscriptions will keep on growing and you will get multiple callback for each call.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;And if you forget to unsubscribe not only you will run into danger of memory leaks but buggy code. Instead of that if you can you use fluent API to make a call something as follows:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre style="font-family: consolas"&gt;&lt;span style="color: #2b91af"&gt;public void GetCustomer(string customerID)&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-family: consolas"&gt;&lt;span style="color: #2b91af"&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-family: consolas"&gt;&lt;span style="color: #2b91af"&gt;StitchMethodFlow&lt;/span&gt;&lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&gt;&lt;br /&gt;                     .Init&lt;br /&gt;                     .WhenCalled(repository.GetCustomerName)&lt;br /&gt;                     .WithParams(customerID)&lt;br /&gt;                     .OnCompletionExecute((o,e)=&gt;{//compelted;})&lt;br /&gt;                     .CallBackSubscriptionProvider(repository.RegisterEventSubscription)&lt;br /&gt;                     .StartExecution();&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Above code looks simple, more maintainable, self explaining and code related to single task being done at single place not leaking to multiple methods. Now event subscription and Unsubscription are taken care by StitchMethodFlow.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;You can download sample from &lt;a href="http://cid-b345d108a9d3464d.office.live.com/self.aspx/.Public/MethodStitcher.zip"&gt;here &lt;/a&gt;to see how it works. Demo is console application.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8985156687507385407-4834468504049468360?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/4834468504049468360/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=4834468504049468360' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/4834468504049468360'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/4834468504049468360'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2010/09/silverlight-using-fluent-api-to-tie.html' title='Silverlight - Using Fluent API to tie related async calls.'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-6277882755631290046</id><published>2010-08-30T03:20:00.000-07:00</published><updated>2010-08-30T04:00:08.756-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='XSL'/><category scheme='http://www.blogger.com/atom/ns#' term='Declarative Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='XML'/><category scheme='http://www.blogger.com/atom/ns#' term='XSLT'/><title type='text'>Recursive XSL Templates</title><content type='html'>I am re-posting my article once published in code project in 2006. This brief article will demonstrate the recursive nature of templates. For original posting &lt;a href="http://www.codeproject.com/KB/XML/RecursiveXSLTemplates.aspx"&gt;click here&lt;/a&gt;.&lt;br /&gt;For source code download &lt;a href="http://www.codeproject.com/KB/XML/RecursiveXSLTemplates/XSLRecursiveTemplates_codefiles.zip"&gt;click here&lt;/a&gt;.&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#ff9900;"&gt;Output Needed:&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_l4GuU8wUOUo/THuHw5JDWuI/AAAAAAAADSI/RxTCrrQyk84/s1600/output.gif"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 119px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5511147843060194018" border="0" alt="" src="http://4.bp.blogspot.com/_l4GuU8wUOUo/THuHw5JDWuI/AAAAAAAADSI/RxTCrrQyk84/s400/output.gif" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#ff9900;"&gt;Introduction&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;XSL is a declarative programming language. Variables that are once declared cannot be reassigned. It is often difficult for programmers coming from procedural languages background to do advanced tasks. Using XSL, we can solve complex problems, which at first glance often seem to be difficult or impossible. In this brief article, I will demonstrate the recursive nature of templates. In a typical case of a product catalog display, our requirement is to display the product details in each cell, and number of columns should be selectable by the user. The sample XML file is listed below:&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#993300;"&gt;&lt;span style="font-size:78%;"&gt;&amp;lt;data&amp;gt;&lt;br /&gt;&amp;lt;product name="Heine HKL with trolleystand" weight="34.4kg" price="230.45"&amp;gt;&lt;br /&gt;&amp;lt;product name="Universal clamp and Projector" weight="10.64kg" price="670.45"&amp;gt;&lt;br /&gt;&amp;lt;product name="Examination Lamp, Universal Mount" weight="1.08kg" price="25.45"&amp;gt;&lt;br /&gt;&amp;lt;product name="Provita Examination Lamp, Mobile Base" weight="1.4kg" price="215.45"&amp;gt;&lt;br /&gt;&amp;lt;product name="35Watt Flexible Arm Light to fit Rail system" weight="11.67kg" price="130.45"&amp;gt;&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;.&lt;br /&gt;&amp;lt;/data&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Assuming we are getting the above from a business component, each element row corresponds to a product, whose attributes comprises of product specific data. Each product along with its details will be rendered in a single cell. And the number of columns should be definable at runtime. Following is the brief XSL which does the rendering:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:78%;color:#993300;"&gt;&amp;lt;xml:namespace prefix = xsl /&gt;&lt;/span&gt;&lt;?xml:namespace prefix = xsl /&gt;&lt;xsl:stylesheet border="0" cellspacing="5" xsl="http://www.w3.org/1999/XSL/Transform" test="" select="3" match="/" version="1.0" name="numCols" cellpadding="0"&gt;&lt;span style="font-size:78%;color:#993300;"&gt;0"&amp;gt;&lt;br /&gt;&amp;lt;tr&amp;gt;&lt;br /&gt;&amp;lt;xsl:apply-templates select=""&gt;=&lt;br /&gt;$startindex and position() &lt; ($startindex+$numofCols)]" mode="rows"&amp;gt; &amp;lt;/xsl:apply-templates&amp;gt; &amp;lt;/tr&amp;gt; &amp;lt;xsl:call-template name="renderColumns"&amp;gt; &amp;lt;xsl:with-param name="listrows" select=""&gt;= $startindex+$numofCols]"&amp;gt;&amp;lt;/xsl:with-param&amp;gt;&lt;br /&gt;&amp;lt;xsl:with-param name="startindex" select="$startindex"&amp;gt;&amp;lt;/xsl:with-param&amp;gt;&lt;br /&gt;&amp;lt;xsl:with-param name="numofCols" select="$numofCols"&amp;gt;&amp;lt;/xsl:with-param&amp;gt;&lt;br /&gt;&amp;lt;/xsl:call-template&amp;gt;&lt;br /&gt;&amp;lt;/xsl:if&amp;gt;&lt;br /&gt;&amp;lt;/xsl:template&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;xsl:template match="node()" mode="rows"&amp;gt;&lt;br /&gt;&amp;lt;td nowrap="true"&amp;gt;&lt;br /&gt;&amp;lt;table style="BORDER-BOTTOM: thin solid; BORDER-LEFT: thin solid; WIDTH: 100%; BORDER-TOP: thin solid; BORDER-RIGHT: thin solid"&amp;gt;&lt;br /&gt;&amp;lt;xsl:apply-templates select="@*"&amp;gt;&amp;lt;/xsl:apply-templates&amp;gt;&lt;br /&gt;&amp;lt;tbody&amp;gt;&amp;lt;/tbody&amp;gt;&amp;lt;/table&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;lt;/xsl:template&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;xsl:template match="@*"&amp;gt;&lt;br /&gt;&amp;lt;tr&amp;gt;&lt;br /&gt;&amp;lt;td style="TEXT-TRANSFORM: uppercase; BACKGROUND-COLOR: gainsboro; FONT-SIZE: larger"&amp;gt;&lt;br /&gt;&amp;lt;xsl:value-of select="name()"&amp;gt;&amp;lt;/xsl:value-of&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;lt;td&amp;gt;&lt;br /&gt;&amp;lt;xsl:value-of select="."&amp;gt;&amp;lt;/xsl:value-of&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;/xsl:template&amp;gt;&lt;br /&gt;&amp;lt;/xsl:stylesheet&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#ff6600;"&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#993300;"&gt;&lt;span style="font-size:78%;"&gt;&amp;lt;xsl:template match="/"&amp;gt;&lt;br /&gt;&amp;lt;xsl:param name="numCols" select="3"&amp;gt;&amp;lt;/xsl:param&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;As would a C or C++ programmer organize and reuse the code using functions or object methods, in XSL, we can organize code using templates. The above code is the root template which will be invoked by the XSLT processor. We are declaring a parameter using xsl:param, whose name is numCols. The user can pass this parameter; if the user is not supplying any value in this parameter, then, by default, it will have a value of 3. This variable will specify the number of columns to be rendered.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:78%;"&gt;&lt;span style="color:#993300;"&gt;&amp;lt;xsl:call-template name="renderColumns"&amp;gt;&lt;br /&gt;&amp;lt;xsl:with-param name="listrows" select="//product"&amp;gt;&amp;lt;/xsl:with-param&amp;gt;&lt;br /&gt;&amp;lt;xsl:with-param name="startindex" select="1"&amp;gt;&amp;lt;/xsl:with-param&amp;gt;&lt;br /&gt;&amp;lt;xsl:with-param name="numofCols" select="$numCols"&gt;&amp;lt;/xsl:with-param&amp;gt;&lt;br /&gt;&amp;lt;/xsl:call-template&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;We are calling the renderColumns template, to which we are passing three parameters. In listrows, we are selecting all product elements, startindex signifies the starting index, and numofCols will control the number of rows to be rendered.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:78%;color:#993300;"&gt;&amp;lt;xsl:template name="renderColumns"&amp;gt;&lt;br /&gt;&amp;lt;xsl:param name="listrows"&amp;gt;&amp;lt;/xsl:param&amp;gt;&lt;br /&gt;&amp;lt;xsl:param name="startindex"&amp;gt;&amp;lt;/xsl:param&amp;gt;&lt;br /&gt;&amp;lt;xsl:param name="numofCols"&amp;gt;&amp;lt;/xsl:param&amp;gt;&lt;br /&gt;&amp;lt;xsl:if test=""&gt;0"&amp;gt;&lt;br /&gt;&amp;lt;tr&amp;gt;&lt;br /&gt;&amp;lt;xsl:apply-templates select=""&gt;=&lt;br /&gt;$startindex and position() &amp;lt; ($startindex+$numofCols)]" mode="rows"&amp;gt;&lt;br /&gt;&amp;lt;/xsl:apply-templates&amp;gt;&lt;br /&gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;xsl:call-template name="renderColumns"&amp;gt;&lt;br /&gt;&amp;lt;xsl:with-param name="listrows" select=""&gt;= $startindex+$numofCols]"&gt;&amp;lt;/xsl:with-param&amp;gt;&lt;br /&gt;&amp;lt;xsl:with-param name="startindex" select="$startindex"&amp;gt;&amp;lt;/xsl:with-param&amp;gt;&lt;br /&gt;&amp;lt;xsl:with-param name="numofCols" select="$numofCols"&amp;gt;&amp;lt;/xsl:with-param&amp;gt;&lt;br /&gt;&amp;lt;/xsl:call-template&amp;gt;&lt;br /&gt;&amp;lt;/xsl:if&amp;gt;&lt;br /&gt;&amp;lt;/xsl:template&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;In the XSL template renderColumns, we are selecting the elements whose position will be greater than or equal to the starting index and whose position is less than or equal to the sum of startindex and numofcols. After rendering these subset of elements, we are recursively calling renderColumns by selecting a subset of elements whose position is greater than the sum of startindex and numofCols, which are already rendered. For exiting this recursive loop, we have a test condition at the start which checks for the count of elements in the listrows variable. As we are selecting only those elements which are yet to be rendered while calling recursively, the set of nodes by each call will be reduced by the number of elements rendered. For rendering rows, in this call template, we are using the following template:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:78%;color:#993300;"&gt;&amp;lt;xsl:template match="node()" mode="rows"&amp;gt;&lt;br /&gt;&amp;lt;td nowrap="true"&amp;gt;&lt;br /&gt;&amp;lt;table style="BORDER-BOTTOM: thin solid; BORDER-LEFT: thin solid; WIDTH: 100%; BORDER-TOP: thin solid; BORDER-RIGHT: thin solid"&amp;gt;&lt;br /&gt;&amp;lt;xsl:apply-templates select="@*"&gt;&amp;lt;/xsl:apply-templates&amp;gt;&lt;br /&gt;&amp;lt;tbody&amp;gt;&amp;lt;/tbody&amp;gt;&amp;lt;/table&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;lt;/xsl:template&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;in which we are converting the attribute nodes into elements and calling another template:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:78%;color:#993300;"&gt;&amp;lt;xsl:template match="@*"&amp;gt;&lt;br /&gt;&amp;lt;tr&amp;gt;&lt;br /&gt;&amp;lt;td style="TEXT-TRANSFORM: uppercase; BACKGROUND-COLOR: gainsboro; FONT-SIZE: larger"&amp;gt;&lt;br /&gt;&amp;lt;xsl:value-of select="name()"&amp;gt;&amp;lt;/xsl:value-of&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;lt;td&amp;gt;&lt;br /&gt;&amp;lt;xsl:value-of select="."&amp;gt;&amp;lt;/xsl:value-of&amp;gt;&lt;br /&gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;lt;/xsl:template&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;which does the job of rendering the product details in a cell.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#ff6600;"&gt;Conclusion&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Even though variables are constants through out the life time of a variable in XSL, we can achieve things which, at first glance, look impossible due to the declarative nature of XSL. Upon close look and thinking in a declarative manner, we can solve the problem.&lt;br /&gt;&lt;br /&gt;&lt;/xsl:stylesheet&gt;&lt;xsl:stylesheet border="0" cellspacing="5" xsl="http://www.w3.org/1999/XSL/Transform" test="" select="3" match="/" version="1.0" name="numCols" cellpadding="0"&gt;&lt;/xsl:stylesheet&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8985156687507385407-6277882755631290046?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/6277882755631290046/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=6277882755631290046' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/6277882755631290046'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/6277882755631290046'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2010/08/recursive-xsl-templates.html' title='Recursive XSL Templates'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_l4GuU8wUOUo/THuHw5JDWuI/AAAAAAAADSI/RxTCrrQyk84/s72-c/output.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-8575536521722094178</id><published>2010-08-25T21:34:00.000-07:00</published><updated>2010-08-25T22:33:35.463-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Creation Patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns By Metaphors'/><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='Design Patterns by Example'/><title type='text'>Design Patterns by Metaphors - Part I (Creational Patterns)</title><content type='html'>From today, I am starting blog series on Design Patterns. There are numerous technical posts, material on this subject out there awaiting just googling to get discovered. So I don't want to repeat same here. What I will be trying to do is explain them using metaphors, in layman terms, so that newbies can understand easily and can adapt them with ease.&lt;br /&gt;&lt;br /&gt;I will start with Creational Patterns.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;&lt;span class="Apple-style-span"  style="font-size:x-large;"&gt;Creational Patters:&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Are patterns which guides you in tackling object creation challenges in certain scenarios.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;Abstract Factory:&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_l4GuU8wUOUo/THXw02Tl7cI/AAAAAAAADRg/t--dysChkNg/s1600/Abstract_Factory.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 800px; height: 439px;" src="http://4.bp.blogspot.com/_l4GuU8wUOUo/THXw02Tl7cI/AAAAAAAADRg/t--dysChkNg/s1600/Abstract_Factory.jpg" border="0" alt="" id="BLOGGER_PHOTO_ID_5509574509879225794" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Simpson lives in world of SONY. Every electronic appliance in his world is of SONY. But he wants agility to move to other electronic worlds as well, may be Samsung in future. In order for him to able to move to different worlds without much effort, he uses remote covers over top of particular remote to access functionality. All remotes in electronics will support that cover. Tomorrow even if he changes to Samsung, he will still be able to use them with his cover.&lt;br /&gt;Due to this common cover fitting all remotes to access only common minimum features, he will not be able to access any special button which might be specific to a particular remote. He trades this for flexibility to be able to move to different worlds.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;Builder Pattern&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_l4GuU8wUOUo/THXxM_TbfMI/AAAAAAAADRo/wGevQeGABic/s1600/Builder_Pattern.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 800px; height: 444px;" src="http://2.bp.blogspot.com/_l4GuU8wUOUo/THXxM_TbfMI/AAAAAAAADRo/wGevQeGABic/s1600/Builder_Pattern.jpg" border="0" alt="" id="BLOGGER_PHOTO_ID_5509574924611321026" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Result is so varied that we do not have common interface for end result. In case of Abstract Factory we had common interface for end result.&lt;br /&gt;User Will provide which builder to use to Director.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;Factory Method&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_l4GuU8wUOUo/THXxk4mBwHI/AAAAAAAADRw/YEJMfLHt0c8/s1600/Factory_Method.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 800px; height: 466px;" src="http://4.bp.blogspot.com/_l4GuU8wUOUo/THXxk4mBwHI/AAAAAAAADRw/YEJMfLHt0c8/s1600/Factory_Method.jpg" border="0" alt="" id="BLOGGER_PHOTO_ID_5509575335127138418" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Whenever Simpson requests for car to Car Factory, he will always get latest car available at that time, wrapped under Car prototype cover. As time evolves, as new models gets added, he still gets latest models but wrapped in car prototype. He will get latest engine performance, latest ABS, latest electronics working under hood.&lt;br /&gt;But all cars are wrapped under car prototype, which has only minimum control points. Though he can experience latest engine performance with car prototypes accelerator, he cannot access to car GenX’s latest Blue Me feature as that is wrapped under car prototype.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;&lt;b&gt;Abstract vs Factory&lt;/b&gt;&lt;/span&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_l4GuU8wUOUo/THXyMKvTXEI/AAAAAAAADR4/Gflu-ZHrpJw/s1600/Abstacts_vs_Factory.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 800px; height: 486px;" src="http://3.bp.blogspot.com/_l4GuU8wUOUo/THXyMKvTXEI/AAAAAAAADR4/Gflu-ZHrpJw/s1600/Abstacts_vs_Factory.jpg" border="0" alt="" id="BLOGGER_PHOTO_ID_5509576010012777538" /&gt;&lt;/a&gt;&lt;br /&gt;Thanks to David Hayden for making it so clear in his post at &lt;a href="http://davidhayden.com/blog/dave/archive/2006/04/04/2900.aspx"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;div&gt;&lt;b&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;Singleton&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_l4GuU8wUOUo/THXygKDaXcI/AAAAAAAADSA/6IpZp6R0_1I/s1600/Singleton.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 800px; height: 496px;" src="http://2.bp.blogspot.com/_l4GuU8wUOUo/THXygKDaXcI/AAAAAAAADSA/6IpZp6R0_1I/s1600/Singleton.jpg" border="0" alt="" id="BLOGGER_PHOTO_ID_5509576353426070978" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8985156687507385407-8575536521722094178?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/8575536521722094178/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=8575536521722094178' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/8575536521722094178'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/8575536521722094178'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2010/08/design-patterns-by-metaphors-part-i.html' title='Design Patterns by Metaphors - Part I (Creational Patterns)'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_l4GuU8wUOUo/THXw02Tl7cI/AAAAAAAADRg/t--dysChkNg/s72-c/Abstract_Factory.jpg' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-6274172218255900054</id><published>2010-08-16T22:44:00.001-07:00</published><updated>2010-08-18T04:36:10.812-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Property Synchronization'/><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><title type='text'>Silverlight - Property Value Synchronization between Two View Models</title><content type='html'>Recently I had been confronted with a design issue to keep two property values in two different View Models in Sync.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_l4GuU8wUOUo/TGooyGwQYHI/AAAAAAAADRQ/4HvcDUzN4Fw/s1600/sync_image.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 139px;" src="http://1.bp.blogspot.com/_l4GuU8wUOUo/TGooyGwQYHI/AAAAAAAADRQ/4HvcDUzN4Fw/s400/sync_image.jpg" border="0" alt="" id="BLOGGER_PHOTO_ID_5506258335685435506" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;It is a simple issue, where you can subscribe to PropertyChange notifications and detect whenever interested property changes in Object1, set AnotherProperty's value in Another Object. And vice versa. To avoid recursive infinite loop situation, you can keep a flag which tracks direction of change and avoids this infinite loop.&lt;br /&gt;&lt;br /&gt;But I wanted to solve this by keeping following constratints&lt;br /&gt;&lt;br /&gt;1. I need to reuse this syncing logic wherever required in future.&lt;br /&gt;2. Loosely Coupled.&lt;br /&gt;3. No reflection, I want compile time support to detect any errors or most of them.&lt;br /&gt;4. Should support FluentAPI for better usability.&lt;br /&gt;&lt;br /&gt;Few assumption I made are&lt;br /&gt;4. Both objects implements INotifyPropertyChanged event which raises event change notifications whenever property is changed.&lt;br /&gt;&lt;br /&gt;Before discussing code, once developed you can use it like following to keep both objects in Sync.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="color:#660000;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;//ViewModel which has a property&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;br /&gt; Class1 orgViewModel = new Class1();&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;//Another ViewModel which has another property.&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;br /&gt;           Class2 anotherViewModel = new Class2();&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;//Sync API will be exposed by this SyncClass Which takes ViewModel types and property type.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;            SyncClass&lt;/span&gt;&lt;class1, int=""&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt; SyncClass = new SyncClass&lt;/span&gt;&lt;class1, int=""&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;();&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;//Configure ViewModel with predicate to detect when property is changed and provide functions to read and write property values.&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;br /&gt;           SyncClass.MonitorClass(orgViewModel, (propname) =&gt; { return propname == "MyVal"; })&lt;br /&gt;               .SetGetValue(()=&gt;orgViewModel.MyVal)&lt;br /&gt;               .SetSetValue((i)=&gt;orgViewModel.MyVal=i);&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;//Configure another ViewModel with predicate to detect when property is changed and provide functions to read and write property values.           &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;            SyncClass.MonitorAnotherClass(anotherViewModel, (propname) =&gt; { return propname == "AnotherVal"; })&lt;br /&gt;               .SetGetValue(()=&gt;anotherViewModel.AnotherVal)&lt;br /&gt;               .SetSetValue((i1)=&gt;anotherViewModel.AnotherVal=i1);&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="color:#993300;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;//Start Synchronization.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;            SyncClass.StartSync();&lt;br /&gt;&lt;/span&gt;&lt;/class1,&gt;&lt;/class1,&gt;&lt;/span&gt;&lt;br /&gt;Once you configure you view models and property in above manner SynClass will auto sync two properties.&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Code Listing&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;pre style="font-family: consolas"&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;delegate&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;ValueChanged&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;object&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; sender,&lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;EventArgs&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; e);&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;UsingClass&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t,t1&gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;bool&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; IsOriginator = &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;event&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;ValueChanged&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; ObjectValueChanged;&lt;br /&gt;&lt;br /&gt;        T _MonitorObject;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Func&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t1&gt; _GetValue;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Action&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t1&gt; _SetValue;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Func&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;,&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;bool&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&gt; _IsPropChanged;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; UsingClass(T MonitorObject,&lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Func&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;,&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;bool&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&gt; IsPropertyChanged)&lt;br /&gt;        {&lt;br /&gt;            _IsPropChanged = IsPropertyChanged;&lt;br /&gt;            _MonitorObject = MonitorObject;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;INotifyPropertyChanged&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; propChanged = _MonitorObject &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;as&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;INotifyPropertyChanged&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;if&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; (propChanged != &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;)&lt;br /&gt;            {&lt;br /&gt;                propChanged.PropertyChanged += (o1, e1) =&gt;&lt;br /&gt;                    {&lt;br /&gt;                        &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;if&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; (_IsPropChanged(e1.Proeprtyname))&lt;br /&gt;                        {&lt;br /&gt;                            &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;.IsOriginator = &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;;&lt;br /&gt;                            &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;if&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; (&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;.ObjectValueChanged != &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;)&lt;br /&gt;                            {&lt;br /&gt;                                &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;.ObjectValueChanged(&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;, &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;EventArgs&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;());&lt;br /&gt;                            }&lt;br /&gt;                        }&lt;br /&gt;                    };&lt;br /&gt;            }&lt;br /&gt;           &lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;UsingClass&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t,t1&gt; SetGetValue(&lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Func&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t1&gt; GetValue)&lt;br /&gt;        {&lt;br /&gt;            _GetValue=GetValue;&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;return&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;;&lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; T1 GetValue()&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;return&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; _GetValue();&lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; SetSetValue(&lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Action&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t1&gt; SetValue)&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;._SetValue = SetValue;&lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; SetValue(T1 val1)&lt;br /&gt;        {&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;if&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; (!IsOriginator)&lt;br /&gt;            {&lt;br /&gt;                _SetValue(val1);&lt;br /&gt;            }&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;br /&gt;            {&lt;br /&gt;                IsOriginator = &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;class&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;SyncClass&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t,t1,tvaluetype&gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;UsingClass&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t,&gt; Object1;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;UsingClass&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t1,&gt; Object2;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;UsingClass&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t,tvaluetype&gt; MonitorClass(T MonitorObject,&lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Func&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;,&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;bool&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&gt; CheckPropertyChanged)&lt;br /&gt;        {&lt;br /&gt;            Object1 = &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;UsingClass&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t,tvaluetype&gt;(MonitorObject,CheckPropertyChanged);&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;return&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; Object1;&lt;br /&gt;&lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;UsingClass&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t1,tvaluetype&gt; MonitorAnotherClass(T1 MonitorAnotherObject, &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;Func&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;, &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;bool&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&gt; CheckPropertyChanged)&lt;br /&gt;        {&lt;br /&gt;            Object2 = &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;UsingClass&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;t1,&gt;(MonitorAnotherObject, CheckPropertyChanged);&lt;br /&gt;            &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;return&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; Object2;&lt;br /&gt;        }&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;public&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; StartSync()&lt;br /&gt;        {&lt;br /&gt;            Object1.ObjectValueChanged += (o, e) =&gt;&lt;br /&gt;            {&lt;br /&gt;                Object2.SetValue(Object1.GetValue());&lt;br /&gt;            };&lt;br /&gt;            Object2.ObjectValueChanged += (o1, e1) =&gt;&lt;br /&gt;            {&lt;br /&gt;                Object1.SetValue(Object2.GetValue());&lt;br /&gt;            };&lt;br /&gt;        }&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;br /&gt;    }&lt;/span&gt;&lt;/pre&gt;&lt;pre style="font-family: consolas"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;I prepared a sample in Console Application which will be easy to execute and see how its working. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://cid-b345d108a9d3464d.office.live.com/self.aspx/.Public/SyncEngine.zip"&gt;Click here&lt;/a&gt; to download sample application. You need VS 2010 to open this solution.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Hope you enjoyed this code crunch..&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/8985156687507385407-6274172218255900054?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/6274172218255900054/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=6274172218255900054' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/6274172218255900054'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/6274172218255900054'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2010/08/silverlight-property-value.html' title='Silverlight - Property Value Synchronization between Two View Models'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_l4GuU8wUOUo/TGooyGwQYHI/AAAAAAAADRQ/4HvcDUzN4Fw/s72-c/sync_image.jpg' height='72' width='72'/><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-3607803433302750714</id><published>2010-07-29T23:14:00.000-07:00</published><updated>2010-07-29T23:32:21.514-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Visual Tree'/><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><category scheme='http://www.blogger.com/atom/ns#' term='Binding'/><title type='text'>Silverlight - Binding System uses Visual Tree</title><content type='html'>Recently I was working on a control where I need to re-arrange layout of passed in user controls, which alters visual tree structure. During this I found binding system relies on visual tree. &lt;br /&gt;&lt;br /&gt;With example:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Page1                                          &lt;br /&gt;|                                              &lt;br /&gt;|--UserControl1                                &lt;br /&gt;|--UserControl2              &lt;br /&gt;|--UserControl3              &lt;br /&gt;                                                                                     &lt;br /&gt;&lt;em&gt;Transforms into&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;Custom Control&lt;br /&gt;|&lt;br /&gt;|-UserControl1&lt;br /&gt;|-UserControl2&lt;br /&gt;|-Page1&lt;br /&gt;|  |-UserControl3&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Above page when passed to this custom control need to strip few controls and arrange in different layout. If Binding done at Page1 level, once you remove user controls to different place than this visual tree, bindings will not be resolved.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8985156687507385407-3607803433302750714?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/3607803433302750714/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=3607803433302750714' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/3607803433302750714'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/3607803433302750714'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2010/07/silverlight-binding-system-uses-visual.html' title='Silverlight - Binding System uses Visual Tree'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-3351921860435236080</id><published>2010-07-16T05:29:00.000-07:00</published><updated>2010-07-16T05:54:55.023-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='Objects in JavaScript'/><title type='text'>Object oriented Javascript</title><content type='html'>In this blog, I will try to explore how to perform basic object oriented development using Javascript. &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Lets explore by a sample shopping cart application. In this we will have &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;Items which represent shopping items which will have attributes like item name, Price per Unit, Quantity, and method Total Price which will return Total sub price of particular Items.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;Customers can Add Multiple Items to Shopping Cart. This will support two behaviors, adding items to cart, TotalPrice which will return total price of items selected into cart.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;  &lt;span class="Apple-style-span"  style="color:#333399;"&gt;  &lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span"  style="color:#333399;"&gt;&amp;lt;script language=javascript&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;            //Class representing Item. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;      &lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;  function Item(productName,pricePerUnit,numberOfUnits) {&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            this.ProductName = productName;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            this.PricePerUnit = pricePerUnit;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            this.NumberOfItems = numberOfUnits;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;      //Total Price Method will return computed total price of this item&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            this.TotalPrice = function () {&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                return this.PricePerUnit * this.NumberOfItems;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            }&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;//Class representing Shopping Cart which will hold all items&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        function ShoppingCart() {&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;       //internal variable holding all items&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            var Items = new Array();&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;//Method which will add item to shopping cart.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            this.AddItem = function (ShoppingItem) {&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                &lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                Items[Items.length++] = ShoppingItem;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            }&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;//Method which will return computed Total price of all items in cart.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            this.TotalPrice = function () {&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                var totalPrice = 0;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                for (var i = 0; i &lt;&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                    totalPrice += Items[i].TotalPrice();&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                }&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                return totalPrice;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            }&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        var soapItems = new Item("Pears",10,10);&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        var foodItems = new Item("Lays", 20, 10);&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        var cart = new ShoppingCart();&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        cart.AddItem(soapItems);&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        cart.AddItem(foodItems);&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        &lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        alert("Total Price :" +cart.TotalPrice());&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="color:#3333FF;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;    &amp;lt;/script&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In JavaScript classes can be defined using functions. You can alter this definition by accessing function prototype later at any point of time. We will see more in future posts.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8985156687507385407-3351921860435236080?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/3351921860435236080/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=3351921860435236080' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/3351921860435236080'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/3351921860435236080'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2010/07/object-oriented-javascript.html' title='Object oriented Javascript'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-883480868356577334</id><published>2010-07-15T23:08:00.000-07:00</published><updated>2010-07-16T04:35:57.945-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Policy Injection'/><category scheme='http://www.blogger.com/atom/ns#' term='Custom Behaviors'/><category scheme='http://www.blogger.com/atom/ns#' term='Aspect Oriented Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='AOP'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF'/><title type='text'>Policy Injection: WCF Instance Provider</title><content type='html'>&lt;div&gt;Policy Injection is one of Enterprise Application Blocks, which can be used to perform AOP(Aspect Oriented Programming). In this blog I will discuss about using Policy Injection for WCF services. For this to happen your WCF service objects should be created using Policy Injection. WCF provides behavior extension points to customize various aspects. We will see how to provide custom instance provider which will be used by WCF to create WCF objects.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;NOTE: In order for a class to be instantiable through Policy Injection it has to either implement a interface or it should be derived from MarshalByRef class&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In scenarios where you want to intercept WCF calls and apply policies defined in Policy Injection, you need to provide your custom WCF instance provider which will use Policy Injection application block to create instances of your WCF services.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;You need to devise custom instance provider and implement custom behavior to use this custom instance provider. Once you define custom behavior you can apply this behavior to your WCF service end points.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Custom WCF instance provider must implement IInstanceProvider.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;WCF Instance Provider:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;public class PolicyInjectionInstanceProvider:IInstanceProvider&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;    {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        private Type serviceContractType { get; set; }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        private static readonly IUnityContainer container;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        private readonly object _sync=new object();&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        private static readonly TransparentProxyInterceptor injector = new TransparentProxyInterceptor();&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        static PolicyInjectionInstanceProvider()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            container = new UnityContainer()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            .AddNewExtension&lt;interception&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            IConfigurationSource configSource = ConfigurationSourceFactory.Create();&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            PolicyInjectionSettings settings = (PolicyInjectionSettings)configSource.GetSection(PolicyInjectionSettings.SectionName);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            if (settings != null)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                settings.ConfigureContainer(container, configSource);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        public PolicyInjectionInstanceProvider(Type t)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            if (t != null &amp;amp;&amp;amp; !t.IsInterface)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                throw new ArgumentException("Specified type must be an interface.");&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            this.serviceContractType = t;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        #region IInstanceProvider Members&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        public object GetInstance(System.ServiceModel.InstanceContext instanceContext, System.ServiceModel.Channels.Message message)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            Type type = instanceContext.Host.Description.ServiceType;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            if (serviceContractType != null)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                lock (_sync)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                    container.Configure&lt;interception&gt;().SetDefaultInterceptorFor(serviceContractType, injector);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                    container.RegisterType(serviceContractType, type);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                    return container.Resolve(serviceContractType);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            else&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                if (!type.IsMarshalByRef)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                    throw new ArgumentException("Type must inherit from MarhsalByRefObject if no ServiceInterface is Specified.");&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                lock (_sync)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                    container.Configure&lt;interception&gt;().SetDefaultInterceptorFor(type, injector);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                    return container.Resolve(type);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;           &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        public object GetInstance(System.ServiceModel.InstanceContext instanceContext)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            return GetInstance(instanceContext, null);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        public void ReleaseInstance(System.ServiceModel.InstanceContext instanceContext, object instance)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            IDisposable disposable = instance as IDisposable;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            if (disposable != null)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;                disposable.Dispose();&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        #endregion&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0); font-size: 16px; "&gt;Custom Behavior Extension Element:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0); font-size: 16px; "&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0); font-size: 16px; "&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;public class PolicyInjectionBehavior : BehaviorExtensionElement, IEndpointBehavior&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;    {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        public override Type BehaviorType&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            get { return typeof(PolicyInjectionBehavior ); }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        protected override object CreateBehavior()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            return new PolicyInjectionBehavior ();&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;      &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        #region IEndpointBehavior Members&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            Type contractType = endpoint.Contract.ContractType;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            endpointDispatcher.DispatchRuntime.InstanceProvider = new PolicyInjectionInstanceProvider(contractType);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;            &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        public void Validate(ServiceEndpoint endpoint)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;        #endregion&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#663300;"&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small; "&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0); font-size: 16px; "&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Import above defined behavior in web.config using behavior extensions provided by WCF. Note that type attribute should provide fully qualify name (and be careful with spaces), along with assembly and version number. &lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0); font-size: 16px; "&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0); font-size: 16px; "&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&amp;lt;system.serviceModel&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;    &amp;lt;extensions&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;      &amp;lt;behaviorExtensions&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;        &amp;lt;add name="policyInjectionInstanceProvider" type="policyInjectionBehavior, assembly_name, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;      &amp;lt;/behaviorExtensions&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;    &amp;lt;/extensions&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;    &amp;lt;behaviors&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;      &amp;lt;endpointBehaviors&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;        &amp;lt;behavior name="PolicyInjectionProviderBehavior"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;          &lt;policyinjectioninstanceprovider/&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;        &amp;lt;/behavior&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;      &amp;lt;/endpointBehaviors&gt;&lt;/span&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0); "&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;  &amp;lt;/behaviors&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0); "&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&amp;lt;/System.serviceModel&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0); font-size: 16px; "&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0); font-size: 16px; "&gt;Once you import this custom behavior extension, you can use this as behavior in your service model and specify this behavior in service end points. &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;span class="Apple-style-span"  style="color:#006600;"&gt;&lt;span class="Apple-style-span" style="color: rgb(0, 0, 0); font-size: 16px; "&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Whenever client tries to access your WCF service, service object will be created using PolicyInjectionInstanceProvider class. &lt;/div&gt;&lt;div&gt;&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/8985156687507385407-883480868356577334?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/883480868356577334/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=883480868356577334' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/883480868356577334'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/883480868356577334'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2010/07/policy-injection-wcf-instance-provider.html' title='Policy Injection: WCF Instance Provider'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-7831856847079993229</id><published>2010-07-15T06:09:00.000-07:00</published><updated>2010-07-15T06:31:50.374-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Delegate Command'/><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><title type='text'>Silverlight: Auto Notifying Delegate Commands</title><content type='html'>Prism provides Delegate Command equivalent to WPF's Delegate Command (ICommand) implementation in Silverlight. Using Delegate Command in Silverlight you can bind Commands to your view.&lt;div&gt;Whenever state of command changes, you will be doing something like this:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;MyDelegateCmd.RaiseCanExecuteChanged()&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;which will notify binding system to requery binded command's CanExecute. In above approach your code will be scattered with RaiseCanExecteChanged across application. Instead you can use following class which will auto notify binding system.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;public class AutoDelegateCommand&amp;lt;T&gt; : DelegateCommand&amp;lt;T&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;    {&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        public INotifyPropertyChanged ViewModel { get; set; }&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        private void ListenForPropertyChagnedEventAndRequery(INotifyPropertyChanged presentationModel)&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        {&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;            if (presentationModel != null)&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;            {&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;                presentationModel.PropertyChanged += (sender, args) =&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;                {&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;                    QueryCanExecute(args);&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;                };&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;                &lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;            }&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        }&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        public void QueryCanExecute(PropertyChangedEventArgs args)&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        {&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;                this.RaiseCanExecuteChanged();&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;            }&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        }&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        public &lt;span class="Apple-style-span" style="font-style: normal; "&gt;&lt;i&gt;AutoDelegateCommand&lt;/i&gt;&lt;/span&gt;(INotifyPropertyChanged PresentationModel, Action&lt;t&gt; executeMethod)&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;            : base(executeMethod)&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        {&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;            this.ViewModel = PresentationModel;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;            ListenForPropertyChagnedEventAndRequery(PresentationModel);&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        }&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        public &lt;span class="Apple-style-span" style="font-style: normal; "&gt;&lt;i&gt;AutoDelegateCommand&lt;/i&gt;&lt;/span&gt;(INotifyPropertyChanged PresentationModel, Action&lt;t&gt; executeMethod, Func&lt;t,&gt; canExecuteMethod)&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;            : base(executeMethod, canExecuteMethod)&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        {&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;            this.ViewModel = PresentationModel;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;            ListenForPropertyChagnedEventAndRequery(PresentationModel);&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;        }&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;    }&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;By using above delegate command there will be no need to call requery in your view model. AutoDelegateCommand will subscribe to PropertyChanged event of passed in view model, and for each property change event it will raise RaiseCanExecute event. &lt;/div&gt;&lt;div&gt;&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/8985156687507385407-7831856847079993229?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/7831856847079993229/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=7831856847079993229' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/7831856847079993229'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/7831856847079993229'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2010/07/silverlight-auto-notifying-delegate.html' title='Silverlight: Auto Notifying Delegate Commands'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-5026566358037034753</id><published>2010-07-14T21:46:00.000-07:00</published><updated>2010-07-14T22:10:39.067-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Profiling'/><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><title type='text'>Silverlight Profiling</title><content type='html'>You can do profiling of your silverlight 4 applications. Inside VS 2010 you will not be able to do it. You have to fire up VS Command Prompt and run following commands to perform profiling. &lt;div&gt;&lt;ol&gt;&lt;li&gt;&lt;i&gt;VSPerfClrEnv /sampleon&lt;/i&gt;&lt;/li&gt;&lt;li&gt;&lt;i&gt;"C:\Program Files\Internet Explorer\iexplore.exe" http:\\youhostname\pathtosilverlightapplication&lt;/i&gt;&lt;/li&gt;&lt;li&gt;&lt;i&gt;VSPerfCmd /start:sample /output:ProfileTraceFileName /attach&lt;processid&gt;. You can Identify Process ID using Task Manager.&lt;/i&gt;&lt;/li&gt;&lt;li&gt;&lt;i&gt;Run you scenarios.&lt;/i&gt;&lt;/li&gt;&lt;li&gt;&lt;i&gt;VSPerfcmd /detach&lt;/i&gt;&lt;/li&gt;&lt;li&gt;&lt;i&gt;VSPerfCmd /shutdown&lt;/i&gt;&lt;/li&gt;&lt;li&gt;&lt;i&gt;VSPerfClrEnv /off&lt;/i&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;You can open your ProfileTraceFileName file in VS 2010 and see you code execution paths. But before you open you need to provide paths to MS Symbol Server and also path to your profiling application's pdb files to load symbols of your application. You can do this using VS 2010's menu "Debug-&gt;Settings", In settings option window, select symbols under Debug category. Ensure Microsoft Symbol server is selected, and also add path to your pdb files. After doing this, if you open Trace file you can see your code execution paths.&lt;/div&gt;&lt;div&gt;&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/8985156687507385407-5026566358037034753?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/5026566358037034753/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=5026566358037034753' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/5026566358037034753'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/5026566358037034753'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2010/07/silverlight-profiling.html' title='Silverlight Profiling'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-2936719641974759987</id><published>2010-07-14T02:36:00.000-07:00</published><updated>2010-07-14T05:10:36.136-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='VS 2010'/><category scheme='http://www.blogger.com/atom/ns#' term='TFS 2008'/><category scheme='http://www.blogger.com/atom/ns#' term='MS Build'/><category scheme='http://www.blogger.com/atom/ns#' term='Continuous Integration'/><title type='text'>Continuous Integration using TFS 2008 and VS 2010 RC</title><content type='html'>&lt;p&gt;&lt;strong&gt;What is Continuous Integration?&lt;br /&gt;&lt;/strong&gt;Continuous Integration is a practice where integration happens on frequent basis, and each integration will trigger automated build along with verification of code with automated unit tests. &lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Why should we use it?&lt;br /&gt;&lt;/strong&gt;Few of issues you  may face during frequent integrations are:&lt;br /&gt;1. Uncompilable code – Occurred largely whenever somebody changed Public API of code and checked in. Depended code elsewhere got broken due to these changes.&lt;br /&gt;2. Bugs – Due to change in internal logic of one module, without checking impact elsewhere, might cause bugs.&lt;br /&gt;By introducing Continuous Integration into development process you will be auto building for each integration, build report can be mailed to all team members. If somebody checked in some code which made solution uncompilable, with this process in place, you will be able to detect as soon as it is failed and can act upon it.&lt;br /&gt;Once build happens, unit tests can be configured to run tests to verify code correctness. By doing this bugs can be detected early, which will help in reducing effort required to fix that bug. The sooner the easier it will be to fix the bug.&lt;br /&gt;NOTE: Using check-in policy, check-ins of uncompilable code can be avoided.&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;Implementing using TFS 2008 and VS 2010 RC&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;We will discuss how to implement in a situation where VS 2010 RC is used for development, and TFS 2008 server is still being used.&lt;br /&gt;Till TFS 2005, there is no easy way to implement continuous integration. But from TFS 2008, Microsoft started support Continuous Integration which made it easier to implement.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Simple Architecture of TFS 2008&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_l4GuU8wUOUo/TD2GxvKx78I/AAAAAAAADNk/0QrdKn763F0/s1600/tfsarchitecture.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 320px; DISPLAY: block; HEIGHT: 202px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493695309495267266" border="0" alt="" src="http://3.bp.blogspot.com/_l4GuU8wUOUo/TD2GxvKx78I/AAAAAAAADNk/0QrdKn763F0/s320/tfsarchitecture.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;I wont discuss TFS 2008 installation here.&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;Steps to implement continuous Integration using TFS 2008:&lt;br /&gt;&lt;i&gt;Build Server Setup&lt;/i&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;1. Designate a machine as build server. Ensure VS 2008 is installed in this machine to be able to successfully build. Install TFS Build service on this system. (Setup of TFS build service can be found in your TFS server CD under BUILD directory in root).&lt;br /&gt;2. During installation of TFS Build Service, you need to provide credentials using which TFS Build service has to run. Ensure this user is also part of particular projects Build Service group in TFS server.&lt;br /&gt;3. In Visual Studio Open Team Explorer. Right Click on Builds and select Manage Builds to add Build agent to use build server.&lt;br /&gt;&lt;/p&gt;&lt;a href="http://4.bp.blogspot.com/_l4GuU8wUOUo/TD2Hb9vNmlI/AAAAAAAADNs/CmW5ifDirPs/s1600/ManageBuilds.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 257px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493696034960677458" border="0" alt="" src="http://4.bp.blogspot.com/_l4GuU8wUOUo/TD2Hb9vNmlI/AAAAAAAADNs/CmW5ifDirPs/s400/ManageBuilds.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;4. In Manage Build Agents dialog box, click on New button to add build server.&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2H-1uLY9I/AAAAAAAADN0/8PZu8ylOI8g/s1600/ManageBuildAgents1.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 231px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493696634104275922" border="0" alt="" src="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2H-1uLY9I/AAAAAAAADN0/8PZu8ylOI8g/s400/ManageBuildAgents1.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;5. In Build Agent properties window, enter build server details&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_l4GuU8wUOUo/TD2IeORQufI/AAAAAAAADN8/mTmxXetuyxQ/s1600/ManageBuildAgentsProperties.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 242px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493697173269821938" border="0" alt="" src="http://4.bp.blogspot.com/_l4GuU8wUOUo/TD2IeORQufI/AAAAAAAADN8/mTmxXetuyxQ/s400/ManageBuildAgentsProperties.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;6. By clicking on OK, you had successfully added build server to TFS service. Now you can define builds to run on this build server.&lt;br /&gt;Once you had added Build Agent, you can define builds and run on that build server.&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;Create Build Defintion&lt;br /&gt;&lt;/strong&gt;&lt;p&gt;1. Open Team Explorer window, Right click on Builds and select New Build Definition…&lt;br /&gt;&lt;/p&gt;&lt;a href="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2L0pZI-ZI/AAAAAAAADOE/u4zE-BtY8rQ/s1600/NewBuildDefinition.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 221px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493700857042631058" border="0" alt="" src="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2L0pZI-ZI/AAAAAAAADOE/u4zE-BtY8rQ/s400/NewBuildDefinition.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;2. In Build Definition window,&lt;br /&gt;a. In General Tab enter &lt;build&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2MB0Y366I/AAAAAAAADOM/897XGX3Dn4U/s1600/NeBuildDefinition_General.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 234px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493701083332602786" border="0" alt="" src="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2MB0Y366I/AAAAAAAADOM/897XGX3Dn4U/s400/NeBuildDefinition_General.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;b. In Workspace tab, select Source Control Folder from which build files needs to be retrieved and Local Folder, specifies local folder on build server to which these build files will be downloaded for building.&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_l4GuU8wUOUo/TD2MvpC2seI/AAAAAAAADOU/QPHWdhXwXMI/s1600/NeBuildDefinition_Workspace.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 272px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493701870561440226" border="0" alt="" src="http://4.bp.blogspot.com/_l4GuU8wUOUo/TD2MvpC2seI/AAAAAAAADOU/QPHWdhXwXMI/s400/NeBuildDefinition_Workspace.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;c. In Project File, under selected source control folder if TFSBuild.Proj is not created, as is case of New Build Definition, click on Create… to create TFSBuild.Proj.&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_l4GuU8wUOUo/TD2M6cNCGrI/AAAAAAAADOc/nq4ogNLJQDk/s1600/NeBuildDefinition_ProjectFile.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 294px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493702056093031090" border="0" alt="" src="http://3.bp.blogspot.com/_l4GuU8wUOUo/TD2M6cNCGrI/AAAAAAAADOc/nq4ogNLJQDk/s400/NeBuildDefinition_ProjectFile.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;d. In MS Build Project File creation Wizard select solution for which you want to build.&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_l4GuU8wUOUo/TD2NVSaH_tI/AAAAAAAADOk/ffbgkLTWIDc/s1600/ProjectFileWizard.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 264px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493702517320056530" border="0" alt="" src="http://4.bp.blogspot.com/_l4GuU8wUOUo/TD2NVSaH_tI/AAAAAAAADOk/ffbgkLTWIDc/s400/ProjectFileWizard.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;e. Select configuration for Build&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2N40OHeAI/AAAAAAAADO0/kS6rlDq0ESU/s1600/ConfigurationForBuild.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 274px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493703127691917314" border="0" alt="" src="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2N40OHeAI/AAAAAAAADO0/kS6rlDq0ESU/s400/ConfigurationForBuild.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;f. In Options, select unit tests and code analysis criteria and click on Finish to finish build file creation.&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2OHe59XpI/AAAAAAAADO8/WdF-TOuxnk4/s1600/ConfigurationOptions.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 238px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493703379668262546" border="0" alt="" src="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2OHe59XpI/AAAAAAAADO8/WdF-TOuxnk4/s400/ConfigurationOptions.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;g. Select Retention Policy, which lays criteria for build management.&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_l4GuU8wUOUo/TD2OYh3Fe1I/AAAAAAAADPE/9tEvErmNiUc/s1600/RetentionPolicy.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 272px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493703672519293778" border="0" alt="" src="http://3.bp.blogspot.com/_l4GuU8wUOUo/TD2OYh3Fe1I/AAAAAAAADPE/9tEvErmNiUc/s400/RetentionPolicy.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;h. In Build Defaults, specify Build Agent using which you need to execute this Build, and also specify a UNC path of Drop location, to which all build files will be deployed.&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_l4GuU8wUOUo/TD2Ou-Ase4I/AAAAAAAADPM/EWVjzbXNnpI/s1600/BuildDefaults.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 227px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493704058032913282" border="0" alt="" src="http://3.bp.blogspot.com/_l4GuU8wUOUo/TD2Ou-Ase4I/AAAAAAAADPM/EWVjzbXNnpI/s400/BuildDefaults.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;i. In Trigger, for Continuous Integration select Build each check-in option. So, for each check-in, build will happen and unit test and code analysis if specified will also be performed.&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2O7iJYGAI/AAAAAAAADPU/csZbn2jGtME/s1600/BuildTrigger.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 255px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493704273891432450" border="0" alt="" src="http://2.bp.blogspot.com/_l4GuU8wUOUo/TD2O7iJYGAI/AAAAAAAADPU/csZbn2jGtME/s400/BuildTrigger.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;j. By Clicking on OK, you had successfully created a Build Definition. Now each check in will trigger a Build.&lt;br /&gt;3. By double clicking on Build Definition in Team Explorer, Build Explorer window will be displayed where you can monitor builds, and see build reports.&lt;br /&gt;&lt;strong&gt;With TFS 2008 and VSTS2010RC&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;If you are using VSTS 2008 and TFS 2008, you had successfully completed. But if you are using VS 2010RC, you are not yet completed. BuildAgent of TFS 2008 uses MSBuild 3.5 engine to compile your solutions. This build engine will not be able to compile VS 2010 solutions. In order to compile VS 2010 solutions as well, on Build Server performing below steps:&lt;br /&gt;1. Install VS 2010RC on Build Server to make sure .Net 4.0 &amp;amp; SDK’s and MSBuild 4.0 are installed.&lt;br /&gt;2. Configure Team Build 2008 to use MSBuild 4.0 instead of MSBuild 3.5. To do this edit %Program Files%\Microsoft Visual Studio\9.0\Common7\IDE\PrivateAssemblies\TFSBuildService.exe.config and set MSBuildPath property to C:\Windows\Microsoft.Net\Framework\v4.0.30128\&lt;br /&gt;3. Restart the Team Foundation Build Service.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Send Mail with Build Report&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;Till now, you had created Build Agent, created Build Definition, and specified check-in as trigger point for continuous Integration process. Now, we want to take step further a bit, and need to send mail with build status along with build report.&lt;br /&gt; &lt;br /&gt;For this there are two options available.&lt;br /&gt;1. All Team members should subscribe for Build Completed events using Project Alerts window.&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_l4GuU8wUOUo/TD2PXxxoYHI/AAAAAAAADPc/JrTFux-w_18/s1600/ProjectAlerts.jpg"&gt;&lt;img style="TEXT-ALIGN: center; MARGIN: 0px auto 10px; WIDTH: 400px; DISPLAY: block; HEIGHT: 202px; CURSOR: hand" id="BLOGGER_PHOTO_ID_5493704759123140722" border="0" alt="" src="http://3.bp.blogspot.com/_l4GuU8wUOUo/TD2PXxxoYHI/AAAAAAAADPc/JrTFux-w_18/s400/ProjectAlerts.jpg" /&gt;&lt;/a&gt;&lt;br /&gt;Using alert “A build completes” you can specify multiple email id’s in Send to for whom you want to alert. This needs to be done for all users.&lt;br /&gt;2. Using Custom Tasks.&lt;br /&gt;You can develop custom tasks which can be executed during Build Process. A custom task can be defined in a class library by deriving from Task abstract class, or by implementing ITask interface.&lt;br /&gt;MSBuild Extensions pack already implemented some custom tasks, of which send mail is one task.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br /&gt;Though Continuous integration doesn’t prevent check-ins of uncompilable code or buggy code, it will enable to identify them quickly and act upon.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8985156687507385407-2936719641974759987?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/2936719641974759987/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=2936719641974759987' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/2936719641974759987'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/2936719641974759987'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2010/07/continuous-integration-using-tfs-2008.html' title='Continuous Integration using TFS 2008 and VS 2010 RC'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_l4GuU8wUOUo/TD2GxvKx78I/AAAAAAAADNk/0QrdKn763F0/s72-c/tfsarchitecture.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8985156687507385407.post-1067409900984278877</id><published>2010-07-14T02:18:00.000-07:00</published><updated>2010-07-14T02:30:54.511-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Unit Test'/><title type='text'>Unit Testing</title><content type='html'>&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;In this article we will discuss about unit testing, why we need it and what are various quirks in implementing them.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;Why?&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;In today’s world of ever increasing complexity of software, where requirements are always changing, we need to control cost of delivery with highest quality. Whoever worked on medium to long term projects will know the complexity of bug fixing after release and maintenance. &lt;br /&gt;Cost of fixing bug increases with lifecycle of software. During requirements phase it costs cheap, and it increases with stages like development, testing, maintenance. To detect bugs in early cycle of development we need a mechanism. Unit test provides a mechanism to detect bugs in development cycle.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;What is Unit Test?&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;In unit test we will take unit of code and isolate it from dependencies and inspect it for any defects. Mostly unit will be a method or set of method in case we are testing public APIs.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;How to write Unit Tests?&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;Before discussing how to write unit tests, let’s inspect problem more thoroughly so that we can understand our approach better. &lt;br /&gt;In development mostly we find following problems relating to quality&lt;br /&gt;1. Requirements not implemented.&lt;br /&gt;2. Requirements not implemented properly.&lt;br /&gt;3. Missed Requirements (Requirements are not defined)&lt;br /&gt;Developer might have missed some requirements to implement in production code, few times implemented but not correctly implemented, in other cases requirements are not defined, but developer has implemented them during development.&lt;br /&gt;Our unit tests should be able to detect above problems. There are three unit test approaches to tackle above problems&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;Structural Unit Testing&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;In structural approach we will try to write unit test cases based on production code we are trying to test. Here we will use code coverage, number of operators, operands in a statement, number of parameters as benchmark in deciding how many tests cases are required.&lt;br /&gt;a. We will try to achieve 100% code coverage.&lt;br /&gt;b. Depending upon number of parameters, number of ways to invoke method will increases. To reduce complexity try to keep number of parameters to minimal. We will try to write unit test cases covering all parameters.&lt;br /&gt;c. Need to write test cases using Boundary values.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;Functional Unit Testing&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;Using functional testing, we will write unit test cases for each requirement. Here we will take requirement/functionality as unit try to test in that aspect isolating class from other dependencies.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;No approach&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;No approach – doesn’t have specific set, but written by experienced developers who will be able to think of cases using which they can test class. It can effective but not a systematic way to assure things are always the way we wanted.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Using Structural testing we will be able to test missed requirements, bugs in code. Using functional testing we will able to detect improper implementation of requirements and bugs as well. &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;Does it solve the problem?&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;Unit tests are better to ensure quality to a finite set of scenarios you had tested. You can reasonably assure that for scenarios you had tested, your code will work. Unit tests acts as a safety net whenever code changes are required. There are still uncertain scenarios which are not tested, where bugs might be lurking. Overtime you will reduce uncertainty in quality.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;I can’t afford to write Unit Tests?&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;In this fast paced world, it looks obvious that there is no time to write unit test cases. By not writing unit tests, you are increasing uncertainty in your code quality. If you take a horizon of more than one year for your code base, by investing in Unit Test you will be reducing effort required for testing and maintenance. Cost is less during development, but increases in maintenance. Unless are running away by not maintaining your code base, you will reduce costs by investing in unit tests in maintenance phase which will be huge.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;What about Integration Tests?&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;You should be able to test Integration tests as well in Unit Test. Integration scenarios are scenarios where class being tested will rely on another class to perform certain task. In those scenarios, typically you will be using mocking dependencies and setting expectations. It is expected by dependent object to perform those expectations as is envisioned in our unit tests. Make these expectations as unit tests of dependent object. By ensuring this you are testing integration scenarios as well. It requires careful planning but never impossible.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;Apart from that make your classes less chatty. Reduce the surface area of interaction between classes. By doing this you are decreasing integration scenarios and making fewer expectations.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;My class is Un-Testable?&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;During testing most often we will stumble upon code which is not testable. Most of the times you should be able to refactor code and make it testable. If you are using Static Method invocations, which are not testable, you can create a proxy and use this proxy to interact with static objects and methods. By doing this you can mock proxy and make this untestable code into testable.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;Reduce Maintenance of Unit Tests&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;As with any code, overtime as with changing code, corresponding unit tests will also get changed. To reduce maintenance cost of unit tests without compromising quality, you can test only public APIs, i.e. Public methods, as these are the methods used by users. Using these methods you should be able to invoke private and protected functionality as well and should be able to test them. This is sort of adding features of structural testing into functional testing and should be able to achieve same results. By testing only public API now your unit tests are more resilient to refactoring of class internals.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;Conclusion&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;If you take cost of software, maintenance costs are huge than construction costs itself. In order to be properly equipped to reduce maintenance costs, unit tests are most indispensible tool. Unit tests reduce uncertainty in your code. &lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8985156687507385407-1067409900984278877?l=kirangudipudi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kirangudipudi.blogspot.com/feeds/1067409900984278877/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8985156687507385407&amp;postID=1067409900984278877' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/1067409900984278877'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8985156687507385407/posts/default/1067409900984278877'/><link rel='alternate' type='text/html' href='http://kirangudipudi.blogspot.com/2010/07/unit-testing.html' title='Unit Testing'/><author><name>Kiran Kumar G</name><uri>http://www.blogger.com/profile/16177138782308325448</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_l4GuU8wUOUo/Sysq_AWp1-I/AAAAAAAADAU/24ECXycV-Zw/S220/DSC01169.JPG'/></author><thr:total>2</thr:total></entry></feed>
