Thursday, March 15, 2007

Castle + iBatisNet + ASP.Net & MonoRail

1. Entity
Make sure each property has a set/get for the class used as ResultMap

2. Manager use iBatisNet DataMapper
Add reference:
IBatisNet.Common
IBatisNet.DataMapper

3. Business implement automatic transaction
Add reference:
Castle.Services.Transaction
Castle.Facilities.IBatisNetIntegration
Castle.Facilities.AutomaticTransactionManagement

[Transactional]
[UsesAutomaticSessionCreation]
public partial class ProductsBusinessRules : BusinessRulesBase, IProductsBusinessRules
{
...
[Transaction(TransactionMode.Requires)]
public void Save(SearchableSortableBindingList items)
{
...
}
}

4. NUnit
Add reference:
Castle.Core
Castle.Windsor
Castle.MicroKernel
Castle.Services.Transaction
Castle.Facilities.IBatisNetIntegration
Castle.Facilities.AutomaticTransactionManagement
NUnit.Framework

//AbstractBusinessRulesTestCase.cs
public class AbstractBusinessRulesTestCase
{
protected IWindsorContainer container;
public AbstractBusinessRulesTestCase()
{
}
[TestFixtureSetUp]
public void Init()
{
container = new WindsorContainer("configuration.xml");
}

[TestFixtureTearDown]
public void Terminate()
{
container.Dispose();
}
}

//
ProductsBusinessRulesTestCase.cs
[TestFixture]
public class ProductsBusinessRulesTestCase : AbstractBusinessRulesTestCase
{
[Test]
public void RetriveAll()
{
IProductsBusinessRules bizRule = container.Resolve();
SearchableSortableBindingList entities = bizRule.RetrieveAll();
}
}

Project properies setting:
Start external program: nunit-gui.exe
Command line argument: test dll file name

Copy all config file to Debug\Bin folder:
configuration.xml: the replace file for web.config
components.config: components setting for Castle
facilities.config: facilities
setting for Castle
providers.config: providers for iBatisNet DataMapper
SqlMap.config: SqlMap for iBatisNet DataMapper


//
configuration.xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<include uri="file://facilities.config"/>
<include uri="file://components.config"/>
</configuration>

Change SqlMap setting in SqlMap.config accordingly.

5. MonoRail
New "Castle MonoRail Projects"
New ProductsController under Controllers folder

//ProductsController.cs
[Layout("default"), Rescue("generalerror")]
public class ProductsController : SmartDispatcherController
{
private IProductsBusinessRules productBizRules;
public ProductsController(IProductsBusinessRules productBizRules)
{
this.productBizRules = productBizRules;
}
public void Index()
{
PropertyBag["products"] = productBizRules.RetrieveAll();
}
}

New view file under Views folder

//Index.rails, under /Views/Products
#foreach($item in $products)
$item.ProductName
#end

Browse to
http://localhost/shop/products/index.rails