Analytics

Wednesday, September 14, 2011

CQRS @ Develop With Passion Webinars

Thank you to everyone who participated in my webinar about CQRS today. A special thanks to Jean-Paul Boodhoo for inviting me to be a part of his Develop with Passion Webinar series. Unfortunately, I did not have the time to cover as many topics as I would have liked. Maybe we’ll have a “Part 2” webinar in the near future to cover the more advanced topics.

In the mean time, you can dig into the code I went through in the webinar. It is available on GitHub at https://github.com/jsmale/TimeTrackerCQRS. There is some additional code there that I did not have a chance to cover. That includes another set of commands and events (StopTask), and some code for using a persistent event store and view model (as opposed to the in-memory ones we used in the demo) using RavenDB.

There is also a commented out section in the Bootstrapper that can be used re-populate the view model from the event store. This works perfectly if you use the RavenEventStore along with the in-memory PersistentViewModel, but I have so far been unable to get it working with the RavenPersistentViewModel (maybe someone can take a look and submit a pull request?). In any case, this technique allows you to create new view models, or modify existing ones, and have them populated with events that occurred from the beginning of time (or at least since your was released to production). You could even use this technique to keep your view model in-memory for production.

If you have any questions about the code, or CQRS in general, feel free to ping me @jasonsmale on twitter.

Oh, any if you want to review the few slides I had, the PowerPoint is also available on GitHub here.

Thanks!

Friday, July 01, 2011

CQRS Presentation Slides and Code

Thank you to everyone who came out to my presentation last night about Command and Query Responsibility Segregation at the TampaIASA meeting. The PowerPoint slides are available here. The code is on github under the src/samples/CQRS folder.

This was my first public presentation, so any feedback you have would be appreciated. Was I loud and clear enough? Did I explain things well enough? Did I go into details too much or not enough? Let me know on twitter @jasonsmale, or leave a comment on this post.

Also, a special thanks to AgileThought for providing the pizza!

I also want to thank CqrsInfo.com, Greg Young, and Udi Dahan, as much of the materials, ideas, and inspiration for my presentation came from them.

Thursday, April 21, 2011

TFS 2010 Copy Files Custom Activity

I had the need recently to copy specific files from my TFS build output directory to a deployment directory. TFS has a built in activity to copy an entire directory, but no way to limit which files get copied. So, I created a quick implementation of CodeActivity that does just that.

  1. using System;
  2. using System.Activities;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6.  
  7. namespace BuildTasks.Activities
  8. {
  9.     public class CopyFiles : CodeActivity
  10.     {
  11.         [RequiredArgument]
  12.         public InArgument<IEnumerable<string>> SourceFileMasks { get; set; }
  13.  
  14.         [RequiredArgument]
  15.         public InArgument<string> SourceDirectory { get; set; }
  16.  
  17.         [RequiredArgument]
  18.         public InArgument<string> DestinationDirectory { get; set; }
  19.         
  20.         public InArgument<bool> OverwriteFiles { get; set; }
  21.  
  22.         protected override void Execute(CodeActivityContext context)
  23.         {
  24.             var sourceFileMasks = context.GetValue(SourceFileMasks);
  25.             if (sourceFileMasks == null || sourceFileMasks.Count() == 0) return;
  26.  
  27.             var sourceDirectory = new DirectoryInfo(context.GetValue(SourceDirectory));
  28.             if (!sourceDirectory.Exists) return;
  29.  
  30.             var destinationDirectory = context.GetValue(DestinationDirectory);
  31.             if (!Directory.Exists(destinationDirectory)) return;
  32.  
  33.             var overwrite = context.GetValue(OverwriteFiles);
  34.  
  35.             foreach (var sourceFileMask in sourceFileMasks)
  36.             {
  37.                 var sourceFiles = sourceDirectory.GetFiles(sourceFileMask);
  38.                 foreach (var sourceFile in sourceFiles)
  39.                 {
  40.                     sourceFile.CopyTo(Path.Combine(destinationDirectory, sourceFile.Name), overwrite);
  41.                 }
  42.             }
  43.         }
  44.     }
  45. }

The SourceFileMasks property allows you to pass in a list of file name masks that match the files you would like to copy. For example, if you needed to copy YourApp.exe, YourApp.exe.config, YourApp.pdb, Dependency.dll, Dependency.pdb, you could specify that with the following list:

