December 17, 2008
@ 01:38 PM

Discovered these little gem during the Christmas Potluck today.

And part 2..

 

UPDATE: Wanted to update this item to give credit to Shane Frost for creating these movies. Shane is no longer with Alliance, but after having watched these movies I would welcome him back any time. Hope things are working out well for you at the state Shane.


 
Categories: Local | Random

December 12, 2008
@ 04:25 PM
Writing this today completely baked my noodle:
 public class ClassMapTester
{
protected XmlElement currentElement;
protected XmlDocument document;
protected XmlNamespaceManager mgr;


public ClassMapTester ForString(string input)
{
document = new XmlDocument();
document.LoadXml(input);
return For(document);
}

public ClassMapTester For(XmlDocument doc)
{
document = doc;
SetNameSpaceManager();
return this;
}

public ClassMapTester For() where A : ClassMap, new()
{
For(new A().CreateMapping(new MappingVisitor()));
return this;
}

private void SetNameSpaceManager()
{
mgr = new XmlNamespaceManager(new NameTable());
mgr.AddNamespace("urn", "urn:nhibernate-mapping-2.2");
}

//....
}

Which allow me to write a Fluent-Nhhbernate ClassMap unit test like this:

[Test]
public void LanguageMap_Creates_Valid_Mapping()
{
new ClassMapTester().For()
.TestDefaultConventions()
.TestProperty("LanguageName", "8", "true").Exists()
.TestProperty("LanguageDisplayName", "50", "true").Exists();
}

 

Someone linked the Oxide Channel 9 video yesterday either on Twitter or in IRC. I don't quite remember who. I was intrigued  after watching the video and have been wanting a good excuse to seriously play around with ASP.NET MVC. I have been wanting to hop blog engines from dasBlog for a while now and Oxide appears to have all the features I use. So I downloaded the bits and started looking around.

Oxide is a fairly straight forward blog engine built on MVC. All the standard features are there including RSS, Trackbacks, Comments and Windows Live Writer support via MetaWebLog API. Oxide also uses a provider model for most of its major functionality, so building alternatives is pretty easy as well.

By default the data access layer is build on Linq To Sql, which I found an interesting choice considering the recent death bells tolled for it. In order to make the data provider generic and swapable, the Oxide team chose to create interfaces for their entities.

In the main Oxide.Data namespace are a series of interfaces that look like this:

public interface IArea
{
Guid SiteID { get; set; }
Guid ID { get; set; }
string Name { get; set; }
string DisplayName { get; set; }
string Description { get; set; }
string Type { get; set; }
string TypeUrl { get; set; }
DateTime? Created { get; set; }
DateTime? Modified { get; set; }
}

 

The Linq to Sql entities are created using a typical DBML file. Partial classes are then created for each entity and they are set to inherit from the interface:

 partial class oxite_Area : IArea
{
public Guid ID
{
get { return AreaID; }
set { AreaID = value; }
}

//......
}

This design allow the rest of the application to only deal with the interface leaving the persistence concerns hidden and isolated in the data provider class. This seemed really clean to me, so I wanted to take a stab at implementing a data provider using my favorite ORM technology.

So I fired up a new project and imported NHibernate, Fluent NHibernate and Linq to NHibernate and started hacking out some code. The main entry point to the data provider is the IOxideDataProvider interface. This interface exposes a series of repository interfaces:

public interface IOxiteDataProvider
{
IAreaRepository AreaRepository { get; }
IBackgroundServiceActionRepository BackgroundServiceActionRepository { get; }
ILanguageRepository LanguageRepository { get; }
IMembershipRepository MembershipRepository { get; }
IMessageRepository MessageRepository { get; }
IPostRepository PostRepository { get; }
IResourceRepository ResourceRepository { get; }
ITagRepository TagRepository { get; }
ITrackbackRepository TrackbackRepository { get; }
}

I started by implementing stubs for this interface. I wired my new assembly into the Oxide web.config as the data provider and fired up the site. I wanted to see what was the first repository it would attempt to use. I quickly got a lemon meringue screen crying about AreaRepository.

AreaRepository deals with the IArea entity interface. The Oxide data provider defines this entity partially in DBML and an added partial class. I needed my own implementation of it. This is where the entity interfaces come in handy.

