I was able to implement a little cool work to work with entity infrastructure.
I figured it out..
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 sure that now everyone is jealous of this part of the work. (Just kidding)
But I have a little design problem at this level of service.
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 that the unit of work needs database injection, so I cannot create an instance of it. I cannot provide the unit of work directly at the service level, because it does not make sense, since the unit of work should be one-time.
And since I use the repository to add my material, there is no need to directly access the unit of work, saving will happen automatically when it is located in any case.
IDatabaseFactory , , . .
UnitOfWork factory?
, ?
.