Hi,
I have a manager class for each of my entities, and for a particular entity (called Company) I have the following method
public static void MGRAdd(CompanyEntity entityToAdd)
{
try
{
// Create adapter
IDataAccessAdapter myAdapter;
myAdapter = LLBLAdapter.GetNew();
// Create unit of work
UnitOfWork2 unitOfWork = new UnitOfWork2();
// Save the entity in the transaction
unitOfWork.AddForSave(entityToAdd,true);
// Do post save actions
PostAddActions(entityToAdd, ref unitOfWork);
// Commit transaction
unitOfWork.Commit(myAdapter,true);
}
catch (Exception ex)
{
throw new Exception ("Unable to add new 'Company' to database - " +ex.Message);
}
}
The PostAddActions sub routine then runs other similar methods for other entities, which in turn run similar methods in other entites and so on until I have a nice big transaction of all the things that need to happen when I add a Company Entity.... Fabulous, I love UnitOfWork!!
I pass the unitOfWork as a refernece parameter to my PostAddActions sub routine so that all the additional steps get included in the same transaction.
My questions:
Should I also pass "myAdapter" down to sub routines so that can use the same adapter to perform lookups in the database etc instead of creating potentially many different IDataAccessObjects objects and disposing of them for just one addition to the Company Table?
If I do pass myAdapter as a paremeter should it be a value or reference parameter - I will create a new DataAccessAdapter in the subroutine if 'null' is passed to it?
From other posts I see that I should actually be using code such as
using (IDataAccessAdapter myAdapter = LLBLAdapter.GetNew())
{
//other statments
}
Is this best practice? Can I still pass myAdapter down to sub routines if I do this?
Any advice on this would be gratefully received - I am trying to build a multi user system for potentially hundreds of users and so getting these 'basics' correct is very important to me.
Regards