Code Monkey home page Code Monkey logo

genericrepositoryunitofwork's Introduction

Generic Repository UnitOfWork

Ther are many repository out there. From many of them meets our requirement or not. So I have created a Complete Repository For any CRUD application.

Prerequisites

This project using MVC Core 2.1 along with visual studio Download it for here

Installing

After creating Project Add DBContext class If you don't know how to add DbContext:How to Cteate Application DbContext

Now flow the steps carefully

  1. Add Application DbContext
  2. Add IRepository.cs
  3. Add Repository.cs
  4. Add IUnitOfWork.cs
  5. Add UnitOfWork.cs

Repository Location

StartUp.cs

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            services.AddScoped(typeof(IUnitOfWork), typeof(UnitOfWork));
Product.cs (Model)
 public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int BrandId { get; set; }
        public Brand Brand { get; set; }
    }
Brand.cs (Model)
 public class Brand
    {
          public int Id { get; set; }
        public string Name { get; set; }

        public List<Product> Products { get; set; }
    }

Initialize in every controller where you making Database Call

private readonly IUnitOfWork _unitOfWork;

)

ProductController.cs
 public class ProductController : Controller
    {
        private readonly IUnitOfWork _unitOfWork;

        public ProductController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public IActionResult BrandIndex()
        {
            var model = _unitOfWork.Repository<Brand>().GetAllInclude(x => x.Products);
            //do something if you want

            return View(model);
        }

        public IActionResult AddBrand()
        {

            return View();
        }

        [HttpPost]
        public IActionResult AddBrand(Brand model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Something wrong");
                return View(model);
            }

            _unitOfWork.Repository<Brand>().Insert(model);
            //save new data to database
            _unitOfWork.Commit();
            return RedirectToAction("BrandIndex");
        }

        public IActionResult AddProduct()
        {
            var model=new Product();
            //brand list for dropdown
            var dbBrand = _unitOfWork.Repository<Brand>().GetAll();

            ViewBag.BrandList = dbBrand.Select(x => new SelectListItem
            {
                Text = x.Name,
                Value = x.Id.ToString()
            });
            return View(model);
        }



        [HttpPost]
        public async Task<IActionResult> AddProduct(Product product)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("","Something wrong");
                return View(product);
            }

            _unitOfWork.Repository<Product>().Insert(product);
            //save asynchronously to database
            await _unitOfWork.CommitAsync();
            return RedirectToAction("ProductList");
        }


        public IActionResult ProductList()
        {
            var model = _unitOfWork.Repository<Product>().GetAllInclude(x => x.Brand);

            return View(model);
        }
    }
Note:
  • Repository should not contain any save method (Personal advice)

genericrepositoryunitofwork's People

Contributors

mubassir-hasan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.