public class Area : AuditedEntity, IArea
{
private string description;
private string displayName;


private string name;
private Guid siteId;
private string type;
private string typeUrl;



public string Name
{
get { return name; }
set { name = value; }
}


//.......

}

Here Area is a simple POCO object that implements IArea. I also pushed up some common properties into a base class set of EntityBase which provides identity via a GUID and a AuditedEntity which provides created and modified date tracking.

Looking through the various repository code, it looked to me like the Oxide team had created  one to one repositories like this to get around limitations of Linq to Sql. I decided to try to create a generic repository to handle all entity types' simple CRUD operations. So I created a typical IRepository interface like so:

 internal interface IRepository
{
IQueryable GetList();
T GetById(long id);
void Save(T entity);
void Delete(T entity);
T GetOne(SpecificationBase query);
IQueryable GetList(SpecificationBase query);
}
internal interface IPagedRepository
{
IQueryable GetPagedList(SpecificationBase query, int pageSize, int pageId);
}

This interface defines very basic Create, Read, Update and Delete functionality for a repository. The Specification stuff is a really cool idea that I blatantly ripped off from RossCode. Check out his article on how that works. It is irrelevant to the topic at hand, so I wont go into it now. I may not even need to use it by the time I am done. I also threw in a interface for paging just in case.

Implementing these interfaces are pretty straight forward and once again this is most likely not my code but something borrowed from somewhere else.

public class Repository
: IRepository, IPagedRepository
{
public Repository(ISession _session)
{
this.session = session;
}


private ISession session { get; set; }



public IQueryable GetPagedList(SpecificationBase query,
int pageSize, int pageId)
{
if (pageId == 0) pageId = 1;
return GetList(query).Take(pageSize).Skip(pageId*pageSize);
}


public IQueryable GetList()
{
return (from entity in _session.Linq() select entity);
}


public T GetById(long id)
{
return _session.Get(id);
}


public void Save(T entity)
{
_session.SaveOrUpdate(entity);
}

public void Delete(T entity)
{
_session.Delete(entity);
_session.Flush();
}


public T GetOne(SpecificationBase query)
{
return query.SatisfyingElementFrom(_session.Linq());
}


public IQueryable GetList(SpecificationBase query)
{
return query.SatisfyingElementsFrom(_session.Linq());
}


}

I can now use my generic repository for all simple CRUD operations required by the interface required repositories. I won't show a full implementation but the basics of it look like this:

 public class AreaRepository : IAreaRepository
{
private readonly IRepository<Area> repository;

public AreaRepository(ISession session)
{
repository = new Repository<Area>(session);
}

private IQueryable<Area> getAreas(Guid siteID)
{
return from a in repository.GetList()
where a.SiteID == siteID
select a;
// orderby a.AreaName
}

//......

}

As you can see the area repository implements IAreaRepository which satisfies the IOxideDataProvider interface. Implementing the rest of the class was pretty much a cut & paste job on the Oxide code to get the linq queries all the methods are based on.

One problem I did run into was that the version of Linq to NHibernate I am using does not appear to support orderby. This might not be the case with the actual trunk version, but the one that is in the Flient NHibernate trunk doesn't. I simply commented the item out for now and will return to it later.

I am currently passing in the NHibernate session and newing up an instance of the generic repository. This isn't the cleanest design but I can push the repository creation up when I get to introducing my IoC container of choice Ninject. I am not quite sure how I am going to do that in Oxide, so I am leaving it for another day.

I now have an end to end solution for the Area entity. All that is left is persistence concerns. I need to create a NHibernate mapping for the Area class to my database. Fluent NHibernate makes this incredibly simple.

public class AreaMap : ClassMap
{
public AreaMap()
{
WithTable("oxite_Area");
Id(x => x.ID,"AreaID").GeneratedBy.Guid().WithUnsavedValue(null);
Map(x => x.Name).TheColumnNameIs("AreaName").WithLengthOf(256).CanNotBeNull();
Map(x => x.DisplayName).WithLengthOf(256).CanNotBeNull();
Map(x => x.Description).WithLengthOf(256).CanNotBeNull();
Map(x => x.Type).WithLengthOf(25).CanNotBeNull();
Map(x => x.TypeUrl).WithLengthOf(25).CanNotBeNull();
Map(x => x.Created).TheColumnNameIs("CreatedDate").CanNotBeNull();
Map(x => x.Modified).TheColumnNameIs("ModifiedDate").CanNotBeNull();
}
}

