I've been following some interesting posts about Domain Driven Design. Steve
Maine's has expanded on his initial posts about creating a Repository. His
latest post uses the idea of an Associate class sparked by
Steve Eichert's post to allow for the saving of root
aggregates from within one another. He's implementing is approach in VS2005
using Generics and Partial Classes. This latest flurry of posts got me thinking
about my current architecture for the Virtual Cellar.
As you may recall,
my initial design allowed me to write some code like this
ProducerGateway pg = VirtualCellarManager.GetProducerGateway();
Producer p = new Producer( "Hightower Cellars" );
pg.Add( p );
// Lookup the producer by the ID
Producer p2 = pg.FindByID( p.ID );
WineGateway wg = VirtualCellarManager.GetWineGateway();
Wine w = new Wine( 2001, "Cabernet Sauvignon" );
//Add the wine for the producer
wg.Add( p2, w );
// or I could do something like this
p2.Wines.Add( w ); |
After looking at the posts, I thought to me self, self what would it look
like under this new model
RepositoryFactor rf = new RepositoryFactory();
IRepository r = rf.GetRepository( typeof(Producer) );
Producer p = Producer.Factory.Create( "Hightower", rf );
r.Save(p);
// Lookup the producer by the ID
Producer p2 = r.Load( p.ID );
Wine w = Wine.Factory.Create( 2001, "Cabernet Sauvignon", rf );
p2.Wines.Add( w ); |
This isn't much different codewise, but it will give me some power as later
on I can begin to ask for repositories based on the object's type rather than "hardcoding"
the type. For example
Producer p = Producer.Factory.Create( "Columbia Winery" );
IRepository r = rf.GetRepository( p.GetType() ); |
So, I've begun creating NUnit Tests for my RepositoryFactory class.
Here's what I've come up with
/// <summary>
/// Tests to make sure a ProducerRepository is returned
/// </summary>
[Test]
public void GetRepositoryForProducer()
{
IRepository r = _rf.GetRepository( typeof(Producer) );
Assert.AreEqual( typeof( Lorengo.VirtualCellar.Business.ProducerRepository), r.GetType() );
}
/// <summary>
/// Test to make sure an exception is thrown when a null type is passed
/// </summary>
[Test]
[ExpectedException( typeof(ArgumentNullException) )]
public void GetRepositoryForNull()
{
IRepository r = _rf.GetRepository( null );
}
/// <summary>
/// Tests to make sure an exception is thrown when a invalid type is passed
/// </summary>
[Test]
[ExpectedException( typeof(ArgumentOutOfRangeException) )]
public void GetRepositoryForInvalidType()
{
IRepository r = _rf.GetRepository( typeof(RepositoryFactory) );
}
|
So, I have a question, is the ArgumentOutOfRangeException, the correct
exception to throw in this case? Or is there another Exception that better
matches the fact that no Repository exists for the System.Type being passed
in?