{"YourApp.*", "Dependency.*"}

The screen shot below shows a simplified workflow using the CopyFiles activity to copy files from the build drop folder to a network share at “\\MyServer\Destination”. Also of note is the OverwriteFiles property where you can specify if any existing files should be overwritten.

image

I put together this code rather quickly, so let me know if you see anything overtly incorrect about it. The code is available as a gist at https://gist.github.com/935122.

Sunday, August 15, 2010

Dictionary Adapter for HttpSessionState

For nearly every web application I've written in the past few years, whenever I need to access the HttpSessionState object, I create a wrapper class to wrap the Session with strongly typed properties, so I do not need to deal with the magic string keys and casting the values. Today, I was about to do the same thing, when I realized that I could probably use the DictionaryAdapter from the Castle Project to automatically create an implementation of this class.

If you've never used the DictionaryAdapter before, basically it lets you create a simple interface with only properties with getters and setters, and you can create an implementation of that interface by wrapping anything that implements IDictionary or NameValueCollection. For example, if you had an interface named INeedAnImplentation, you could create the implementation as easily as this:

new DictionaryAdapterFactory().GetAdapter<INeedAnImplentation>(new HashSet());

So, since HttpSessionState acts like a dictionary, I figured I could wrap the session directly. Unfortunately, HttpSessionState does not implement IDictionary or NameValueCollection. No fear, the adapter pattern to the rescue. Just like the DictionaryAdapter can make an IDictionary look like your own interface, I can create an adapter for HttpSessionState to make it look like an IDictionary. Here is that adapter:

public class SessionDictionary : IDictionary<string,object>, IDictionary
{
readonly HttpSessionState sessionState;
readonly CollectionAdapter<string> keysAdapter;
readonly CollectionAdapter<object> valuesAdapter;

public SessionDictionary(HttpSessionState sessionState)
{
this.sessionState = sessionState;
keysAdapter
= new CollectionAdapter<string>(sessionState.Keys);
valuesAdapter
= new CollectionAdapter<object>(sessionState);
}

public bool ContainsKey(string key)
{
return keysAdapter.Contains(key);
}

public void Add(string name, object value)
{
sessionState.Add(name, value);
}

public bool Remove(string name)
{
if (!ContainsKey(name)) return false;
sessionState.Remove(name);
return true;
}

public bool TryGetValue(string key, out object value)
{
if (ContainsKey(key))
{
value
= this[key];
return true;
}

value
= null;
return false;
}

public void Add(KeyValuePair<string, object> item)
{
Add(item.Key, item.Value);
}

public bool Contains(object key)
{
return ContainsKey((string) key);
}

public void Add(object key, object value)
{
Add((
string)key, value);
}

public void Clear()
{
sessionState.Clear();
}

IDictionaryEnumerator IDictionary.GetEnumerator()
{
return new DictionaryEnumerator<string, object>(KeyValueEnumerable().GetEnumerator());
}

public void Remove(object key)
{
Remove((
string) key);
}

object IDictionary.this[object key]
{
get { return this[(string) key]; }
set { this[(string) key] = value; }
}

public bool Contains(KeyValuePair<string, object> item)
{
if (!ContainsKey(item.Key)) return false;
return Equals(this[item.Key], item.Value);
}

public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
CopyTo((Array)array, arrayIndex);
}

public bool Remove(KeyValuePair<string, object> item)
{
if (!Contains(item)) return false;
return Remove(item.Key);
}

IEnumerator
<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
{
return KeyValueEnumerable().GetEnumerator();
}

IEnumerable
<KeyValuePair<string, object>> KeyValueEnumerable()
{
return Keys.Select(key => new KeyValuePair<string, object>(key, this[key]));
}

public IEnumerator GetEnumerator()
{
return sessionState.GetEnumerator();
}

public object this[string name]
{
get { return sessionState[name]; }
set { sessionState[name] = value; }
}

public void CopyTo(Array array, int index)
{
sessionState.CopyTo(array, index);
}

public int Count
{
get { return sessionState.Count; }
}

public object SyncRoot
{
get { return sessionState.SyncRoot; }
}

