Subsonic, part deux


I have now finished my database schema, so I played with setting up my data access using SubSonic. Thus far, I am pretty impressed.
 
First, if you need a repository, you can do something like this:

provider = new SqlDataProvider();
provider.DefaultConnectionString =
"{connectionstring here}";
ISubSonicRepository
repository = new SubSonicRepository(provider);

What this gives you is the ability to set up a mock, by deriving from ISubSonicRepository. I like this and will use it rather extensively in my test code. Thus far, I have written most of my tests against the respository. Yeah, I know this is not necessary, but it is one of the best ways to learn how to use it.
 
Here is the code to get a Backend with the id of 1:

Backend

backend = repository.Get<Backend>(1);

And, to add a new one, I simply create and save:

Backend backend = new Backend();
backend.BackendName =
"My Backend";
ISubSonicRepository repository = new SubSonicRepository(provider);
repository.Save(backend);

You gotta love generics. The one thing I am not sure I like is the way collections are implemented, as I will have to mock up each collection for my code instead of the Repository. Here is a load everything for the backend collection (to load a dropdownlist, for example):

BackendCollection coll = new BackendCollection();
coll.Load();

To me, that breaks the entire idea of having a Repository. I will have to think it through before making a firm decision, however. Perhaps this is something left over from the Active Record implementation?
 
Here are a few tests. Realize that these were to learn SubSonic and break many rules for tests I would write for my own stuff. ALso realize these are more Integration tests than Unit tests, as they actually manipulate real data. They are a good start for understanding how the repository works and will help in future posts when I am mocking the Repository. If you are a purist, please cut me some slack until I show some real tests. Then you can skewer me all you want.
 
namespace Microtrak.MultiProgrammer.Data.Tests.VisualStudio
{
 
/// <summary>
  /// Summary description for RepositoryTests
  /// </summary>
  [TestClass]
 
public class RepositoryTests
  {
   
private static SqlDataProvider provider;

    public RepositoryTests()
    {
     
//
      // TODO: Add constructor logic here
      //
    }

    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
     
get
      {
       
return testContextInstance;
      }
     
set
      {
        testContextInstance =
value;
      }
    }

    #region Additional test attributes
   
//
    // You can use the following additional attributes as you write your tests:
    //
    // Use ClassInitialize to run code before running the first test in the class
    [ClassInitialize()]
   
public static void MyClassInitialize(TestContext testContext)
    {
      provider =
new SqlDataProvider();
      provider.DefaultConnectionString =
"server=(local);database=UnitWarehouse;UIS=sa;PWD=pass@word1;";
    }
    

    #endregion

    [TestMethod]
   
public void GetBackendForDallasColo()
    {
     
//expected
      int backendId = 1;
     
string backendName = "Dallas Colo";

      ISubSonicRepository repository = new SubSonicRepository(provider);

      Backend backend = repository.Get<Backend>(1);

      Assert.AreEqual(backendId, backend.BackendId, "Backend Id is different");
     
Assert.AreEqual(backendName, backend.BackendName, "Backend name is different");

    }

    [TestMethod]
   
public void GetAllBackends()
    {
     
BackendCollection coll = new BackendCollection();
      coll.Load();

      Assert.IsTrue(coll.Count == 4, "There are not four items in the collection");
    }

    [TestMethod]
   
public void AddtoCollection()
    {
     
Backend backend = new Backend();
      backend.BackendName =
"My Backend";
     
ISubSonicRepository repository = new SubSonicRepository(provider);
      repository.Save(backend);
    }

  }
}

 
 
Well, that is it for now. I will shared more as I get into my own tests.
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: