Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit f5a4e72

Browse files
author
Morten Turn Pedersen
authored
Merge pull request #1 from nodes-dotnet/feature/initial
Feature/initial
2 parents 3b29b9a + 058e7c1 commit f5a4e72

File tree

9 files changed

+270
-0
lines changed

9 files changed

+270
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
parameters:
2+
- name: projects
3+
type: string
4+
default: '**/*.csproj'
5+
6+
- name: buildConfiguration
7+
type: string
8+
default: 'Release'
9+
10+
- name: skipInstall
11+
type: boolean
12+
default: false
13+
14+
- name: version
15+
type: string
16+
default: '3.1.x'
17+
18+
steps:
19+
- template: 'Install Net Core.yml'
20+
parameters:
21+
skip: '${{parameters.skipInstall}}'
22+
version: '${{parameters.version}}'
23+
24+
- task: DotNetCoreCLI@2
25+
displayName: 'Restore NuGet packages'
26+
inputs:
27+
command: 'restore'
28+
projects: '${{parameters.projects}}'
29+
feedsToUse: 'select'
30+
31+
- task: DotNetCoreCLI@2
32+
displayName: 'Build projects'
33+
inputs:
34+
command: 'build'
35+
projects: '${{parameters.projects}}'
36+
arguments: '--configuration ${{parameters.buildConfiguration}}'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
parameters:
2+
- name: skip
3+
type: boolean
4+
default: false
5+
6+
- name: version
7+
type: string
8+
default: '3.1.x'
9+
10+
steps:
11+
- task: UseDotNet@2
12+
displayName: 'Install .NET Core 3.1'
13+
condition: and(succeeded(), eq('${{parameters.skip}}', false))
14+
inputs:
15+
packageType: 'sdk'
16+
version: '${{parameters.version}}'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
parameters:
2+
- name: projects
3+
type: string
4+
default: '**/*.csproj'
5+
6+
- name: buildConfiguration
7+
type: string
8+
default: 'Release'
9+
10+
- name: skipInstall
11+
type: boolean
12+
default: false
13+
14+
- name: version
15+
type: string
16+
default: '3.1.x'
17+
18+
- name: skipBuild
19+
type: boolean
20+
default: false
21+
22+
steps:
23+
- template: 'Install Net Core.yml'
24+
parameters:
25+
skip: '${{parameters.skipInstall}}'
26+
version: '${{parameters.version}}'
27+
28+
- task: DotNetCoreCLI@2
29+
displayName: 'Pack projects'
30+
inputs:
31+
command: 'pack'
32+
arguments: '--configuration ${{parameters.buildConfiguration}}'
33+
packagesToPack: '${{parameters.projects}}'
34+
nobuild: '${{parameters.skipBuild}}'
35+
versioningScheme: 'off'

Build Configurations/ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Starter pipeline
2+
# Start with a minimal pipeline that you can customize to build and deploy your code.
3+
# Add steps that build, run tests, deploy, and more:
4+
# https://aka.ms/yaml
5+
6+
pr:
7+
- develop
8+
9+
pool:
10+
vmImage: 'ubuntu-latest'
11+
12+
variables:
13+
buildConfiguration: 'Release'
14+
15+
steps:
16+
- template: 'Templates/Build Net Core.yml'
17+
parameters:
18+
buildConfiguration: '$(buildConfiguration)'
19+
20+
- template: 'Templates/Pack Net Core.yml'
21+
parameters:
22+
projects: '**/NetCoreEntityFramework.csproj'
23+
buildConfiguration: '$(buildConfiguration)'
24+
skipBuild: true
25+
skipInstall: true

