Castle Active Record custom persistence service

20 views
Skip to first unread message

gd34

unread,
Jul 30, 2009, 6:07:07 AM7/30/09
to NBuilder
Info for anyone using castle active record...

You can easily create a custom NBuilder persistence service like the
one below and register it using the BuilderSetup class.

Once you've done that, you can simply save your active record classes
like:

Builder<Product>.CreateListOfSize(10).Persist();

(Remember to also disable naming for your primary key, otherwise AR
will think they're existing entities)

You could easily do the same thing for NHibernate too.

(p.s. I might release NBuilder.ActiveRecordSupport.dll /
NBuilder.NHibernateSupport.dll / NBuilder.LinqToSqlSupport.dll etc
with these in to make things easier)


BuilderSetup.DisablePropertyNamingFor<Product, int>(x => x.Id);

BuilderSetup.SetPersistenceService(new ActiveRecordPersistenceService
());


public class ActiveRecordPersistenceService : IPersistenceService
{
public void Create<T>(T obj)
{
ActiveRecordMediator.Save(obj);
}

public void Create<T>(IList<T> list)
{
foreach (var instance in list)
{
ActiveRecordMediator.Save(instance);
}
}

public void Update<T>(T obj)
{
ActiveRecordMediator.Update(obj);
}

public void Update<T>(IList<T> list)
{
foreach (var instance in list)
{
ActiveRecordMediator.Update(instance);
}
}

public void SetPersistenceCreateMethod<T>(Action<T>
saveMethod)
{
throw new System.NotImplementedException();
}

public void SetPersistenceUpdateMethod<T>(Action<T>
saveMethod)
{
throw new System.NotImplementedException();
}
}
Reply all
Reply to author
Forward
0 new messages