0 to 60 with Highway.Data Video Available!

My buddy, Shawn, over at UserGroup.tv as hanging out at the Northwest Arkansas TechFest this year and he videoed on my my talks.  The one on Highway.Data.  Highway.Data is an abstraction layer of Entity Framework (and soon Nhibernate) that  makes working with data access much easier.  It’s built in repository, unit of work and query specification really bring EF, or any ORM for that matter, to a well rounded tool for you tool belt.

This is the second video for me on UserGroup.tv and thanks to Shawn for hosting them. You can check out the videos of other presentations by me and others by going to UserGroup.tv.

Thanks, Shawn!

Enotita: Northwest Arkansas Christian Music Festival

enotita2

Two of my passions is my faith and music.  I just found out there is going to be first annual Christian Music Festival in Northwest Arkansas called Enotita.  The word 'enotita' is taken from the Greek for unity.  There will be a different worship band on stage each hour representing various churches from all over Northwest Arkansas.  There will also be a campfire area for times of peace and reflection.  Backyard Burgers will be selling food from 5-8 PM.

WHAT: Enotita – Northwest Arkansas Christian Music Festival
WHEN:  October 6, 2012 from 4-10 PM
WHERE: 8522 West Wagon Wheel Rd in Springdale, AR [MAP]
Admission: Free

I am praying that they may all be one – just as you, Father, are in me, and I in you, that they too may be in us, so that the world may believe that you sent me.”  John 17:21

