Created
September 15, 2018 07:03
-
-
Save danielplawgo/2e3d414fd3f28c34f217bcca494df6de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Category : BaseModel | |
{ | |
public string Name { get; set; } | |
public virtual ICollection<Product> Products { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CategoryProfile : Profile | |
{ | |
public CategoryProfile() | |
{ | |
CreateMap<Category, CategoryViewModel>(); | |
CreateMap<Product, ProductViewModel>(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CategoryViewModel | |
{ | |
public string Name { get; set; } | |
public IEnumerable<ProductViewModel> Products { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Product : BaseModel | |
{ | |
public string Name { get; set; } | |
public int CategoryId { get; set; } | |
public virtual Category Category { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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