Skip to main content
tag
Link
dfhwze
  • 13.9k
  • 3
  • 38
  • 101
added 5 characters in body; edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

I'm create aI've created the unitofwork class below:

I'm create a unitofwork class below

I've created the unitofwork class below:

added 280 characters in body
Source Link
Rod
  • 131
  • 1
  • 7
  public class UnitOfWork : IUnitOfWork
  {
    private AppDbContext _context { get; set; }
    private IDbTransaction _transaction { get; set; }
    private ObjectContext _objectContext { get; set; }
    private bool _isDisposed;
    public UnitOfWork(AppDbContext context)
    {
      _context = context;
      _context.Initialize();
    }
    public DbSet<T> Set<T>() where T : class
    {
      return _context.Set<T>();
    }
    public DbEntityEntry<T> Entry<T>(T entity) where T : class
    {
      return _context.Entry(entity);
    }
    public void Begin()
    {
      _objectContext = ((IObjectContextAdapter)_context).ObjectContext;

      if (_objectContext.Connection.State != ConnectionState.Open)
      {
        _objectContext.Connection.Open();
        if(_transaction != null)
        {
          _transaction.Dispose();
        }
        _transaction = _objectContext.Connection.BeginTransaction();
      }
    }
    public void AutoDetectChangesEnabled(bool option)
    {
      _context.Configuration.AutoDetectChangesEnabled = option;
    }
    public void LazyLoadingEnabled(bool option)
    {
      _context.Configuration.LazyLoadingEnabled = option;
    }
  
    public void SaveChanges()
    {
      _context.SaveChanges();
    }
    public Task SaveChangesAsync()
    {
      return _context.SaveChangesAsync();
    }
    public void Commit()
    {
      _transaction.Commit();
    }
    public void Rollback()
    {
      _transaction.Rollback();
      foreach (var entry in _context.ChangeTracker.Entries())
      {
        switch (entry.State)
        {
          case System.Data.Entity.EntityState.Modified:
            entry.State = System.Data.Entity.EntityState.Unchanged;
            break;
          case System.Data.Entity.EntityState.Added:
            entry.State = System.Data.Entity.EntityState.Detached;
            break;
          case System.Data.Entity.EntityState.Deleted:
            entry.State = System.Data.Entity.EntityState.Unchanged;
            break;
        }
      }
    }
    public void Dispose()
    {
      if (!_isDisposed)
      {
        if (_context != null)
        {
          _context.Database.Connection.Close();
          _context.Dispose();
          _isDisposed_context = true;null;
        }
      }  if(_transaction != null)
      GC  {
          _transaction.SuppressFinalizeDispose(this);
          _transaction = null;
        }
        _isDisposed = true;
      }
    }
  }
}
  public class UnitOfWork : IUnitOfWork
  {
    private AppDbContext _context { get; set; }
    private IDbTransaction _transaction { get; set; }
    private ObjectContext _objectContext { get; set; }
    private bool _isDisposed;
    public UnitOfWork(AppDbContext context)
    {
      _context = context;
      _context.Initialize();
    }
    public DbSet<T> Set<T>() where T : class
    {
      return _context.Set<T>();
    }
    public DbEntityEntry<T> Entry<T>(T entity) where T : class
    {
      return _context.Entry(entity);
    }
    public void Begin()
    {
      _objectContext = ((IObjectContextAdapter)_context).ObjectContext;

      if (_objectContext.Connection.State != ConnectionState.Open)
      {
        _objectContext.Connection.Open();
        _transaction = _objectContext.Connection.BeginTransaction();
      }
    }
    public void AutoDetectChangesEnabled(bool option)
    {
      _context.Configuration.AutoDetectChangesEnabled = option;
    }
    public void LazyLoadingEnabled(bool option)
    {
      _context.Configuration.LazyLoadingEnabled = option;
    }
    public void SaveChanges()
    {
      _context.SaveChanges();
    }
    public Task SaveChangesAsync()
    {
      return _context.SaveChangesAsync();
    }
    public void Commit()
    {
      _transaction.Commit();
    }
    public void Rollback()
    {
      _transaction.Rollback();
      foreach (var entry in _context.ChangeTracker.Entries())
      {
        switch (entry.State)
        {
          case System.Data.Entity.EntityState.Modified:
            entry.State = System.Data.Entity.EntityState.Unchanged;
            break;
          case System.Data.Entity.EntityState.Added:
            entry.State = System.Data.Entity.EntityState.Detached;
            break;
          case System.Data.Entity.EntityState.Deleted:
            entry.State = System.Data.Entity.EntityState.Unchanged;
            break;
        }
      }
    }
    public void Dispose()
    {
      if (!_isDisposed)
      {
        if (_context != null)
        {
          _context.Dispose();
          _isDisposed = true;
        }
      }
      GC.SuppressFinalize(this);
    }
  }
 public class UnitOfWork : IUnitOfWork
  {
    private AppDbContext _context { get; set; }
    private IDbTransaction _transaction { get; set; }
    private ObjectContext _objectContext { get; set; }
    private bool _isDisposed;
    public UnitOfWork(AppDbContext context)
    {
      _context = context;
    }
    public DbSet<T> Set<T>() where T : class
    {
      return _context.Set<T>();
    }
    public DbEntityEntry<T> Entry<T>(T entity) where T : class
    {
      return _context.Entry(entity);
    }
    public void Begin()
    {
      _objectContext = ((IObjectContextAdapter)_context).ObjectContext;

      if (_objectContext.Connection.State != ConnectionState.Open)
      {
        _objectContext.Connection.Open();
        if(_transaction != null)
        {
          _transaction.Dispose();
        }
        _transaction = _objectContext.Connection.BeginTransaction();
      }
    }
    public void AutoDetectChangesEnabled(bool option)
    {
      _context.Configuration.AutoDetectChangesEnabled = option;
    }
    public void LazyLoadingEnabled(bool option)
    {
      _context.Configuration.LazyLoadingEnabled = option;
    }
  
    public void SaveChanges()
    {
      _context.SaveChanges();
    }
    public Task SaveChangesAsync()
    {
      return _context.SaveChangesAsync();
    }
    public void Commit()
    {
      _transaction.Commit();
    }
    public void Rollback()
    {
      _transaction.Rollback();
      foreach (var entry in _context.ChangeTracker.Entries())
      {
        switch (entry.State)
        {
          case System.Data.Entity.EntityState.Modified:
            entry.State = System.Data.Entity.EntityState.Unchanged;
            break;
          case System.Data.Entity.EntityState.Added:
            entry.State = System.Data.Entity.EntityState.Detached;
            break;
          case System.Data.Entity.EntityState.Deleted:
            entry.State = System.Data.Entity.EntityState.Unchanged;
            break;
        }
      }
    }
    public void Dispose()
    {
      if (!_isDisposed)
      {
        if (_context != null)
        {
          _context.Database.Connection.Close();
          _context.Dispose();
          _context = null;
        }
        if(_transaction != null)
        {
          _transaction.Dispose();
          _transaction = null;
        }
        _isDisposed = true;
      }
    }
  }
}
Source Link
Rod
  • 131
  • 1
  • 7
Loading