Skip to content

Commit

Permalink
Adding SharedKernel
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Feb 7, 2021
1 parent b9f83ea commit 7f9d7e6
Show file tree
Hide file tree
Showing 24 changed files with 637 additions and 0 deletions.
39 changes: 39 additions & 0 deletions SharedKernel/SharedKernel.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30914.41
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{D1E74835-F6E4-40C4-A30E-D3AA302F8FEB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PluralsightDdd.SharedKernel", "src\PluralsightDdd.SharedKernel\PluralsightDdd.SharedKernel.csproj", "{1B00F252-C484-4B6B-8D97-446BD59524EC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{60E34973-F9FB-4919-A6EC-58B747BBD3F6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PluralsightDdd.SharedKernel.UnitTests", "tests\PluralsightDdd.SharedKernel.UnitTests\PluralsightDdd.SharedKernel.UnitTests.csproj", "{8B203C8A-A69A-44FD-A687-55934306EFFD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1B00F252-C484-4B6B-8D97-446BD59524EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B00F252-C484-4B6B-8D97-446BD59524EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B00F252-C484-4B6B-8D97-446BD59524EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B00F252-C484-4B6B-8D97-446BD59524EC}.Release|Any CPU.Build.0 = Release|Any CPU
{8B203C8A-A69A-44FD-A687-55934306EFFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8B203C8A-A69A-44FD-A687-55934306EFFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B203C8A-A69A-44FD-A687-55934306EFFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B203C8A-A69A-44FD-A687-55934306EFFD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{1B00F252-C484-4B6B-8D97-446BD59524EC} = {D1E74835-F6E4-40C4-A30E-D3AA302F8FEB}
{8B203C8A-A69A-44FD-A687-55934306EFFD} = {60E34973-F9FB-4919-A6EC-58B747BBD3F6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {852D94AE-2E71-4DC5-9D22-B59BC08C2634}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions SharedKernel/src/PluralsightDdd.SharedKernel/BaseDomainEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using MediatR;
using System;

namespace PluralsightDdd.SharedKernel
{
public abstract class BaseDomainEvent : INotification
{
public DateTime DateOccurred { get; protected set; } = DateTime.UtcNow;
}
}
11 changes: 11 additions & 0 deletions SharedKernel/src/PluralsightDdd.SharedKernel/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace PluralsightDdd.SharedKernel
{
public abstract class BaseEntity<TId>
{
public TId Id { get; set; }

public List<BaseDomainEvent> Events = new List<BaseDomainEvent>();
}
}
66 changes: 66 additions & 0 deletions SharedKernel/src/PluralsightDdd.SharedKernel/DateTimeRange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Ardalis.GuardClauses;
using System;
using System.Collections.Generic;

namespace PluralsightDdd.SharedKernel
{
public class DateTimeRange : ValueObject
{
public DateTime Start { get; private set; }
public DateTime End { get; private set; }

public DateTimeRange(DateTime start, DateTime end)
{
// Ardalis.GuardClauses supports extensions with custom guards per project
Guard.Against.OutOfRange(start, nameof(start), start, end);
Start = start;
End = end;
}

public DateTimeRange(DateTime start, TimeSpan duration) : this(start, start.Add(duration))
{
}

public int DurationInMinutes()
{
return (int)Math.Round((End - Start).TotalMinutes, 0);
}

public DateTimeRange NewDuration(TimeSpan newDuration)
{
return new DateTimeRange(this.Start, newDuration);
}

public DateTimeRange NewEnd(DateTime newEnd)
{
return new DateTimeRange(this.Start, newEnd);
}

public DateTimeRange NewStart(DateTime newStart)
{
return new DateTimeRange(newStart, this.End);
}

public static DateTimeRange CreateOneDayRange(DateTime day)
{
return new DateTimeRange(day, day.AddDays(1));
}

public static DateTimeRange CreateOneWeekRange(DateTime startDay)
{
return new DateTimeRange(startDay, startDay.AddDays(7));
}

public bool Overlaps(DateTimeRange dateTimeRange)
{
return this.Start < dateTimeRange.End &&
this.End > dateTimeRange.Start;
}

protected override IEnumerable<object> GetEqualityComponents()
{
yield return Start;
yield return End;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace PluralsightDdd.SharedKernel.Interfaces
{
// Apply this marker interface only to aggregate root entities
// Repositories will only work with aggregate roots, not their children
public interface IAggregateRoot { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PluralsightDdd.SharedKernel.Interfaces
{
public interface IApplicationEvent
{
string EventType { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace PluralsightDdd.SharedKernel.Interfaces
{
public interface IDomainEvent
{
DateTime DateTimeEventOccurred { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace PluralsightDdd.SharedKernel.Interfaces
{
public interface IHandle<T> where T : BaseDomainEvent
{
Task HandleAsync(T args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Ardalis.Specification;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PluralsightDdd.SharedKernel.Interfaces
{
public interface IRepository
{
Task<T> GetByIdAsync<T, TId>(TId id) where T : BaseEntity<TId>, IAggregateRoot;
Task<List<T>> ListAsync<T, TId>() where T : BaseEntity<TId>, IAggregateRoot;
Task<List<T>> ListAsync<T, TId>(ISpecification<T> spec) where T : BaseEntity<TId>, IAggregateRoot;
Task<T> AddAsync<T, TId>(T entity) where T : BaseEntity<TId>, IAggregateRoot;
Task UpdateAsync<T, TId>(T entity) where T : BaseEntity<TId>, IAggregateRoot;
Task DeleteAsync<T, TId>(T entity) where T : BaseEntity<TId>, IAggregateRoot;
Task<int> CountAsync<T, TId>(ISpecification<T> spec) where T : BaseEntity<TId>, IAggregateRoot;
}
}
21 changes: 21 additions & 0 deletions SharedKernel/src/PluralsightDdd.SharedKernel/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Steve Smith

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<PackageId>PluralsightDdd.SharedKernel</PackageId>
<Title>PluralsightDdd.SharedKernel</Title>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Authors>Steve Smith (@ardalis)</Authors>
<Company>Ardalis.com</Company>
<PackageProjectUrl>https://github.com/ardalis/pluralsight-ddd-fundamentals</PackageProjectUrl>
<Description>An SharedKernal packaged used as a sample in a DDD Pluralsight course.</Description>
<Summary>Includes common abstractions and base types.</Summary>
<RepositoryUrl>https://github.com/ardalis/pluralsight-ddd-fundamentals</RepositoryUrl>
<PackageTags>aspnet asp.net aspnetcore asp.net core ddd dddesign value object entity aggregate domain event</PackageTags>
<PackageReleaseNotes>Initial version.</PackageReleaseNotes>
<Version>1.0.0</Version>
<AssemblyName>PluralsightDdd.SharedKernel</AssemblyName>
<PackageIconUrl>https://user-images.githubusercontent.com/782127/33497760-facf6550-d69c-11e7-94e4-b3856da259a9.png</PackageIconUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<LangVersion>8.0</LangVersion>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ardalis.Specification" Version="4.2.0" />
<PackageReference Include="MediatR" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
</ItemGroup>

</Project>
122 changes: 122 additions & 0 deletions SharedKernel/src/PluralsightDdd.SharedKernel/ValueObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace PluralsightDdd.SharedKernel
{
/// <summary>
/// See: https://enterprisecraftsmanship.com/posts/value-object-better-implementation/
/// </summary>
[Serializable]
public abstract class ValueObject : IComparable, IComparable<ValueObject>
{
private int? _cachedHashCode;

protected abstract IEnumerable<object> GetEqualityComponents();

public override bool Equals(object obj)
{
if (obj == null)
return false;

if (GetUnproxiedType(this) != GetUnproxiedType(obj))
return false;

var valueObject = (ValueObject)obj;

return GetEqualityComponents().SequenceEqual(valueObject.GetEqualityComponents());
}

public override int GetHashCode()
{
if (!_cachedHashCode.HasValue)
{
_cachedHashCode = GetEqualityComponents()
.Aggregate(1, (current, obj) =>
{
unchecked
{
return current * 23 + (obj?.GetHashCode() ?? 0);
}
});
}

return _cachedHashCode.Value;
}

public int CompareTo(object obj)
{
Type thisType = GetUnproxiedType(this);
Type otherType = GetUnproxiedType(obj);

if (thisType != otherType)
return string.Compare(thisType.ToString(), otherType.ToString(), StringComparison.Ordinal);

var other = (ValueObject)obj;

object[] components = GetEqualityComponents().ToArray();
object[] otherComponents = other.GetEqualityComponents().ToArray();

for (int i = 0; i < components.Length; i++)
{
int comparison = CompareComponents(components[i], otherComponents[i]);
if (comparison != 0)
return comparison;
}

return 0;
}

private int CompareComponents(object object1, object object2)
{
if (object1 is null && object2 is null)
return 0;

if (object1 is null)
return -1;

if (object2 is null)
return 1;

if (object1 is IComparable comparable1 && object2 is IComparable comparable2)
return comparable1.CompareTo(comparable2);

return object1.Equals(object2) ? 0 : -1;
}

public int CompareTo(ValueObject other)
{
return CompareTo(other as object);
}

public static bool operator ==(ValueObject a, ValueObject b)
{
if (a is null && b is null)
return true;

if (a is null || b is null)
return false;

return a.Equals(b);
}

public static bool operator !=(ValueObject a, ValueObject b)
{
return !(a == b);
}

internal static Type GetUnproxiedType(object obj)
{
const string EFCoreProxyPrefix = "Castle.Proxies.";
const string NHibernateProxyPostfix = "Proxy";

Type type = obj.GetType();
string typeString = type.ToString();

if (typeString.Contains(EFCoreProxyPrefix) || typeString.EndsWith(NHibernateProxyPostfix))
return type.BaseType;

return type;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using FluentAssertions;
using System;
using Xunit;

namespace PluralsightDdd.SharedKernel.UnitTests.BaseDomainEventTests
{
public class BaseDomainEvent_Constructor
{
public class TestEvent : BaseDomainEvent
{ }

[Fact]
public void SetsTimeToCurrentTime()
{
var newEvent = new TestEvent();

newEvent.DateOccurred.Should().BeCloseTo(DateTime.UtcNow, 100);
}
}
}
Loading

0 comments on commit 7f9d7e6

Please sign in to comment.