Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am creating generic Repositories and generic Unit of work .when i am trying to save record but unfortunately record is not saved and even no error is occur.

What I have tried:

public interface IRepository<TEntity> where TEntity : class,new()
    {
        Task<IReadOnlyList<TEntity>> ListAllAsync();
        Task AddAsync(TEntity entity);
    }


public class Repository<T> : IRepository<T> where T : class, new()
   {
       protected readonly DbContext _dbContext;
       public Repository(DbContext dbContext)
       {
           this._dbContext = dbContext;
       }

       public async Task AddAsync(T entity)
       {
           await _dbContext.Set<T>().AddAsync(entity);
       }

       public async Task<IReadOnlyList<T>> ListAllAsync()
       {
           return await _dbContext.Set<T>().AsNoTracking().ToListAsync();
       }
   }

public interface IUnitOfWork : IDisposable
    {
        Task CompleteAsync();
    }

public class UnitOfWork : IUnitOfWork
    {
        private readonly DbContext _dbContext;
        public UnitOfWork(DbContext dbContext)
        {
            _dbContext = dbContext;
        }
        public async Task CompleteAsync()
        {
           await _dbContext.SaveChangesAsync();
        }

        public void Dispose()
        {
            _dbContext.Dispose();
        }
    }

public class CustomerService : ICustomerService
    {
        private readonly IRepository<Customer> _customerRepository;
        private readonly IUnitOfWork _unitOfWork;
        public CustomerService(IRepository<Customer> customerRepository, IUnitOfWork unitOfWork)
        {
            this._customerRepository = customerRepository;
            this._unitOfWork = unitOfWork;
        }
        public async Task<IEnumerable<CustomerList>> ListAsync()
        {
            var customer = await _customerRepository.ListAllAsync();
            List<CustomerList> customers = new List<CustomerList>();
            foreach (var item in customer)
            {
                customers.Add(new CustomerList { Id = item.Id, Name = item.Name });
            }
            return customers;
        }

        public async Task<CustomerResource> SaveAsync(CustomerResource category)
        {
            
            try
            {
                await _customerRepository.AddAsync(new Customer { Name = category.Name });
                await _unitOfWork.CompleteAsync();
               return new CustomerResource(true, "Successfully");
            }
            catch (Exception ex)
            {

              return  new CustomerResource($"An error occurred when saving the customer :{ex.Message}" );
            }

        }
    }

services.AddScoped<DbContext, ApplicationDbContext>();

services.AddScoped<IUnitOfWork, UnitOfWork>();
            services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
Posted
Updated 26-Feb-19 23:04pm

1 solution

You don't post your ApplicationDbContext, or how your conenction string is provided, but pottentially ApplicationDbContext should extend DbContext, so:

public class ApplicationDbContext: DbContext
    {
        public ApplicationDbContext(DbContextOptions<CrmContext> options) : base(options)
        {
            
        }

// Remaining code here...
    }



And your Unit of Work should receive your ApplicationDbContext:

public class UnitOfWork : IUnitOfWork
    {
        private readonly ApplicationDbContext_dbContext;
        public UnitOfWork(ApplicationDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        public async Task CompleteAsync()
        {
           await _dbContext.SaveChangesAsync();
        }

        public void Dispose()
        {
            _dbContext.Dispose();
        }
    }


And then you can pass your connection string as part of the dependency injection:

services.AddDbContext<ApplicationDbContext>((options => options.UseSqlServer([connection string details go here]));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900