Commit 756a1d87 authored by boncho vylkov's avatar boncho vylkov

create new branch for Uow

parent 242399b9
using MvcCoreTemplate.Data.Common.Repositories;
using MvcCoreTemplate.Data.Models;
using System.Threading.Tasks;
namespace MvcCoreTemplate.Data.UnitOfWork
{
public interface IUowData
{
IDeletableEntityRepository<Feature> Features { get; }
Task<int> SaveChangesAsync();
void Dispose();
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using MvcCoreTemplate.Data.Common.Repositories;
using MvcCoreTemplate.Data.Models;
using MvcCoreTemplate.Data.Repositories;
using MvcCoreTemplate.Data.Common.Models;
namespace MvcCoreTemplate.Data.UnitOfWork
{
public class UowData : IUowData
{
private readonly ApplicationDbContext context;
private readonly Dictionary<Type, object> repositories = new Dictionary<Type, object>();
public IDeletableEntityRepository<Feature> Features
{
get { return this.GetRepository<Feature>(); }
}
public UowData(ApplicationDbContext context)
{
this.context = context;
}
private IDeletableEntityRepository<T> GetRepository<T>() where T : BaseModel<int>
{
if (!this.repositories.ContainsKey(typeof(T)))
{
var type = typeof(EfDeletableEntityRepository<T>);
this.repositories.Add(typeof(T), Activator.CreateInstance(type, this.context));
}
return (IDeletableEntityRepository<T>)this.repositories[typeof(T)];
}
public void Dispose()
{
context.Dispose();
}
public async Task<int> SaveChangesAsync()
{
return await context.SaveChangesAsync();
}
}
}
......@@ -8,15 +8,18 @@ using MvcCoreTemplate.Data.Common.Repositories;
using MvcCoreTemplate.Data.Models;
using AutoMapper.QueryableExtensions;
using MvcCoreTemplate.Web.Infrastructure.Models.Features;
using MvcCoreTemplate.Data.UnitOfWork;
namespace MvcCoreTemplate.Web.Controllers
{
public class FeaturesController : BaseController
{
private readonly IDeletableEntityRepository<Feature> repository;
public FeaturesController(IDeletableEntityRepository<Feature> repository,ICommonService commonService) : base(commonService)
private readonly IUowData data;
public FeaturesController(IUowData data,IDeletableEntityRepository<Feature> repository,ICommonService commonService) : base(commonService)
{
this.repository = repository;
this.data = data;
}
public IActionResult Index()
......@@ -29,6 +32,16 @@ namespace MvcCoreTemplate.Web.Controllers
return View(allFeatures);
}
public IActionResult UnitOfWork()
{
var allFeatures = data.Features.All().ProjectTo<FeatureViewModel>().ToList();
if (allFeatures.Count == 0)
{
allFeatures.AddRange(commonService.CreateFeatures());
}
return View("Index",allFeatures);
}
}
}
\ No newline at end of file
......@@ -16,6 +16,7 @@ using MvcCoreTemplate.Data.Seeding;
using MvcCoreTemplate.Data.Common.Repositories;
using MvcCoreTemplate.Data.Repositories;
using MvcCoreTemplate.Web.Infrastructure.Models.Features;
using MvcCoreTemplate.Data.UnitOfWork;
namespace MvcCoreTemplate.Web
{
......@@ -64,6 +65,8 @@ namespace MvcCoreTemplate.Web
services.AddScoped(typeof(IDeletableEntityRepository<>), typeof(EfDeletableEntityRepository<>));
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
services.AddScoped(typeof(IUowData), typeof(UowData));
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
......
......@@ -34,6 +34,7 @@
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-area="" asp-controller="Features" asp-action="Index">Features</a></li>
<li><a asp-area="" asp-controller="Features" asp-action="UnitOfWork">Features by Unit of work</a></li>
</ul>
@await Html.PartialAsync("_LoginPartial")
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment