Much of what I’m going to show today has been borrowed from Alex Henderson on the storage facility.
This post continues the hosting base4 inside your web application post
To integrate base4 in castle you can register either components or create a facility which allows it to hook into some bits of the castle life-cycle.
It doesn’t make such a big difference when actually writing code except for the fact that you have transactions managed by castle, caching etc practically for free.
The ObjectTransaction that is available in base4 is built on the transaction scope and uses TransactionOptions.Required. Translated freely this means it will attach itself to an existing transaction if one exists if not it will create one.
So below I’ll first put the code for the storage facility and the base4TransactionManager and the configuration in the config files. The full version of these files and the classes can be found in the source control repository of NBlogr
The storage facility
namespace NBlogr.Common.Base4Integration
{
public class Base4StorageFacility : AbstractFacility
{
protected override void Init()
{
// If no context has been set yet (but should be done in application_start) set the default context.
if (StorageContext.Default == null)
{
string base4Context = FacilityConfig.Attributes["base4Context"];
if (string.IsNullOrEmpty(base4Context))
{
throw new StorageException(“The Base4StorageFacility requires a \”base4Context\” attribute to be set”);
}
StorageContext.SetDefault(base4Context);
}
// Add the transactionmanager to the registered components
Kernel.AddComponent(“base4.transactionManager”, typeof(ITransactionManager), typeof(Base4TransactionManager));
// Add the IItemcontext to the registered components
Kernel.AddComponentInstance(“base4.defaultContext”, typeof(IItemContext), StorageContext.Default);
// Add the base4Dataobject
Kernel.AddComponent(“base4.dataObject”, typeof(IDataObject<>), typeof(BaseDataObject<>));
}
}
}
The transaction manager
namespace NBlogr.Common.Base4Integration
{
[PerThread]
public class Base4TransactionManager : DefaultTransactionManager
{
}
}
The IDataObject interface
public interface IDataObject
where T : class, IItem, new()
{
void Delete(T item);
Base4.Storage.IItemList
Base4.Storage.IItemList
Base4.Storage.IItemList
Base4.Storage.IItemList
Base4.Storage.IItemList
Base4.Storage.IItemList
Base4.Storage.IItemList
Base4.Storage.IItemList
Base4.Storage.IItemList
Base4.Storage.IItemList
T GetById(Guid Id);
T GetOne(string oPath, params object[] parameters);
T GetOne(string oPath);
T GetOne(ObjectPath oPath);
T GetOneUsingSQL(string SQL);
T GetOneUsingSQL(string SQL, ObjectScope scope);
T GetOne(string opath, ObjectScope scope);
T GetOne(ObjectPath path, ObjectScope scope);
T Save(T item);
string SortExpression { get; set; }
void DeleteAll();
void Delete(ObjectPath path);
void Delete(string path);
void Delete(string path, params string[] replaces);
IItemList
IItemList
IItemList
IItemList
IItemList
}
The facilities.config file
<configuration>
<facilities>