Build Configurations/publish.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Starter pipeline
2+
# Start with a minimal pipeline that you can customize to build and deploy your code.
3+
# Add steps that build, run tests, deploy, and more:
4+
# https://aka.ms/yaml
5+
6+
trigger:
7+
- master
8+
9+
pool:
10+
vmImage: 'ubuntu-latest'
11+
12+
variables:
13+
buildConfiguration: 'Release'
14+
15+
steps:
16+
- template: 'Templates/Build Net Core.yml'
17+
parameters:
18+
buildConfiguration: '$(buildConfiguration)'
19+
20+
- template: 'Templates/Pack Net Core.yml'
21+
parameters:
22+
projects: '**/NetCoreEntityFramework.csproj'
23+
buildConfiguration: '$(buildConfiguration)'
24+
skipBuild: true
25+
skipInstall: true
26+
27+
- task: NuGetCommand@2
28+
inputs:
29+
command: 'push'
30+
packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
31+
nuGetFeedType: 'external'
32+
publishFeedCredentials: 'NuGet Feed'

NetCoreEntityFramework.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29905.134
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCoreEntityFramework", "NetCoreEntityFramework\NetCoreEntityFramework.csproj", "{30F23EBE-4571-4215-8D70-F776A71BA51E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{30F23EBE-4571-4215-8D70-F776A71BA51E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{30F23EBE-4571-4215-8D70-F776A71BA51E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{30F23EBE-4571-4215-8D70-F776A71BA51E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{30F23EBE-4571-4215-8D70-F776A71BA51E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {2EAA433C-A97C-4EC8-99FA-B20F49448300}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using System;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.ComponentModel.DataAnnotations.Schema;
5+
6+
namespace Nodes.NetCore.EntityFramework.Models
7+
{
8+
public abstract class EntityBase
9+
{
10+
[Key]
11+
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
12+
public Guid? Id { get; set; }
13+
14+
[Required]
15+
public DateTime Created { get; set; }
16+
17+
[Required]
18+
public DateTime Updated { get; set; }
19+
20+
public DateTime? DeletedAt { get; set; }
21+
22+
public bool Deleted { get; set; }
23+
24+
public void Delete()
25+
{
26+
Deleted = true;
27+
DeletedAt = DateTime.UtcNow;
28+
}
29+
30+
/// <summary>
31+
/// Updates the database correctly
32+
/// </summary>
33+
/// <typeparam name="T">Should be same type as the model class</typeparam>
34+
/// <param name="table">The database table to delete the instance from</param>
35+
public void Delete<T>(DbSet<T> table) where T : EntityBase
36+
{
37+
if (typeof(T).FullName != GetType().FullName)
38+
{
39+
throw new ArgumentException($"Tried updating a table of type {typeof(T).FullName}. Expected type {GetType().FullName}", nameof(table));
40+
}
41+
42+
Delete();
43+
44+
table.Update(this as T);
45+
}
46+
47+
public void Restore()
48+
{
49+
Deleted = false;
50+
}
51+
}
52+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<PackageId>Nodes.NetCore.EntityFramework.Helpers</PackageId>
6+
<Version>0.1.1</Version>
7+
<Authors>Nodes</Authors>
8+
<Company>Nodes</Company>
9+
<Product>.NET Core Entity Framework Helpers</Product>
10+
<Description>The package contains base classes for entities and repositories</Description>
11+
<AssemblyName>Nodes.NetCore.EntityFramework</AssemblyName>
12+
<RootNamespace>Nodes.NetCore.EntityFramework</RootNamespace>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
17+
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Nodes.NetCore.EntityFramework.Models;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace Nodes.NetCore.EntityFramework.Repositories
7+
{
8+
public abstract class EntityRepository<T, TContext> : IDisposable where T : EntityBase where TContext : DbContext
9+
{
10+
protected DbSet<T> Table { get; private set; }
11+
private TContext Context { get; set; }
12+
13+
protected EntityRepository(TContext context, DbSet<T> table)
14+
{
15+
Context = context;
16+
Table = table;
17+
}
18+
19+
public async Task<T> Get(Guid id)
20+
{
21+
return await Table.FirstOrDefaultAsync(entity => !entity.Deleted && entity.Id == id);
22+
}
23+
24+
public void Dispose()
25+
{
26+
Context.SaveChanges();
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)