views:

264

answers:

5

Howdy,

I have a question at SO asking how to wire a middle tier to a DataSet.

I put up an answer showing what I had come up with, but I am not happy with the tight coupling. I have just started to get into testing and find it a good goal for my code.

How would this code be de-coupled to allow for unit testing?

Thank you,
Keith

A: 

If you have entity objects, you can use mocks for unit testing your middle tier.

RWendi

RWendi
A: 

Have you tried Spring.net? It will make your code cleaner and less coupled. It also provides useful classes to do your integration tests.

Pablo Fernandez
+1  A: 

IMO; DataSets are evil. They are, and should only be used as, offline databases. Nothing more, IMO. However, what you do in your Data Access Layer (DAL) should not really impact your Business layer (BL). I'd just use objects (use interfaces) between them (IList) and then use an interface to define your DAL (IRepository) and the nyou can mock that interface to return whatever you need in your BL for unit testing. Unit testing Datasets is another beast, never tried it and I hopefully never have to... Perhaps an in-memory database is your best bet there...

Oh, and for mocking I've used RhinoMock with some success. I'd also encourage you to look at IoCs (http://www.castleproject.org/).

noocyte
I agree. I currently have an application that uses NHibernate, Castle Windsor and RhinoMock and I'm very happy.I use the Repository pattern and have almost total decoupling from the DAL for my unit tests.Never would want to go back to datasets ever again
Tigraine
A: 

It depends on what you want to test:

  • Do you want to test the data retrieval from the database?
  • Building the objects from the datasets?
  • Inserts or updates to the database?
  • And so on...

Here's a suggestion:

An order contains all its children. This is an aggregate, a whole. You get an order with details from a repository:

var order = repository.GetOrderBy(id);

The repository gets the data from the database:

var dataset = orderDatabase.GetOrderAndDetailsBy(id);

The repository could use a builder to create the order:

var order = orderBuilder.CreateOrderAndDetailsFrom(dataset);

You would have to create a repository as follows:

var repository = new OrderRepository(orderDatabase, orderBuilder);

Now you can create a repository with fake collaborators, depending on what you want to test.

Thomas Eyde
A: 

You need IOC (inversion of control) and mock objects.

I encourage you to watch dnrTV episode 126 with James Kovacs.

He demonstrates exactly what you are looking for.

Doug L.