add EF Generic Repository Pattern

This commit is contained in:
Collin M. Barrett 2017-04-08 09:24:05 -05:00
parent 55f07fed08
commit 4d93ecce21
9 changed files with 117 additions and 3 deletions

View file

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\FilterLists.Models\FilterLists.Models.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,6 @@
namespace FilterLists.Data.Contracts
{
public interface IListRepository
{
}
}

View file

@ -9,6 +9,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FilterLists.Data.Contracts\FilterLists.Data.Contracts.csproj" />
<ProjectReference Include="..\FilterLists.Models\FilterLists.Models.csproj" />
</ItemGroup>

View file

@ -0,0 +1,16 @@
//https://code.msdn.microsoft.com/Generic-Repository-Pattern-f133bca4/sourcecode?fileId=164016&pathId=804969589
using System.Collections.Generic;
using FilterLists.Models;
namespace FilterLists.Data
{
public interface IRepository<T> where T : BaseEntity
{
IEnumerable<T> GetAll();
T Get(long id);
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
}
}

View file

@ -1,6 +1,12 @@
namespace FilterLists.Data
using FilterLists.Data.Contracts;
using FilterLists.Models;
namespace FilterLists.Data
{
public class ListRepository
public class ListRepository : Repository<List>, IListRepository
{
public ListRepository(FilterListsContext context) : base(context)
{
}
}
}

View file

@ -0,0 +1,55 @@
//https://code.msdn.microsoft.com/Generic-Repository-Pattern-f133bca4/sourcecode?fileId=164016&pathId=902500354
using System;
using System.Collections.Generic;
using System.Linq;
using FilterLists.Models;
using Microsoft.EntityFrameworkCore;
namespace FilterLists.Data
{
public class Repository<T> : IRepository<T> where T : BaseEntity
{
private readonly FilterListsContext _context;
private readonly DbSet<T> _entities;
public Repository(FilterListsContext context)
{
_context = context;
_entities = context.Set<T>();
}
public IEnumerable<T> GetAll()
{
return _entities.AsEnumerable();
}
public T Get(long id)
{
return _entities.SingleOrDefault(s => s.Id == id);
}
public void Insert(T entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
_entities.Add(entity);
_context.SaveChanges();
}
public void Update(T entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
_context.SaveChanges();
}
public void Delete(T entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
_entities.Remove(entity);
_context.SaveChanges();
}
}
}

View file

@ -0,0 +1,13 @@
//https://code.msdn.microsoft.com/Generic-Repository-Pattern-f133bca4/sourcecode?fileId=164016&pathId=1938870460
using System;
namespace FilterLists.Models
{
public class BaseEntity
{
public long Id { get; set; }
public DateTime AddedDate { get; set; }
public DateTime ModifiedDate { get; set; }
}
}

View file

@ -3,7 +3,7 @@
namespace FilterLists.Models
{
public class List
public class List : BaseEntity
{
[StringLength(1024)] [Column("description")] public string Description;
[StringLength(512)] [Column("description_source_url")] public string DescriptionSourceUrl;

View file

@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FilterLists.Data", "FilterL
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FilterLists.Models", "FilterLists.Models\FilterLists.Models.csproj", "{EBEF0C2E-2631-4AEF-ACAF-F5AE5A568975}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FilterLists.Data.Contracts", "FilterLists.Data.Contracts\FilterLists.Data.Contracts.csproj", "{07A5064E-1CB9-4990-84F8-25A9A89B1D45}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -27,6 +29,10 @@ Global
{EBEF0C2E-2631-4AEF-ACAF-F5AE5A568975}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBEF0C2E-2631-4AEF-ACAF-F5AE5A568975}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBEF0C2E-2631-4AEF-ACAF-F5AE5A568975}.Release|Any CPU.Build.0 = Release|Any CPU
{07A5064E-1CB9-4990-84F8-25A9A89B1D45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07A5064E-1CB9-4990-84F8-25A9A89B1D45}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07A5064E-1CB9-4990-84F8-25A9A89B1D45}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07A5064E-1CB9-4990-84F8-25A9A89B1D45}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE