Wednesday, September 19, 2018

Published 2:16 AM by with 0 comment

AspNet Core 2.1- Services/Repostory Pattern

Service /Repository Pattern:

What Is Services\Repository Pattern:

    public class StudentClassService : IRepository<STD_CLASS>, IDisposable
    {

        private readonly _DbContext _DbContext;
        public StudentClassService(_DbContext dbContext)
        {
            _DbContext = dbContext;
        }

        public void Add(STD_CLASS entity)
        {
            _DbContext.STDCLASS.Add(entity);
            _DbContext.SaveChanges();
        }

        public async Task<bool> AddAsync(STD_CLASS entity)
        {
            await _DbContext.STDCLASS.AddAsync(entity);
            await _DbContext.SaveChangesAsync();
            return true;
        }

        public async Task<bool> AddRangeAsync(IEnumerable<STD_CLASS> entity)
        {
            await _DbContext.STDCLASS.AddRangeAsync(entity);
            await _DbContext.SaveChangesAsync();
            return true;

        }

        public void Delete(STD_CLASS entity)
        {
            _DbContext.Entry(entity).State = EntityState.Modified;
            _DbContext.SaveChanges();
        }

        public async Task<bool> DeleteAsync(STD_CLASS entity)
        {
            _DbContext.Entry(entity).State = EntityState.Modified;
            await _DbContext.SaveChangesAsync();
            return true;
        }

        public void DeleteByFlag(STD_CLASS entity)
        {
            throw new NotImplementedException();
        }

        public void DeleteById(int id)
        {
            throw new NotImplementedException();
        }

        public Task<bool> DeleteRange(STD_CLASS entity)
        {
            throw new NotImplementedException();
        }

        public void DeleteRange(IEnumerable<STD_CLASS> entity)
        {
            throw new NotImplementedException();
        }

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

        public STD_CLASS Get(int id)
        {
            return _DbContext.STDCLASS.Where(c => c.Id == id && c.IsDeleted == false).FirstOrDefault();
        }
        public STD_CLASS Get(string Name)
        {
            return _DbContext.STDCLASS.Where(c => c.ClassName.ToLower() == Name.ToLower() && c.IsDeleted == false).FirstOrDefault();
        }

        public IEnumerable<STD_CLASS> GetAll()
        {
            return _DbContext.STDCLASS.Where(c => c.IsDeleted==false).ToList();
        }

        public void Update(STD_CLASS entity)
        {
            _DbContext.Entry(entity).State = EntityState.Modified;
            _DbContext.SaveChanges();

        }

        public async Task<bool> UpdateAsync(STD_CLASS entity)
        {
            _DbContext.Entry(entity).State = EntityState.Modified;
            await _DbContext.SaveChangesAsync();
            return true;
        }
    }

Read More
      edit
Published 2:13 AM by with 0 comment

AspNet Core MVC 2.1-Delete Action Method


In Asp.net Core the Delete Action is work as follows:

You Need To Create  Get or Post Method


Get Action:


        public async Task<IActionResult> Delete(int? id)
        {
            if (id== null)
            {
                return NotFound();
            }
            var s = await _dbContext.STUDENTINFO.AsNoTracking()
                .FirstOrDefaultAsync(m=>m.Id ==id);
            if(s == null)
            {
                return NotFound();
            }
            //if(saveChangesError.GetValueOrDefault)
            return View();
        }

Post Method:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Delete(int? id, IFormCollection collection)
        {
            if (id == null)
            {
                return NotFound();
            }
            var s = await _dbContext.STUDENTINFO.AsNoTracking()
                .FirstOrDefaultAsync(m => m.Id == id);

            if (s == null)
            {
                return NotFound();
            }
            try
            {
                // TODO: Add delete logic here
                //var d= await _dbContext.STUDENTINFO.FindAsync(id);
                s.UpdatedBy = 1;
                s.UpdatedDateTime = DateTime.Now;
                await _studentInfoService.DeleteAsync(s);
                Flag = !Flag;
                return RedirectToAction(nameof(Index));
                
            }
            catch
            {
                return View();
            }
        }

Here you need to create a service pattern or Repository pattern but Its Optional
Remember:Repository Pattern is optional .

Read More
      edit
Published 2:00 AM by with 0 comment

About Us

About Us:
Hi, My name is Muhammad Abdullah. I'm Fulltime Blogger.
Programming Forum is a bloging forum where is share Code,Project,Problem Solution faced by me and other developers.
I also Invite you to write post freely.

I'm full-time Asp.net core Developer
Read More
      edit
Published 1:51 AM by with 0 comment

Privacy

Privacy Policy:
  • The Post you see on this blog are all unique and not copy by another place.
  • Information We take
    We may take information about you for just information purpose or to sent you notifation about the updates
    We may collect your information like( Name,Email ,Country ,Location).
    The Information we collect will not be shared by any Third Person.
  • You can delete your date any time when you want.
Read More
      edit