You can find our more about the event on facebook (http://facebook.com/enotita2012) and follow them on Twitter (http://twitter.com/enotita2012).  You can also contact Allegra Hanson (enotitanwa@gmail.com), the Event Coordinator, at the Agape Bible Church.

Domain Events Made Easy with JaySmith.DomainEvents

Today I want to present a new library that I have developed to ease the implementation of domain events in my current projects.  This is largely based on Udi Dahan’s blog series on domain events.   This blog will be a walk through of how to add the library to a console application, create a domain object, domain event, domain event handler and raise the event.  My goal is to make adding support for Domain Events to an application easy so you can spend more time working in the problem domain not wasting time wiring up infrastructure.

Steps to getting this working:

  1. Add Domain Events package for StructureMap to application
  2. Create Bootstrapper class to initialize StructureMap
  3. Create a Domain Object
  4. Create Application class to contain our main application logic
  5. Define a Domain Event
  6. Define a Domain Event Handler
  7. Raise the Event

Create New Console Application

Open Visual Studio and create a new Console Application targeting .NET 4.0/4.5.

Add the JaySmith.DomainEvents.StructureMap package via nuget.  This will install all necessary dependencies including StructureMap.  StructureMap is my IoC of choice.  I will be doing a future post on how to use JaySmith.DomainEvents with your IoC of choice.

DomainEvents_InstallPackage

Create Bootstrapper to initialize StructureMap

   1:  public static class Bootstrapper
   2:  {
   3:      public static void Initialize()
   4:      {
   5:          ObjectFactory.Initialize(x => x.Scan(scan =>
   6:              {
   7:                  scan.TheCallingAssembly();
   8:                  scan.WithDefaultConventions();
   9:   
  10:                  scan.AssembliesFromApplicationBaseDirectory();
  11:                  scan.LookForRegistries();
  12:   
  13:                  scan.ConnectImplementationsToTypesClosing(typeof (IDomainEventHandler<>));
  14:              }));
  15:      }
  16:  }

Most StructureMap users will not that for a default configuration lines 7 and 8 are all that are usually required.  Domain Events uses Common Service Locater to do resolution for finding implementations of event handlers and the Domain Events StructureMap package contains a registry that needs to be loaded.  So lines 10 and 11 tell StructureMap to load assemblies in the base directory which is where the JaySmith.DomainEvents.StructureMap.dll will be and then it tells it to look for registries.  This will allow the library to wire up the StructureMapAdapter for Common Service Locator.  You don’t have to understand how this works to utilize the library.  I just wanted to point out why the extra configuration is needed.

Line 13 is also required, it tells StructureMap how to locate instances of IDomainEventHandler<T>.  This will make more sense when we start creating the handler.  I am looking for a way to move this to my StructureMap registry to it doesn’t have to be manually added here.  If anyone knows how to do that drop me a line.

Create a Domain Object

Let go ahead and create a domain object for our application.  We are going to have a Person object that looks like the following:

   1:  public class Person
   2:  {
   3:      public Person(string firstName, string lastName)
   4:      {
   5:          FirstName = firstName;
   6:          LastName = lastName;
   7:      }
   8:   
   9:      public string FirstName { get; set; }
  10:      public string LastName { get; set; }
  11:   
  12:      public string FullName
  13:      {
  14:          get { return string.Format("{0} {1}", FirstName, LastName); }
  15:      }
  16:  }

We need to model the behavior that is interesting to our application.  We are going to be interested in when someone changes their last name.  This can happen for any number of reasons and our system want to be able to handle that.  So let’s add a method to allow us to change the last name.

   1:  public void UpdateLastName(string newLastName)
   2:  {
   3:      LastName = newLastName;
   4:  }

This may seem a little over simplified but we will be adding more code to this method shortly.

Create Application class to contain our main logic

Now that we have a Person object and we want to change the last name of a user in the system we need to write the code to do that.  Here I want to create a class to encapsulate the application logic.

   1:  public class MyApplication
   2:  {
   3:      public void Run()
   4:      {
   5:          var person = new Person("Kristina", "Parker");
   6:          person.UpdateLastName("Smith");
   7:      }
   8:  }

Here we see that we have a person “Kristina Parker” who for whatever reason, in this case she married me Winking smile, changed her name to “Smith”.  Our code reflects this change by calling the UpdateLastName method.  This keeps our domain object up to date, but what if other parts of the system need to do something when a Persons last name changes? 

We could put that logic in the UpdateLastName method but over time this method would get very long and hard to maintain.  It also doesn’t help us keep with SRR (The Single Responsibility Principle).  I would be awesome if we could just raise an event that the last name has changed and those other things just happened.  This is where the domain event comes in. 

Before we do that we need to wire up our MyApplication class in out program main so our application actually executes.

Your Program class should like the following:

   1:  public class Program
   2:  {
   3:      public static void Main(string[] args)
   4:      {
   5:          Bootstrapper.Initialize();
   6:   
   7:          var app = ObjectFactory.GetInstance<MyApplication>();
   8:          app.Run();
   9:   
  10:          Console.Read();
  11:      }
  12:  }

Here initialize our IoC with Bootstrapper.Initialize() and then we get an instance of our application class and then call run.  If we build and execute the application at this point it will execute just fine, the person object will be created and the last name will change.  Now we want to create that domain event so we can notify other parts of the system that something interesting has happened.

Define a Domain Event

The Domain Event is responsible for modeling any information that an Domain Event Handler might need to do its job.  Here is the PersonLastNameChangedEvent:

   1:  public class PersonLastNameChangedEvent : IDomainEvent
   2:  {
   3:      public Person Person { get; set; }
   4:      public string OldValue { get; set; }
   5:      public string NewValue { get; set; }
   6:   
   7:      public PersonLastNameChangedEvent(Person person, string oldValue, string newValue)
   8:      {
   9:          Person = person;
  10:          OldValue = oldValue;
  11:          NewValue = newValue;
  12:      }
  13:  }

The class implements the IDomainEvent interface.  This is necessary for the Domain Events library.  I usually provide an instance of the object that has changed to allow handlers to have full access to any information they may need.  I also like to provide the old and new values of what changed and providing a constructor just eases the creation of the object.   There is no requirement on how you name the class for the event but I recommend using a name that represents what happened in the system.  In this example we used the name of the class the event occurs in and then the name of the event.  This allows you to quickly see where this event actually gets raised; When the LastName is changed on the Person object.

Define a Domain Event Handler

Now that we have have a Domain Event there needs to be something that can handle that event when it occurs. 

   1:  public class PersonLastNameChangedHandler 
   2:              : IDomainEventHandler<PersonLastNameChangedEvent>
   3:  {
   4:      public void Handle(PersonLastNameChangedEvent args)
   5:      {
   6:          var oldLastName = args.OldValue;
   7:          var newLastName = args.NewValue;
   8:          var fullName = args.Person.FullName;
   9:   
  10:          Console.WriteLine("Last Name changed from {0} to {1}",
  11:                  oldLastName, newLastName);
  12:      }
  13:  }

To implement a Domain Event Handler for the PeronLastNameChangedEvent we create a class that implements IDomainEventHandler<PersonLastNameChangedEvent>.  You can have more than one class that handles this event.  IDomainEventHandler has one method that must be implemented named Handle. 

The Handle method is pasted an instance of IDomainEvent that represents the Event that was raised.  In this case it is an instance of PeronsLastChangedEvent.  As you can the handle has full access to any information contained in the DomainEvent class.

In this example we write out a message to the console, your handler will probably do much more interesting things than this.  I hope you are starting to see how powerful this can become.

Raise the Event

We have created a Domain Object, Defined a Domain Event, and created a Domain Event Handler but the event still isn’t firing.  We have add the code to actually raise the event. 

To raise the event when the Persons last name changes we want to modify the Person.UpdateLastName method to to the following:

   1:  public void UpdateLastName(string newLastName)
   2:  {
   3:      var oldLastName = LastName;
   4:      LastName = newLastName;
   5:   
   6:      var lastNameChangedEvent = 
   7:          new PersonLastNameChangedEvent(this, oldLastName, newLastName);
   8:      DomainEventManager.Raise(lastNameChangedEvent);
   9:  }

We have added line 3 to capture the current LastName before we change it.  In line 6 and 7 we instantiate the PersonLastNameChangedEvent providing the required information.  Finally on line 8 we use the DomainEventManager to raise the event.

Executing the code now will raise the event and you will see the message from the Event Handler printed into you console window.

Summary

I hope this walk through gives you a good feel of just how easy it is to add Domain Events to an application.  If you have any questions, critics, or suggestions please do not hesitate to contact me.  If you run into any issues please log them on the project site located on github.  The source code is also available there and I do accept updates from the community.

Clear Internet Explorer Cache Command Line

During a recent project where I was creating a SharePoint feature I had the need to clear the browser cache before starting a new debug session.  The feature was all JavaScript and I wanted to be sure I was getting the latest version.  So, I added this command to the post build event.

InetCpl.cpl,ClearMyTracksByProcess 8

I have also used this in trouble shooting browser issues with clients, the easiest way to know for sure they have cleared their browser cache.

How to Upload a Document with Properties using SharePoint 2010 Client Object Model

Today I came across the need to be able to upload documents to a SharePoint list.  Not so bad, I have done this before, but this time I also need to populate some custom metadata about the document.  After a lot of looking, searching and exceptions I finally found the answer and here it is.

Add References to Microsoft.SharePoint.Client Library

Install-Package Microsoft.SharePoint.Client

Define a form

The important part I always forget is the enctype=”multipart/form-data”.

 1: <form id="formUpload" method="POST" enctype="multipart/form-data ">
 2:     <input type="file" id="fileUpload" name="fileUpload" runat="server"/>
 3:     <input type="submit" value="upload" 
 4:         OnServerClick="btnUploadTheFile_Click" runat="server"/>
 5: </form>

The btnUploadTheFile_Click Handler

 1: private void UpLoadToSharePoint()
 2:  {
 3:     var fileName = Path.GetFileName(fileUpload.PostedFile.FileName);
 4:     var fileStream = new FileStream(fileUpload.PostedFile.FileName, FileMode.Open);
 5:     var byteStream = new byte[fileStream.Length];
 6:     fileStream.Read(byteStream, 0, byteStream.Length);
 7:  
 8:     var serverRelativeUrl = "/MySite/MyDocumentLibrary/" + fileName;
 9:  
 10:     var fileCreationInformation = new FileCreationInformation
 11:     {
 12:         Content = byteStream, 
 13:         Overwrite = true, 
 14:         Url = serverRelativeUrl
 15:     };
 16:  
 17:     var file = _sharePointList.RootFolder.Files.Add(fileCreationInformation);
 18:     file.ListItemAllFields["Column 1"] = "Value 1";
 19:     file.ListItemAllFields["Column 2"] = "Value 2";
 20:     file.ListItemAllFields.Update();
 21:     
 22:     _sharePointContext.ExecuteQuery();
 23: }

Summary

This simple example shows how easy it is to upload a file to a SharePoint document list and provide additional column information.

Calendar

<<  October 2014  >>
MonTueWedThuFriSatSun
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

Month List