Similar to how database connections made within the scope of a TransactionScope will behave accordingly, I want to be able to test inside my class whether it is within an instance of another class?
My Scenario
I have a class (EntityBase) that all database objects are built upon, and has methods such as AddToDatabase, UpdateInDatabase, DeleteFromDatabase.
I want to create a BulkLoader class, that will handle inserts in a bulk fashion. This will then have a complete method, like the TransactionScope class, which will be the point that the data is loaded to the database.
This will require changing the EntityBase class so that it behaves differently if it is being instanitated within the scope of a BulkLoader class, and interacting with that class.
Example code:
using (BulkLoader bulk = new BulkLoader(connection, transaction))
{
foreach (IToDatabase assignment in assignmentsCollection)
{
assignment.WriteToDataBase(connection, transaction);
}
bulk.Complete();
}
class ClientAssignment : IToDatabase
{
public int WriteToDataBase(IDbConnection connection,
IDbTransaction transaction)
{
foreach (EntityBase account in accountsCollection)
{
account.AddToDataBase(connection, transaction);
}
foreach (EntityBase person in personsCollection)
{
person.AddToDataBase(connection, transaction);
}
}
}
class EntityBase
{
public virtual int AddToDatabase(IDbConnection connection,
IDbTransaction transaction)
{
// question relates to this bit
if (inBulkLoaderClass)
{
// interact with bulk loader
}
else
{
// do existing code
}
}
}