using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessLayer.Units
{
    /// <summary>
    /// Base unit of work class for db operations
    /// </summary>
    public class UnitOfWorkBase : IDisposable
    {
        public UnitOfWorkBase(ISDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// Save changes to dbcontext
        /// </summary>
        public async Task SaveAsync()
        {
            await _context.SaveChangesAsync();
        }

        private bool disposed = false;

        /// <summary>
        /// Disposes dbcontext
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    _context.Dispose();
                }
            }
            this.disposed = true;
        }

        /// <summary>
        /// Disposes the instancs
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private ISDbContext _context;
    }
}
