Hello,
Ive been able to implement a little cool unit of work based on Serenarules one suggested in one thread.
I came up with …
public class UnitOfWork : IUnitOfWork
{
private Database _database;
private IDatabaseFactory _databaseFactory;
private DbTransaction transaction;
public UnitOfWork(IDatabaseFactory databaseFactory)
{
_databaseFactory = databaseFactory;
_database = Database;
transaction = _database.Database.Connection.BeginTransaction();
}
public Database Database
{
get { return _database ?? (_database = _databaseFactory.Get()); }
}
public void Dispose()
{
try
{
_database.SaveChanges();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
}
}
}
I am pretty sure everyone is jealous of this unit of work now. (Kidding)
But i have a little design problem in this service layer.
public class JournalService : IJournalService
{
IJournalRepository _journalRepository;
public JournalService(IJournalRepository journalRepository)
{
_journalRepository = journalRepository;
}
public void AjouterJournal(Journal j)
{
[B]using (IUnitOfWork uow = new UnitOfWork())[/B]
{
var journal = new Journal();
journalRepository.AddJournal(journal);
}
}
}
The problem is the unit of work need a database injection so i cant create an instance of it. I can’t provide an unit of work directly in the service layer because it would make no sense since the unit of work need to be disposable.
And because i use repository to add my stuff there no need to access the unit of work directly, the saving will occur automaticly when it will be disposed anyway.
I could inject the IDatabaseFactory in my service layer but the idea is to not use it there. Actually the service layer shouldnt know about it.
How about an unit of work factory ?
Any ideas or suggestion?