public bool IsSynchronized
{
get { return sessionState.IsSynchronized; }
}

public ICollection<string> Keys
{
get { return keysAdapter; }
}

ICollection IDictionary.Values
{
get { return valuesAdapter; }
}

ICollection IDictionary.Keys
{
get { return keysAdapter; }
}

public ICollection<object> Values
{
get { return valuesAdapter; }
}

public bool IsReadOnly
{
get { return sessionState.IsReadOnly; }
}

public bool IsFixedSize
{
get { return false; }
}
}


While implementing that adapter, I realized I needed two more adapters. One to expose the keys and values as ICollection implementations, and another to implement the IDictionaryEnumerator interface. Here are those implementations:


public class CollectionAdapter<T> : ICollection<T>, ICollection
{
readonly ICollection collection;

public void CopyTo(Array array, int index)
{
collection.CopyTo(array, index);
}

public object SyncRoot
{
get { return collection.SyncRoot; }
}

public bool IsSynchronized
{
get { return collection.IsSynchronized; }
}

public CollectionAdapter(ICollection collection)
{
this.collection = collection;
}

IEnumerator
<T> IEnumerable<T>.GetEnumerator()
{
return collection.Cast<T>().GetEnumerator();
}

public IEnumerator GetEnumerator()
{
return collection.GetEnumerator();
}

public void Add(T item)
{
throw new NotSupportedException();
}

public void Clear()
{
throw new NotSupportedException();
}

public bool Contains(T item)
{
return collection.Cast<T>().Any(x => Equals(x, item));
}

public void CopyTo(T[] array, int arrayIndex)
{
collection.CopyTo(array, arrayIndex);
}

public bool Remove(T item)
{
throw new NotSupportedException();
}

public int Count
{
get { return collection.Count; }
}

public bool IsReadOnly
{
get { return true; }
}
}

public class DictionaryEnumerator<TKey,TValue> : IDictionaryEnumerator, IDisposable
{
readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator;

public DictionaryEnumerator(IEnumerator<KeyValuePair<TKey,TValue>> enumerator)
{
this.enumerator = enumerator;
}

public void Dispose()
{
enumerator.Dispose();
}

public bool MoveNext()
{
return enumerator.MoveNext();
}

public void Reset()
{
enumerator.Reset();
}

public object Current
{
get { return enumerator.Current; }
}

public object Key
{
get { return enumerator.Current.Key; }
}

public object Value
{
get { return enumerator.Current.Value; }
}

public DictionaryEntry Entry
{
get { return new DictionaryEntry(Key, Value); }
}
}

Once that was complete, I could now use the Castle DictionaryAdapter to create my interface. I only wanted to create it once, so I registered my interface with StructureMap so it would automatically create an instance for each HttpContext:


public class WebRegistry : Registry
{
public WebRegistry()
{
var dictionaryAdapterFactory
= new DictionaryAdapterFactory();
For
<IWebSession>()
.HttpContextScoped()
.Use(()
=> dictionaryAdapterFactory.GetAdapter<IWebSession>(new SessionDictionary(HttpContext.Current.Session)));
}
}


Where the IWebSession is defined like so:


public interface IWebSession
{
SearchRequest LastSearchRequest {
get; set; }
}

Then to access the session, I only need to make it a dependency on any class that needs to use it. For example, to access it in my FubuMVC controller, I add the IWebSession to the constructor arguments. Then I can set and get the session variables by using the properties on the IWebSession instance:


public class SearchController
{
readonly ISearchService searchService;
readonly IWebSession webSession;

public SearchController(ISearchService searchService, IWebSession webSession)
{
this.searchService = searchService;
this.webSession = webSession;
}

public SearchResponse SearchResults(SearchInput searchInput)
{
var searchRequest
= webSession.LastSearchRequest
?? new SearchRequest();
return searchService(searchRequest, searchInput.Page);
}

public FubuContinuation SearchSuppliers(SearchRequest searchRequest)
{
webSession.LastSearchRequest
= searchRequest;
return FubuContinuation.RedirectTo(new SearchInput{Page = 1});
}
}

The full source code for these files is available as a Gist on GitHub.com:
http://gist.github.com/524832

Please let me know if you find this useful, or have any suggestions for improving this implementation.