Skip to content
Open
258 changes: 129 additions & 129 deletions samples/UnitOfWork.Host/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Arch.EntityFrameworkCore.UnitOfWork.Host.Controllers
public class ValuesController : Controller
{
private readonly IUnitOfWork _unitOfWork;
private ILogger<ValuesController> _logger;
private readonly ILogger<ValuesController> _logger;

// 1. IRepositoryFactory used for readonly scenario;
// 2. IUnitOfWork used for read/write scenario;
Expand All @@ -27,135 +27,137 @@ public ValuesController(IUnitOfWork unitOfWork, ILogger<ValuesController> logger
var repo = _unitOfWork.GetRepository<Blog>(hasCustomRepository: true);
if (repo.Count() == 0)
{
repo.Insert(new Blog
{
Id = 1,
Url = "/a/" + 1,
Title = $"a{1}",
Posts = new List<Post>{
new Post
{
Id = 1,
Title = "A",
Content = "A's content",
Comments = new List<Comment>
{
new Comment
{
Id = 1,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 2,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 3,
Title = "c",
Content = "c's content",
}
},
},
new Post
{
Id = 2,
Title = "B",
Content = "B's content",
Comments = new List<Comment>
{
new Comment
{
Id = 4,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 5,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 6,
Title = "c",
Content = "c's content",
}
},
},
new Post
{
Id = 3,
Title = "C",
Content = "C's content",
Comments = new List<Comment>
{
new Comment
{
Id = 7,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 8,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 9,
Title = "c",
Content = "c's content",
}
},
},
new Post
{
Id = 4,
Title = "D",
Content = "D's content",
Comments = new List<Comment>
{
new Comment
{
Id = 10,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 11,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 12,
Title = "c",
Content = "c's content",
}
},
}
},
});
SeedInitialEntities(repo);
_unitOfWork.SaveChanges();
}
}

private static void SeedInitialEntities(IRepository<Blog> repo)
=> repo.Insert(new Blog
{
Id = 1,
Url = "/a/" + 1,
Title = $"a{1}",
Posts = new List<Post>{
new Post
{
Id = 1,
Title = "A",
Content = "A's content",
Comments = new List<Comment>
{
new Comment
{
Id = 1,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 2,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 3,
Title = "c",
Content = "c's content",
}
},
},
new Post
{
Id = 2,
Title = "B",
Content = "B's content",
Comments = new List<Comment>
{
new Comment
{
Id = 4,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 5,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 6,
Title = "c",
Content = "c's content",
}
},
},
new Post
{
Id = 3,
Title = "C",
Content = "C's content",
Comments = new List<Comment>
{
new Comment
{
Id = 7,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 8,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 9,
Title = "c",
Content = "c's content",
}
},
},
new Post
{
Id = 4,
Title = "D",
Content = "D's content",
Comments = new List<Comment>
{
new Comment
{
Id = 10,
Title = "A",
Content = "A's content",
},
new Comment
{
Id = 11,
Title = "b",
Content = "b's content",
},
new Comment
{
Id = 12,
Title = "c",
Content = "c's content",
}
},
}
},
});

// GET api/values
[HttpGet]
public async Task<IList<Blog>> Get()
{
return await _unitOfWork.GetRepository<Blog>().GetAllAsync(include: source => source.Include(blog => blog.Posts).ThenInclude(post => post.Comments));
}
public async Task<IList<Blog>> Get()
=> await _unitOfWork.GetRepository<Blog>()
.GetAllAsync(include: source => source.Include(blog => blog.Posts).ThenInclude(post => post.Comments));

// GET api/values/Page/5/10
[HttpGet("Page/{pageIndex}/{pageSize}")]
[HttpGet("Page/{pageIndex:int}/{pageSize:int}")]
public async Task<IPagedList<Blog>> Get(int pageIndex, int pageSize)
{
// projection
Expand All @@ -170,11 +172,11 @@ public async Task<IPagedList<Blog>> Get(string term)
{
_logger.LogInformation("demo about first or default with include");

var item = _unitOfWork.GetRepository<Blog>().GetFirstOrDefault(predicate: x => x.Title.Contains(term), include: source => source.Include(blog => blog.Posts).ThenInclude(post => post.Comments));
var item = await _unitOfWork.GetRepository<Blog>().GetFirstOrDefaultAsync(predicate: x => x.Title.Contains(term), include: source => source.Include(blog => blog.Posts).ThenInclude(post => post.Comments));

_logger.LogInformation("demo about first or default without include");

item = _unitOfWork.GetRepository<Blog>().GetFirstOrDefault(predicate: x => x.Title.Contains(term), orderBy: source => source.OrderByDescending(b => b.Id));
item = await _unitOfWork.GetRepository<Blog>().GetFirstOrDefaultAsync(predicate: x => x.Title.Contains(term), orderBy: source => source.OrderByDescending(b => b.Id));

_logger.LogInformation("demo about first or default with projection");

Expand All @@ -184,11 +186,9 @@ public async Task<IPagedList<Blog>> Get(string term)
}

// GET api/values/4
[HttpGet("{id}")]
public async Task<Blog> Get(int id)
{
return await _unitOfWork.GetRepository<Blog>().FindAsync(id);
}
[HttpGet("{id:int}")]
public async Task<Blog> Get(int id)
=> await _unitOfWork.GetRepository<Blog>().FindAsync(id);

// POST api/values
[HttpPost]
Expand Down
5 changes: 1 addition & 4 deletions samples/UnitOfWork.Host/Models/BlogggingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public BloggingContext(DbContextOptions<BloggingContext> options)
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.EnableAutoHistory(null);
}
protected override void OnModelCreating(ModelBuilder modelBuilder) => modelBuilder.EnableAutoHistory(null);
}

public class Blog
Expand Down
9 changes: 2 additions & 7 deletions samples/UnitOfWork.Host/Models/CustomBlogRepository.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using Arch.EntityFrameworkCore.UnitOfWork;
using Arch.EntityFrameworkCore.UnitOfWork.Host.Models;
using Microsoft.EntityFrameworkCore;

namespace Host.Models
namespace Arch.EntityFrameworkCore.UnitOfWork.Host.Models
{
public class CustomBlogRepository : Repository<Blog>, IRepository<Blog>
public class CustomBlogRepository : Repository<Blog>
{
public CustomBlogRepository(BloggingContext dbContext) : base(dbContext)
{

}
}
}
1 change: 0 additions & 1 deletion samples/UnitOfWork.Host/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;
using Host.Models;

namespace Arch.EntityFrameworkCore.UnitOfWork.Host
{
Expand Down
23 changes: 13 additions & 10 deletions samples/UnitOfWork.Host/UnitOfWork.Host.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Arch.EntityFrameworkCore.UnitOfWork.Host</RootNamespace>
<PreserveCompilationContext>true</PreserveCompilationContext>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.8" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\UnitOfWork\UnitOfWork.csproj" />
Expand Down
18 changes: 0 additions & 18 deletions src/Microsoft.EntityFrameworkCore.UnitOfWork/IRepositoryFactory.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Arch.EntityFrameworkCore.UnitOfWork.Collections
/// <summary>
/// Provides some extension methods for <see cref="IEnumerable{T}"/> to provide paging capability.
/// </summary>
public static class IEnumerablePagedListExtensions
public static class EnumerablePagedListExtensions
{
/// <summary>
/// Converts the specified source to <see cref="IPagedList{T}"/> by the specified <paramref name="pageIndex"/> and <paramref name="pageSize"/>.
Expand Down
Loading