Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created September 15, 2018 07:03
Show Gist options
  • Save danielplawgo/2e3d414fd3f28c34f217bcca494df6de to your computer and use it in GitHub Desktop.
Save danielplawgo/2e3d414fd3f28c34f217bcca494df6de to your computer and use it in GitHub Desktop.
public class CategoriesController : Controller
{
private Lazy<ICategoryRepository> _categoryRepository;
protected ICategoryRepository CategoryRepository
{
get { return _categoryRepository.Value; }
}
private Lazy<IMapper> _mapper;
protected IMapper Mapper
{
get { return _mapper.Value; }
}
public CategoriesController(Lazy<ICategoryRepository> categoryRepository,
Lazy<IMapper> mapper)
{
_categoryRepository = categoryRepository;
_mapper = mapper;
}
public ActionResult LazyLoading()
{
var categories = CategoryRepository.GetAllActive();
var viewModel = Mapper.Map<IEnumerable<CategoryViewModel>>(categories);
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
}
public class CategoriesController : Controller
{
private Lazy<ICategoryRepository> _categoryRepository;
protected ICategoryRepository CategoryRepository
{
get { return _categoryRepository.Value; }
}
private Lazy<IMapper> _mapper;
protected IMapper Mapper
{
get { return _mapper.Value; }
}
public CategoriesController(Lazy<ICategoryRepository> categoryRepository,
Lazy<IMapper> mapper)
{
_categoryRepository = categoryRepository;
_mapper = mapper;
}
// GET: Categories
public ActionResult Index()
{
var categories = CategoryRepository.GetAllActive(1, 10);
return View(categories);
}
public ActionResult LazyLoading()
{
var categories = CategoryRepository.GetAllActive();
var viewModel = Mapper.Map<IEnumerable<CategoryViewModel>>(categories);
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
public ActionResult EarlyLoading()
{
var categories = CategoryRepository.GetAllActiveWithProducts();
var viewModel = Mapper.Map<IEnumerable<CategoryViewModel>>(categories);
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
}
public class Category : BaseModel
{
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class CategoryProfile : Profile
{
public CategoryProfile()
{
CreateMap<Category, CategoryViewModel>();
CreateMap<Product, ProductViewModel>();
}
}
public partial class CategoryRepository : Repository<Category>, ICategoryRepository
{
public CategoryRepository(Lazy<DataContext> db)
: base(db)
{
}
public override IEnumerable<Category> GetAllActive()
{
return DataContext.Set<Category>()
.Where(e => e.IsActive)
.ToList();
}
}
public partial class CategoryRepository : Repository<Category>, ICategoryRepository
{
public CategoryRepository(Lazy<DataContext> db)
: base(db)
{
}
public override IEnumerable<Category> GetAllActive()
{
return DataContext.Set<Category>()
.Where(e => e.IsActive)
.ToList();
}
public IEnumerable<Category> GetAllActiveWithProducts()
{
return DataContext.Categories.Include(c => c.Products).Where(c => c.IsActive);
}
}
public class CategoryViewModel
{
public string Name { get; set; }
public IEnumerable<ProductViewModel> Products { get; set; }
}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize();
AutofacConfig.Configure();
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
public class Product : BaseModel
{
public string Name { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
}
public partial class ProductRepository : Repository<Product>, IProductRepository
{
public ProductRepository(Lazy<DataContext> db)
: base(db)
{
}
public override IEnumerable<Product> GetAllActive(int page, int pageSize)
{
return DataContext.Set<Product>()
.Where(e => e.IsActive)
.OrderBy(e => e.Id)
.Skip((page - 1) * pageSize)
.Take(pageSize).ToList();
}
}
public class ProductsController : Controller
{
private Lazy<IProductRepository> _productRepository;
protected IProductRepository ProductRepository
{
get { return _productRepository.Value; }
}
private Lazy<IMapper> _mapper;
protected IMapper Mapper
{
get { return _mapper.Value; }
}
public ProductsController(Lazy<IProductRepository> productRepository,
Lazy<IMapper> mapper)
{
_productRepository = productRepository;
_mapper = mapper;
}
// GET: Products
public ActionResult Index()
{
var products = ProductRepository.GetAllActive();
var viewModels = Mapper.Map<IEnumerable<ProductViewModel>>(products);
return Json(viewModels, JsonRequestBehavior.AllowGet);
}
}
public class ProductsController : Controller
{
private Lazy<IProductRepository> _productRepository;
protected IProductRepository ProductRepository
{
get { return _productRepository.Value; }
}
private Lazy<IMapper> _mapper;
protected IMapper Mapper
{
get { return _mapper.Value; }
}
public ProductsController(Lazy<IProductRepository> productRepository,
Lazy<IMapper> mapper)
{
_productRepository = productRepository;
_mapper = mapper;
}
// GET: Products
public ActionResult Index()
{
var products = ProductRepository.GetAllActive();
var viewModels = Mapper.Map<IEnumerable<ProductViewModel>>(products);
return Json(viewModels, JsonRequestBehavior.AllowGet);
}
public ActionResult IndexWithPagination()
{
var products = ProductRepository.GetAllActive(1, 20);
var viewModels = Mapper.Map<IEnumerable<ProductViewModel>>(products);
return Json(viewModels, JsonRequestBehavior.AllowGet);
}
}
public class ProductViewModel
{
public string Name { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment