<?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-1535807340352183565</id><updated>2011-10-10T09:22:41.753-07:00</updated><category term='C#'/><category term='Builder'/><category term='Agile'/><category term='CQRS'/><category term='.NET 4.0'/><category term='Linq'/><category term='Sharepoint'/><category term='Dynamic'/><category term='Event Sourcing'/><category term='UserProfile'/><category term='Silverlight'/><category term='Redpoint'/><category term='Testing'/><title type='text'>In a Perfect World</title><subtitle type='html'>Coding exploits from the real world.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://dailyandersen.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://dailyandersen.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Bruce</name><uri>http://www.blogger.com/profile/09355720423868876069</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1535807340352183565.post-3054411827381183637</id><published>2011-10-10T09:22:00.000-07:00</published><updated>2011-10-10T09:22:42.044-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><category scheme='http://www.blogger.com/atom/ns#' term='Redpoint'/><title type='text'>Fun with Silverlight</title><content type='html'>In my previous post, I mentioned a bit about the architecture from a project I was working on.&amp;nbsp; In this post I wanted to mention a couple of the things that we did with Silverlight and how they worked out.&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;One solution for both silverlight and the client services (run as a console app)&lt;/li&gt;&lt;ul&gt;&lt;li&gt;It was nice to have one solution rather than multiple.&amp;nbsp; And changing the start-up type of the project to multiple allowed both apps to run at once.&lt;/li&gt;&lt;li&gt;Debugging was a breeze in either run-time.&lt;/li&gt;&lt;li&gt;There was no way to enforce start-up order/delay to make sure the services were running before starting silverlight.&amp;nbsp; This was rectified by adding some retry logic when connecting to the services.&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;Sharing of code between the two runtimes (Silverlight and .NET)&lt;/li&gt;&lt;ul&gt;&lt;li&gt;We went with a file sharing approach opposed to conditional compiler statements.&lt;/li&gt;&lt;li&gt;Sharing files was a bit of a pain, although with partial classes you could have three implementations.&amp;nbsp; A common version (shared between both), a .NET version, and a Silverlight version.&lt;/li&gt;&lt;li&gt;Resharper bound to one runtime and would not search in the other, i.e. finding usages of a method only returned results from one runtime not both.&lt;/li&gt;&lt;li&gt;Navigating to the file sometimes would go to the link, i.e. alt+shift+L&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;Rich domain without DTOs, the entities themselves were sent to the Silverlight side.&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&amp;nbsp;This got rid of the required set of mappers/builders to translate from server to client and back.&lt;/li&gt;&lt;li&gt;Encapsulation became much more difficult.&lt;/li&gt;&lt;li&gt;Events from the server could be passed as is to the client and would work the same, i.e. no translation/mapping work required.&lt;/li&gt;&lt;li&gt;some navigation properties on the entities (those that created a circular link, like a link back to the parent) could not be serialized requiring some custom logic on the Silverlight side to set them.&lt;/li&gt;&lt;li&gt;When working with a system where events are used, the only time that a domain object is sent to the client is during a query.&amp;nbsp; With commands a domain object is never sent back to the server.&amp;nbsp; This made DTOs a bit heavy weight of a solution.&lt;/li&gt;&lt;li&gt;I would have liked to do something unique with Queries like just return ViewModels directly if possible.&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;Testing in Silverlight is crappy&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Debugging is difficult as you have to start up the silverlight client by running it as a startup project.&amp;nbsp; The error itself has to be closed to then edit the code.&lt;/li&gt;&lt;li&gt;Statlight allowed the tests to be run as part of the build, but this required the build service to be able to interact with the desktop (to launch the silverlight client which then runs the test).&lt;/li&gt;&lt;li&gt;Integration tests proved to be a pain as well as you have to mock out the data returned from the services.&amp;nbsp; This would be similar as pre-populating/seeding a db with data on the server side, but in this case you have to match web service calls and return the correct data.&amp;nbsp; Mocking can be used for this, but with the asynchronous nature it is ugly and difficult to read.&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1535807340352183565-3054411827381183637?l=dailyandersen.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dailyandersen.blogspot.com/feeds/3054411827381183637/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dailyandersen.blogspot.com/2011/10/fun-with-silverlight.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/3054411827381183637'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/3054411827381183637'/><link rel='alternate' type='text/html' href='http://dailyandersen.blogspot.com/2011/10/fun-with-silverlight.html' title='Fun with Silverlight'/><author><name>Bruce</name><uri>http://www.blogger.com/profile/09355720423868876069</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1535807340352183565.post-5336456554826446059</id><published>2011-10-07T08:03:00.000-07:00</published><updated>2011-10-07T08:03:59.769-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Event Sourcing'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><category scheme='http://www.blogger.com/atom/ns#' term='Redpoint'/><category scheme='http://www.blogger.com/atom/ns#' term='CQRS'/><title type='text'>CQRS and Event Sourcing in .NET 4 and Silverlight</title><content type='html'>In my latest engagement we built an occasionally connected application that required synchronization, concurrency/conflict resolution, and event driven.&amp;nbsp; Taking all these requirements into account we came up with a solution that utilized commands and event sourcing to satisfy the requirements.&amp;nbsp; From a high-level this architecture looked something like:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-BmXBv2QTORw/To8HaL3pi7I/AAAAAAAAAA8/XFWuWdHDQhw/s1600/FieldClaims_LayerDiagram.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://2.bp.blogspot.com/-BmXBv2QTORw/To8HaL3pi7I/AAAAAAAAAA8/XFWuWdHDQhw/s320/FieldClaims_LayerDiagram.png" width="268" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Moving through the layers, we start with Silverlight. There was some debate as to use WPF or Silverlight.&amp;nbsp; We went with Silverlight and it made it easier to make a web based version of the applicatoin.&amp;nbsp; The difficulty that this added was another framework.&amp;nbsp; We ended up sharing code between the silverlight projects and .net projects which is a major pain (opposed to compiler directives).&lt;br /&gt;&lt;br /&gt;We made extensive use of MVVM and Prism.&amp;nbsp; The view models required event hookups between both the views and the viewmodels as well as from the model to the view models.&amp;nbsp; In our design we used a rich domain that raised events which could be used to trigger changes in the UI.&lt;br /&gt;&lt;br /&gt;To achieve this, we had to have an identity map on the silverlight side.&amp;nbsp; Using CQRS, we would query for the object once and then receive events on that object.&amp;nbsp; Any updates to the object would be sent via a command.&amp;nbsp; Our WCF services were quite simple, loads, property changed events, collection changed events, and sending commands.&lt;br /&gt;&lt;br /&gt;The silverlight side (OOB) communicated with a small console app that was WCF over TCP.&amp;nbsp; These client services handled queries, commands, and events as IO.&amp;nbsp; For the service binding we used the channel factory which enabled us to use dynamic proxies.&amp;nbsp; Although, the proxies did not change much as there was extensive use of generics to make the signatures of the methods dynamic.&amp;nbsp; To reference objects dynamically from a service call that was bound to a concrete type, we created an object called EntityUID.&amp;nbsp; This contained a type and an id, just enough to uniquely identify the entity.&amp;nbsp; This allowed our service definitions to be quite simple, for example load might be something like: T Load&lt;t&gt;(EntityUID entityUid).&lt;/t&gt;&lt;br /&gt;&lt;br /&gt;I will post more about the service layer in another post, but from a high level, the service layer was composed using Entity Framework Code First, Unity for dependency injection, the built in .net property changed events and observable collections, SQL CE, and WCF.&lt;br /&gt;&lt;br /&gt;Look for more posts on the lessons learned from this architecture as well as some neat tricks to doing things with .net 4. &lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1535807340352183565-5336456554826446059?l=dailyandersen.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dailyandersen.blogspot.com/feeds/5336456554826446059/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dailyandersen.blogspot.com/2011/10/cqrs-and-event-sourcing-in-net-4-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/5336456554826446059'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/5336456554826446059'/><link rel='alternate' type='text/html' href='http://dailyandersen.blogspot.com/2011/10/cqrs-and-event-sourcing-in-net-4-and.html' title='CQRS and Event Sourcing in .NET 4 and Silverlight'/><author><name>Bruce</name><uri>http://www.blogger.com/profile/09355720423868876069</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-BmXBv2QTORw/To8HaL3pi7I/AAAAAAAAAA8/XFWuWdHDQhw/s72-c/FieldClaims_LayerDiagram.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1535807340352183565.post-8548892602020408496</id><published>2010-12-21T08:20:00.000-08:00</published><updated>2010-12-21T08:21:22.153-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Agile'/><category scheme='http://www.blogger.com/atom/ns#' term='Redpoint'/><title type='text'>Why I Miss Pair Programming</title><content type='html'>One of the best parts of my job is working with some really great people.  People who are at the top of their field and you can learn a lot from.  In Agile there is a practice called pair programming, where you pair up with another teammate on a project.  One of the biggest criticisms that I have heard about this practice is that it wastes people’s time, since you can’t be twice as productive in terms of raw coding when pairing.  Of course the natural response to this criticism is that while you don’t produce twice as much code, you do produce much better code.  The pair creates code that is more maintainable and is less prone to errors.&lt;br /&gt;&lt;br /&gt;Anyway, getting back to the original subject, I have been out on projects without pair programming now for some time and I can honestly say that I miss it.  It is very rewarding to work with someone else who brings a different set of ideas when solving a problem.  It becomes magical when it creates something that is far cleverer than what you could accomplish alone.&lt;br /&gt;&lt;br /&gt;Of course pair programming doesn’t typically start all roses.  Usually it begins with one person being a keyboard hog or at least that is how it feels sometimes.  Also, it can feel as though the other person is challenging your ideas and it can put you in a defensive mindset.&lt;br /&gt;&lt;br /&gt;I typically try to mitigate the first issue (keyboard hog) by using an approach called ping-pong.  Basically one person in the pair is in charge of writing a test which breaks.  The other person is then responsible for fixing this test and then creates the next broken test which the first person then has to fix.  This not only breaks the monogamy of a one typer show, it also enforces a Test Driven Development approach.&lt;br /&gt;&lt;br /&gt;For the second issue, having a feeling of being attacked and adopting a defensive mindset, it comes down to comfort with the other person.  There are always more than one way to do things, and adopting the “do the simplest thing that could possibly work” kind of limits that set of ways to do things.  However this type of discussion, determining what is the best way to solve a problem, is what supplies the learning experience.  After all, most people can create code given pseudo-code.  So the best approach is to really talk through each option and try to determine the best solution. When you have a solution that both people think is best, the chances of it working well are that much higher.&lt;br /&gt;&lt;br /&gt;The design type banter is what I miss most about Pair Programming.  It is exciting to hear alternative solutions, to come up with real reasons on why one solution is better than another, and to try something different, an approach that you might not have thought of or done if you were not pairing.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1535807340352183565-8548892602020408496?l=dailyandersen.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dailyandersen.blogspot.com/feeds/8548892602020408496/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dailyandersen.blogspot.com/2010/12/why-i-miss-pair-programming.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/8548892602020408496'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/8548892602020408496'/><link rel='alternate' type='text/html' href='http://dailyandersen.blogspot.com/2010/12/why-i-miss-pair-programming.html' title='Why I Miss Pair Programming'/><author><name>Bruce</name><uri>http://www.blogger.com/profile/09355720423868876069</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1535807340352183565.post-830257890391338274</id><published>2010-12-02T09:45:00.000-08:00</published><updated>2010-12-02T09:45:18.369-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Sharepoint'/><category scheme='http://www.blogger.com/atom/ns#' term='Redpoint'/><category scheme='http://www.blogger.com/atom/ns#' term='UserProfile'/><title type='text'>Sharepoint UserProfileManager Security and Sharing</title><content type='html'>For this blog I will look at the Sharepoint UserProfileManager.  In particular how to get around user permissions in regards to profile management and to share an instance accross an application.&lt;br /&gt;&lt;br /&gt;The user profile manager will always check the users permissions to determine if they have the ability to manage user profiles.  This is not feasible for an internet application.  Instantiating an instance of the user profile manager is expensive so using a common instance allievates this cost.&lt;br /&gt;&lt;br /&gt;To get around the security concerns, there are two things that must be done.  First, you must run with elevated privledges.  Second you must clear out the httpcontext so the useris not present.  This would be something like:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;"&gt;HttpContext.Current = Nothing&lt;br /&gt;SPSecurity.RunWithElevatedPrivileges(AddressOf GetUserProfileManager)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Obviously this is a very simple implementation, for a real one lets add in the instance sharing:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;"&gt;Private ReadOnly Property UserProfileManager() As UserProfileManager&lt;br /&gt;    Get&lt;br /&gt;        If (_userProfileManager Is Nothing) Then&lt;br /&gt;            SyncLock (_syncLock)&lt;br /&gt;                If (_userProfileManager Is Nothing) Then&lt;br /&gt;                    SPSecurity.RunWithElevatedPrivileges(AddressOf GetUserProfileManager)&lt;br /&gt;                End If&lt;br /&gt;            End SyncLock&lt;br /&gt;        End If&lt;br /&gt;        Return _userProfileManager&lt;br /&gt;    End Get&lt;br /&gt;End Property&lt;br /&gt;&lt;br /&gt;Private Sub GetUserProfileManager()&lt;br /&gt;    Using systemSite = New SPSite(_site().ID) 'where _site is Func(Of SPSite)&lt;br /&gt;        Dim siteContext As ServerContext = ServerContext.GetContext(systemSite)&lt;br /&gt;        Dim currentContext As HttpContext = HttpContext.Current&lt;br /&gt;        HttpContext.Current = Nothing&lt;br /&gt;        Try&lt;br /&gt;            _userProfileManager = New UserProfileManager(siteContext)&lt;br /&gt;        Finally&lt;br /&gt;            HttpContext.Current = currentContext&lt;br /&gt;        End Try&lt;br /&gt;    End Using&lt;br /&gt;End Sub&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now we are setting a local variable to the instance of the UserProfileManager the first time that we use it.  This code assumes a singleton type class hence the multithreading locking.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So this is all fine and dandy, but how do we use the UserProflieManager?  Unfortunately even when we use the manager, we still need to cleare out the current HttpContext, i.e.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;"&gt;Public Function Exists(ByVal userName As String) As Boolean Implements IUserInformationSharepointProfileDataAccess.Exists    &lt;br /&gt;    'Get User Profile manager BEFORE setting the current context to nothing&lt;br /&gt;    Dim userProfileManager As UserProfileManager = Me.UserProfileManager&lt;br /&gt;&lt;br /&gt;    Dim currentContext As HttpContext = HttpContext.Current&lt;br /&gt;    HttpContext.Current = Nothing&lt;br /&gt;    Try&lt;br /&gt;        Return userProfileManager.UserExists(userName)&lt;br /&gt;    Finally&lt;br /&gt;        HttpContext.Current = currentContext&lt;br /&gt;    End Try&lt;br /&gt;End Function&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This exposes a potential issue where we get the UserProfileManager after we set the HttpContext to nothing.  This causes issues in the SPSecurity.RunWithElevatedPrivileges method, hence the comment in the code.&lt;br /&gt;&lt;br /&gt;With all the changes above, we can now use one instance of the manager for the life of the application and for any user.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1535807340352183565-830257890391338274?l=dailyandersen.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dailyandersen.blogspot.com/feeds/830257890391338274/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dailyandersen.blogspot.com/2010/12/sharepoint-userprofilemanager-security.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/830257890391338274'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/830257890391338274'/><link rel='alternate' type='text/html' href='http://dailyandersen.blogspot.com/2010/12/sharepoint-userprofilemanager-security.html' title='Sharepoint UserProfileManager Security and Sharing'/><author><name>Bruce</name><uri>http://www.blogger.com/profile/09355720423868876069</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1535807340352183565.post-7290915531274241382</id><published>2010-12-02T07:50:00.000-08:00</published><updated>2010-12-02T09:13:29.241-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Testing'/><category scheme='http://www.blogger.com/atom/ns#' term='Sharepoint'/><category scheme='http://www.blogger.com/atom/ns#' term='Redpoint'/><title type='text'>Unit Testing With SharePoint</title><content type='html'>This article tackles some of the unit and integration testing woes when using the sharepoint object model.&lt;br /&gt;&lt;br /&gt;Testing with Sharepoint kind of sucks.  First it lacks interfaces making mocking impossible.  Second it uses the non-generic functions, like IEnumerable rather than IEnumerable(Of x).  Third, it typically returns complex objects from service methods and these complex objects are not DTOs or value objects and require more than a simple new operator to instantiate.  Fourth, there are a couple of fundemental objects that are used a lot, SPSite and SPWeb come to mind.&lt;br /&gt;&lt;br /&gt;To work around the lack of interfaces, a facade works well.  In this example, I am working with the Sharepoint User Profile Object.  Lets say we want to get/set values for a users profile.&lt;br /&gt;&lt;br /&gt;We would need an interface like this:&lt;br /&gt;&lt;br /&gt;&lt;pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;Public Interface ISharePointUserProfileFacade&lt;br /&gt;Default ReadOnly Property Item(ByVal propertyName As String) As ISharePointUserProfileValueCollectionFacade&lt;br /&gt;End Interface&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Notice that I also need to create a second facade for the valuecollection that is returned from the built-in UserProfileValueCollection.  For this example, the interface could look like:&lt;br /&gt;&lt;br /&gt;&lt;pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;Public Interface ISharePointUserProfileValueCollectionFacade&lt;br /&gt;Inherits IEnumerable&lt;br /&gt;Property Value() As Object&lt;br /&gt;End Interface&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now we have a facade that we can substitute in a mock for when testing.&lt;br /&gt;&lt;br /&gt;I will include a quick implementation of the interface for those interested.&lt;br /&gt;&lt;br /&gt;&lt;pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;Public Class SharePointUserProfile&lt;br /&gt;Implements ISharePointUserProfileFacade&lt;br /&gt;&lt;br /&gt;Public Sub New(ByVal userProfile As UserProfile)&lt;br /&gt;_userProfile = userProfile&lt;br /&gt;End Sub&lt;br /&gt;&lt;br /&gt;Public ReadOnly Property Item(ByVal propertyName As String) As ISharePointUserProfileValueCollectionFacade Implements ISharePointUserProfileFacade.Item&lt;br /&gt;Get&lt;br /&gt;If String.IsNullOrEmpty(propertyName) Then&lt;br /&gt;Throw New ArgumentException("Null or empty property name specified")&lt;br /&gt;End If&lt;br /&gt;&lt;br /&gt;Return New SharePointUserProfileValueCollection(_userProfile(propertyName))&lt;br /&gt;End Get&lt;br /&gt;End Property&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In this code we can create the facade from a UserProfile Sharepoint object.  Now for the value collection:&lt;br /&gt;&lt;br /&gt;&lt;pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;Public Class SharePointUserProfileValueCollection&lt;br /&gt;Implements ISharePointUserProfileValueCollectionFacade&lt;br /&gt;&lt;br /&gt;Public Sub New(ByVal collection As UserProfileValueCollection, ByVal _trackChangeDelegate As TrackChange)&lt;br /&gt;_collection = collection&lt;br /&gt;_trackChange = _trackChangeDelegate&lt;br /&gt;End Sub&lt;br /&gt;&lt;br /&gt;Public Property Value() As Object Implements ISharePointUserProfileValueCollectionFacade.Value&lt;br /&gt;Get&lt;br /&gt;Return _collection.Value&lt;br /&gt;End Get&lt;br /&gt;Set(ByVal value As Object)&lt;br /&gt;If (_collection.Value Is Nothing AndAlso (Not value Is Nothing)) OrElse (Not _collection.Value Is Nothing AndAlso Not _collection.Value.Equals(value)) Then&lt;br /&gt;' track change&lt;br /&gt;If Not _trackChange Is Nothing Then&lt;br /&gt;_trackChange(_collection.Property.Name, value)&lt;br /&gt;End If&lt;br /&gt;End If&lt;br /&gt;_collection.Value = value&lt;br /&gt;End Set&lt;br /&gt;End Property&lt;br /&gt;End Class&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In this example, the use of a facade allows additional behavior to existing value collection object.  I have added a change tracker to the set property fairly easily.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1535807340352183565-7290915531274241382?l=dailyandersen.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dailyandersen.blogspot.com/feeds/7290915531274241382/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dailyandersen.blogspot.com/2010/12/unit-testing-with-sharepoint.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/7290915531274241382'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/7290915531274241382'/><link rel='alternate' type='text/html' href='http://dailyandersen.blogspot.com/2010/12/unit-testing-with-sharepoint.html' title='Unit Testing With SharePoint'/><author><name>Bruce</name><uri>http://www.blogger.com/profile/09355720423868876069</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1535807340352183565.post-3728455867990897769</id><published>2010-05-11T13:06:00.000-07:00</published><updated>2010-05-11T13:20:12.073-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Builder'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Redpoint'/><title type='text'>Ruby XML Builder in C# .NET 4.0</title><content type='html'>Now that .NET has the power of a dynamic language the first comparison is going to be with the powerhouses that are currently gaining momentum.  I decided to implement something similar to the Builder library in Ruby.  It seemed like a fun starting point that would not be over-involving.&lt;br /&gt;&lt;br /&gt;Before I go too far, lets see an example in ruby, although don't worry about the ruby itself as this is a blog more on .NET 4.0&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;"&gt;...&lt;br /&gt;xml.contacts do&lt;br /&gt;xml.contact(:type =&gt; "work") do&lt;br /&gt;xml.name("Joe Smith")&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Basically this uses missing methods to add elements to an XML document.  For attributes, it uses hashes.  For nesting elements, it uses blocks of code.&lt;br /&gt;&lt;br /&gt;Some might say that Linq to XML already implemented a simple XML document builder with it's functional construction approach.  This is a little different from that.&lt;br /&gt;&lt;br /&gt;The first hurdle is types and syntax.  Hashes translate to KeyValuePairs in a dictionary which is a bit messy done in a parameter call.  Blocks of code, lambdas in .NET require themselves to be cast as a delegate or syntax trees which again is messy.  I went with the easy approach, strings for the element/value pairs, string for the value, and use nested calls rather than code blocks.  So with all these changes in place here is a sample use of the XmlBuilder:&lt;br /&gt;&lt;pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;"&gt;dynamic xmlBuilder = new XmlBuilder();&lt;br /&gt;var result = xmlBuilder.Contacts(&lt;br /&gt;         xmlBuilder.Contact("type", "work",&lt;br /&gt;             xmlBuilder.Name("Joe Smith"),&lt;br /&gt;             xmlBuilder.Phone("type", "work", "(123) 456-7890"),&lt;br /&gt;             xmlBuilder.Address("type", "home",&lt;br /&gt;                 xmlBuilder.Street("123 Main St."),&lt;br /&gt;                 xmlBuilder.City("SpringField"))));&lt;br /&gt;&lt;br /&gt;Console.WriteLine("XML: \n" + result.ToString());&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;It is not too much worse than the Ruby version which is a win with a more verbose language like C#.  Yes, there were a few concessions i.e. not using hashes for elements.&lt;br /&gt;&lt;br /&gt;So now that we have our requirements lets look at the code necessary to meet them.&lt;br /&gt;&lt;br /&gt;The first thing that we need is to inherit from DynamicObject and override the TryInvokeMember() function.  This allows us to always accept method calls which do not match real methods on the object.  This is done as follows:&lt;br /&gt;&lt;pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;"&gt;class XmlBuilder : DynamicObject&lt;br /&gt;{&lt;br /&gt;  public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)&lt;br /&gt;  {&lt;br /&gt;      //parse arguments&lt;br /&gt;      //Create XML Document&lt;br /&gt;      //Set Result&lt;br /&gt;      //Return true (handled method)&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Simple enough, the next hurdle is going to be argument parsing.  This overload puts all our arguments in an object array, not all that helpful.  Now we will need to do some casting and parsing.  In our case we are looking to break up the elements from the nested XDocuments constructed from the nested calls, and the optional value parameter.  Something like the following should work:&lt;br /&gt;&lt;pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;"&gt;private dynamic ParseArguments(object[] arguments)&lt;br /&gt;{&lt;br /&gt;var documents = new List&lt;xdocument&gt;();&lt;br /&gt;var strings = new List&lt;string&gt;();&lt;br /&gt;var attributes = new List&lt;xattribute&gt;();&lt;br /&gt;string text = null;&lt;br /&gt;&lt;br /&gt;foreach (var arg in arguments)&lt;br /&gt;{&lt;br /&gt;    XDocument xDocArgument;&lt;br /&gt;    string stringArgument;&lt;br /&gt;    if (CastAs&lt;xdocument&gt;(arg, out xDocArgument))&lt;br /&gt;        documents.Add(xDocArgument);&lt;br /&gt;    else if (CastAs&lt;string&gt;(arg, out stringArgument))&lt;br /&gt;        strings.Add(stringArgument);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;if (strings.Count % 2 == 1)&lt;br /&gt;{&lt;br /&gt;    text = strings.Last();&lt;br /&gt;    strings.RemoveAt(strings.Count - 1);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;for (int i = 0; i &lt;&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;        &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;return new { Documents = documents, Attributes = attributes, Text = text };&lt;br /&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;&lt;/xdocument&gt;    &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;&lt;/xdocument&gt;    &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;private bool CastAs&lt;t&gt;(object value, out T castedValue)&lt;br /&gt;&lt;/t&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;    &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;{&lt;br /&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;&lt;/xdocument&gt;        &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;if (value is T)&lt;br /&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;&lt;/xdocument&gt;        &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;{&lt;br /&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;&lt;/xdocument&gt;            &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;castedValue = (T)value;&lt;br /&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;&lt;/xdocument&gt;            &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;return true;&lt;br /&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;&lt;/xdocument&gt;        &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;}&lt;br /&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;&lt;/xdocument&gt;        &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;castedValue = default(T);&lt;br /&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;&lt;/xdocument&gt;        &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;return false;&lt;br /&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;&lt;/xdocument&gt;    &lt;xdocument&gt;&lt;string&gt;&lt;xattribute&gt;&lt;xdocument&gt;&lt;string&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xattribute&gt;&lt;/string&gt;&lt;/xdocument&gt;&lt;/xdocument&gt;&lt;/xdocument&gt;&lt;/pre&gt;&lt;br /&gt;We start by breaking up the arguments into to lists then further separate them into the three parameters.  Casting ugliness was a big motivator to the move to dynamic types, and you can see how most of the code is just concerned with typing issues.  I did get to sneak in an anonymous object as the return value.&lt;br /&gt;&lt;br /&gt;Now that we have the parameters, the rest is simple:&lt;br /&gt;&lt;pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;"&gt;public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)&lt;br /&gt;{&lt;br /&gt;  var arguments = ParseArguments(args);&lt;br /&gt;  XElement rootElement = new XElement(binder.Name, arguments.Text, arguments.Attributes);&lt;br /&gt;  foreach (XDocument xDocument in arguments.Documents)&lt;br /&gt;      rootElement.Add(xDocument.FirstNode);&lt;br /&gt;&lt;br /&gt;  result = new XDocument(rootElement);&lt;br /&gt;  return true;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1535807340352183565-3728455867990897769?l=dailyandersen.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dailyandersen.blogspot.com/feeds/3728455867990897769/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dailyandersen.blogspot.com/2010/05/ruby-xml-builder-in-c-net-40.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/3728455867990897769'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/3728455867990897769'/><link rel='alternate' type='text/html' href='http://dailyandersen.blogspot.com/2010/05/ruby-xml-builder-in-c-net-40.html' title='Ruby XML Builder in C# .NET 4.0'/><author><name>Bruce</name><uri>http://www.blogger.com/profile/09355720423868876069</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1535807340352183565.post-513587150465572297</id><published>2010-05-10T14:56:00.000-07:00</published><updated>2010-05-11T13:19:45.422-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Dynamic'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Redpoint'/><category scheme='http://www.blogger.com/atom/ns#' term='Linq'/><title type='text'>.NET 4.0 Dynamics and Linq Anonymous Types</title><content type='html'>I have been trying to find an occassion in an existing application where Dynamics would be a good fit.  The big problem is that the existing application is strongly-typed.  Use of dynamic methods doesn't really exist.  Until I started looking at some Linq usage.  Linq uses anonymous types quite a bit.  This was fine as long as the type was used locally, it could not be passed around and used.  What a perfect fit for Dyanmics.  Here is an example:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;"&gt;private static string[] primaryColors = new string[] { "Blue", "Red" };&lt;br /&gt;private static string[] secondaryColors = new string[] { "Black", "Silver" };&lt;br /&gt;&lt;br /&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;  foreach (var result in GetColorList())&lt;br /&gt;  {&lt;br /&gt;      Console.WriteLine("Color Selection {0} and {1}", result.PrimaryColor, result.SecondaryColor);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static IEnumerable&lt;dynamic&gt; GetColorList()&lt;br /&gt;{&lt;br /&gt;  return from primaryColor in primaryColors&lt;br /&gt;         from secondaryColor in secondaryColors&lt;br /&gt;         select new { PrimaryColor = primaryColor, SecondaryColor = secondaryColor };&lt;br /&gt;}&lt;br /&gt;&lt;/dynamic&gt;&lt;/pre&gt;&lt;br /&gt;Here I can access all of the properties on the list with the use of dynamic for the anonymous type.  Of course this will not be as fast as static code, returning an enumeration of strongly typed objects but it is a good illustration of dynamics in action.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1535807340352183565-513587150465572297?l=dailyandersen.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dailyandersen.blogspot.com/feeds/513587150465572297/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://dailyandersen.blogspot.com/2010/05/i-have-been-trying-to-find-occassion-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/513587150465572297'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1535807340352183565/posts/default/513587150465572297'/><link rel='alternate' type='text/html' href='http://dailyandersen.blogspot.com/2010/05/i-have-been-trying-to-find-occassion-in.html' title='.NET 4.0 Dynamics and Linq Anonymous Types'/><author><name>Bruce</name><uri>http://www.blogger.com/profile/09355720423868876069</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