I can even verify this is right in unit tests by using the very awesome PersistenceSpecification class provided by Fluent NHibernate.

       [Test]
public void Can_Add_Area_To_Database()
{
var testDate = new DateTime(2008, 12, 1);
new PersistenceSpecification(Session)
.CheckProperty(x => x.Name, "Name")
.CheckProperty(x => x.DisplayName, "DisplayName")
.CheckProperty(x => x.Description, "Description")
.CheckProperty(x => x.TypeUrl, "TypeUrl")
.CheckProperty(x => x.Type, "Type")
.CheckProperty(x => x.Created, testDate)
.CheckProperty(x => x.Modified, testDate)
.VerifyTheMappings();
}

Now that is hot. James and the gang have done amazing things with Fluent NHibernate. Next up, I plan to map all the entities in my project in an effort to understand the domain model a bit more and flesh out some unit tests around what I have written.

I have not released the source code for this post yet, as I want to get a working Oxide Data Provider first that way people can actually see it working. So look for more posts on this as I work my way though it.


 
Categories: Development | Oxide

The somewhat monthly meeting of the Seattle ALT.NET Open Spaces Group has been scheduled for December 13th at Microsoft's West Lake office. Please feel free to join us as we discuss a wide variety of topics in the software development and process space.

If you are curious what IoC, Continuous Improvement or NHibernate is all about, this is the meeting to come check out. And if finally no one is interested in those topics we might cover Git, Lean, Kanban or other cutting edge practices that you need to know about.

Where: Microsoft, 320 Westlake Avenue, Seattle

When: Saturday, Dec. 13th, 10am-5pm

Go to the 3rd floor once you arrive, and then you'll see a phone number to call to get in.
 

Categories: Development | Events | Local

Since discovering the ALT.NET community and its dramatic effect on my personal outlook on the way I think about and write code, I have been looking for ways to bring this philosophy to my local developer community. This meeting represents the first major achievement in that goal.

Connecting Camey with Glenn Block and Ade Miller was a stroke of divine intervention. If ever there was a time to come down and check out what the South Sound .NET Users Group is all about this is it.

Please join us on Thursday, December 11th for a presentation on MEF by Glenn Block.

Meeting Summary
Today, it is difficult for applications and frameworks to meet an open-ended set of needs. Building in extensibility allows third-party customization, however there are many challenges in doing so. The Managed Extensibility Framework (MEF) is a new extensibility model in the .NET framework that addresses many of these problems. It provides as simple declarative model for application developers and extenders. Come to this session and get an overview on what it is and what it will do for you.

About Glenn Block
Glenn is a PM for the new Managed Extensibility Framework in .NET 4.0. Prior to Microsoft, he worked for 10 years in various startups and ISVs wearing many different hats all related to developing software. Glenn has been writing code practically since the time he learned how to ride a bicycle. When he's not writing code, he's working on ways to build better software through learning good software design principles and methodologies. Glenn is a geek at heart and spends a good portion of the rest of this time spreading that geekdom through conferences, and the community through groups such as ALT.NET. When he's not working and playing with technology, he spends his time with his wife and four year old daughter either at their Seattle apartment or at one of the local coffee shops.


Meeting Specifics
December 11th, 7 - 9 pm
Olympia Center (222 Columbia NW)


All attendees are eligible for the prize drawings. Past prizes have included technical books, passes to Devscovery, copies of Visual Studio, Vista, Office 2007 and more. Don't forget to let your friends and co-workers know about this meeting.  Feel free to forward this email and/or direct them to www.ssdotnet.org for more information.


 
Categories: Development | Events | Local

December 5, 2008
@ 07:41 AM
using System;
using System.Reflection;

namespace AllianceEnterprises.TestTools
{
public static class ReflectionHelper
{
public static void SetPrivateField(object item, string fieldName, object value)
{
item.GetType()
.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance)
.SetValue(item, value);
}

public static void SetStaticField(string fieldName, object value)
{
typeof (T).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, value);
}
}
}

 
Categories: Blogging | Random | Tools