mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
add EF Generic Repository Pattern
This commit is contained in:
parent
55f07fed08
commit
4d93ecce21
9 changed files with 117 additions and 3 deletions
|
|
@ -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>
|
||||
6
api/FilterLists.Data.Contracts/IListRepository.cs
Normal file
6
api/FilterLists.Data.Contracts/IListRepository.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
namespace FilterLists.Data.Contracts
|
||||
{
|
||||
public interface IListRepository
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FilterLists.Data.Contracts\FilterLists.Data.Contracts.csproj" />
|
||||
<ProjectReference Include="..\FilterLists.Models\FilterLists.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
|||
16
api/FilterLists.Data/IRepository.cs
Normal file
16
api/FilterLists.Data/IRepository.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
55
api/FilterLists.Data/Repository.cs
Normal file
55
api/FilterLists.Data/Repository.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
13
api/FilterLists.Models/BaseEntity.cs
Normal file
13
api/FilterLists.Models/BaseEntity.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue