Welcome to Habanero Land!   Register  |  Login
You are here:   Blogs
  So, why do you need to register to download Habanero and access the additional materials? 
The success of any open source project is based on the community built around it. So go ahead, register / login and get involved!
Mar
23
Wed
Posted By Andrew on Wednesday, March 23, 2011
479 Views


Anyone with some experience of working with Habanero would probably have started out by using FireStarter or the more recent FireStarter Modeller to generate their business objects and class definitions. FireStarter is indeed a very powerful tool that will allow you to build a domain model and then generate a full solution with source code utilising the power of Habanero with all the appropriate projects (including test projects).

There are however, other ways to create your domain model.  In the project I am currently working on we have opted to use Smooth Habanero to generate the class definitions for our domain objects which have been hand coded.  Smooth Habanero uses a convention over configuration approach to building your class definitions. You can add attributes to properties on your domain objects to apply rules such as marking a property as a unique constraint or setting it's default value.

The downside to using Smooth Habanero is that you don't get a set of auto generated unit tests to test these domain objects (as you would get using FireStarter
Modeller).

As testing is a very important part of how we code at Chillisoft this meant a whole load of hand written tests to cover the business objects.
I got started writing some tests which looked like the following:

        [Test]
        public void Code_HasUniqueConstraint()
        {
            //---------------Set up test pack-------------------
            var material = CreateMaterial();
            var ucCode = material.ClassDef.KeysCol["UC_Code"];
            //---------------Assert Precondition----------------
            Assert.IsNotNull(material);
            Assert.IsNotNull(ucCode);
            //---------------Execute Test ----------------------
            var propDef = ucCode[0];
            //---------------Test Result -----------------------
            Assert.AreEqual("Code", propDef.PropertyName);
        }

As I am fairly new to Habanero I didn't know there was a better option until I was pointed in the direction
of Habanero Testability by a colleague.  This is a library specifically designed for testing Habanero business objects.
Now to write the test above would be as easy as

        [Test]
        public void Code_HasUniqueConstraint2()
        {
            //---------------Set up test pack-------------------
            var boTester = new BOTester();
            //---------------Test Result -----------------------
            boTester.ShouldBeUniqueConstraint(material => material.Code);
        }

Easy to read and easy to write.
Some other methods available on the BOTester class are:

boTester.ShouldBeCompulsory(material => material.Code);
boTester.ShouldHaveAllPropsMapped();
boTester.ShouldHaveDefault(material => material.IsActive, "True");

This is a fairly limited list of what is available. These just happen to be the ones that I needed during my re-factorings whilst writing this article.

Note there is some setup code needed when working with ClassDefs (this is also somewhat different when using Smooth Habanero).
Here is the setup for my test class


    [TestFixture]
    public class TestMaterial
    {
        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            //DO Load class defs
            ClassDef.ClassDefs.Add(ClassDefBroker.GetClassDefs());
            GlobalRegistry.LoggerFactory = new HabaneroLoggerFactoryStub();
        }

        [SetUp]
        public void Setup()
        {
            BORegistry.DataAccessor = new DataAccessorInMemory();

        }

  .....
  
And my ClassDefBroker for Smooth Habanero

    public static class ClassDefBroker
    {
        public static IEnumerable GetClassDefs()
        {
            return typeof (Material).MapClasses();
        }

    }

 
Just to give the complete picture here is what the Material class looks like

    public class Material : BusinessObject, IMaterial
    {
        [AutoMapUniqueConstraint("UC_Code")]
        [AutoMapCompulsory]
        public string Code
        {
            get { return (string)GetPropertyValue("Code"); }
            set { SetPropertyValue("Code", value); }
        }

        public string Description
        {
            get { return (string)GetPropertyValue("Description"); }
            set { SetPropertyValue("Description", value); }
        }

        public int? MaxQuantity
        {
            get { return (int?)GetPropertyValue("MaxQuantity"); }
            set { SetPropertyValue("MaxQuantity", value); }
        }

        [AutoMapCompulsory]
        [AutoMapDefault("True")]
        public bool? IsActive
        {
            get { return (bool?)GetPropertyValue("IsActive"); }
            set { SetPropertyValue("IsActive", value); }
        }

    } 

 
If you would like some further details on Smooth Habanero please let me know in the comments.

Categories

 Top Viewed
 Habanero Cloud
 Archive
 

This website is best viewed in Internet Explorer 7 & 8; Firefox 3.6.11; Opera 10.63; & Safari 4.