Skip to content

Commit cd1dbc8

Browse files
author
Matthew Thornton
committed
Add domain exceptions.
1 parent 532b578 commit cd1dbc8

File tree

8 files changed

+151
-23
lines changed

8 files changed

+151
-23
lines changed

Winton.DomainModelling.Abstractions.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{697902B3-26B
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{02075BDC-A66D-4346-8093-2F2667660F36}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Winton.DomainModelling.Abstractions", "src\Winton.DomainModelling.Abstractions\Winton.DomainModelling.Abstractions.csproj", "{4CC49F58-367B-4E87-8925-49D913FA7BD1}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Winton.DomainModelling.Abstractions", "src\Winton.DomainModelling.Abstractions\Winton.DomainModelling.Abstractions.csproj", "{4CC49F58-367B-4E87-8925-49D913FA7BD1}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Winton.DomainModelling.Abstractions.Tests", "test\Winton.DomainModelling.Abstractions.Tests\Winton.DomainModelling.Abstractions.Tests.csproj", "{8FD5A9B0-44ED-470F-A0F0-721E55B5790D}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Winton.DomainModelling.Abstractions.Tests", "test\Winton.DomainModelling.Abstractions.Tests\Winton.DomainModelling.Abstractions.Tests.csproj", "{8FD5A9B0-44ED-470F-A0F0-721E55B5790D}"
1313
EndProject
1414
Global
1515
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace Winton.DomainModelling
4+
{
5+
/// <inheritdoc />
6+
/// <summary>
7+
/// Represents domain errors.
8+
/// </summary>
9+
public class DomainException : Exception
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="DomainException" /> class.
13+
/// </summary>
14+
/// <param name="message">The message that describes the error.</param>
15+
public DomainException(string message)
16+
: base(message)
17+
{
18+
}
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace Winton.DomainModelling
4+
{
5+
/// <inheritdoc />
6+
/// <summary>
7+
/// An error indicating that an entity could not be found.
8+
/// </summary>
9+
/// <typeparam name="TEntity">The type of entity.</typeparam>
10+
/// <typeparam name="TEntityId">The type of entity id.</typeparam>
11+
public class EntityNotFoundException<TEntity, TEntityId> : DomainException
12+
where TEntity : Entity<TEntityId>
13+
where TEntityId : IEquatable<TEntityId>
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="EntityNotFoundException{TEntity, TEntityId}" /> class.
17+
/// </summary>
18+
public EntityNotFoundException()
19+
: base($"The specified {typeof(TEntity).Name} could not be found.")
20+
{
21+
}
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Winton.DomainModelling
2+
{
3+
/// <inheritdoc />
4+
/// <summary>
5+
/// An error indicating that the action being performed is not authorized.
6+
/// </summary>
7+
public class UnauthorizedException : DomainException
8+
{
9+
/// <summary>
10+
/// Initializes a new instance of the <see cref="UnauthorizedException" /> class.
11+
/// </summary>
12+
/// <param name="message">The message that describes the error.</param>
13+
public UnauthorizedException(string message)
14+
: base(message)
15+
{
16+
}
17+
}
18+
}

stylecop.json

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
{
2-
"$schema":
3-
"https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/1.0.2/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
4-
"settings": {
5-
"documentationRules": {
6-
"documentExposedElements": false,
7-
"documentInterfaces": false,
8-
"documentInternalElements": false,
9-
"xmlHeader": false
10-
},
11-
"maintainabilityRules": {
12-
"topLevelTypes": [
13-
"class",
14-
"enum",
15-
"interface",
16-
"struct"
17-
]
18-
},
19-
"orderingRules": {
20-
"blankLinesBetweenUsingGroups": "omit",
21-
"usingDirectivesPlacement": "outsideNamespace"
2+
"$schema":
3+
"https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/1.0.2/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
4+
"settings": {
5+
"documentationRules": {
6+
"documentExposedElements": false,
7+
"documentInterfaces": false,
8+
"documentInternalElements": false,
9+
"xmlHeader": false
10+
},
11+
"maintainabilityRules": {
12+
"topLevelTypes": [
13+
"class",
14+
"enum",
15+
"interface",
16+
"struct"
17+
]
18+
},
19+
"orderingRules": {
20+
"blankLinesBetweenUsingGroups": "omit",
21+
"usingDirectivesPlacement": "outsideNamespace"
22+
}
2223
}
23-
}
2424
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using FluentAssertions;
2+
using Xunit;
3+
4+
namespace Winton.DomainModelling
5+
{
6+
public class DomainExceptionTests
7+
{
8+
public sealed class Message : DomainExceptionTests
9+
{
10+
[Theory]
11+
[InlineData("Test")]
12+
[InlineData("Foo")]
13+
private void ShouldSetMessageFromConstructor(string message)
14+
{
15+
var exception = new DomainException(message);
16+
17+
exception.Message.Should().Be(message);
18+
}
19+
}
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using FluentAssertions;
2+
using Xunit;
3+
4+
namespace Winton.DomainModelling
5+
{
6+
public class EntityNotFoundExceptionTests
7+
{
8+
[Fact]
9+
private void ShouldSetMessageBasedOnTypeOfEntity()
10+
{
11+
var exception = new EntityNotFoundException<TestEntity, int>();
12+
13+
exception.Message.Should().Be("The specified TestEntity could not be found.");
14+
}
15+
16+
// ReSharper disable once ClassNeverInstantiated.Local
17+
private sealed class TestEntity : Entity<int>
18+
{
19+
public TestEntity(int id)
20+
: base(id)
21+
{
22+
}
23+
}
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using FluentAssertions;
2+
using Xunit;
3+
4+
namespace Winton.DomainModelling
5+
{
6+
public class UnauthorizedExceptionTests
7+
{
8+
public sealed class Message : UnauthorizedExceptionTests
9+
{
10+
[Theory]
11+
[InlineData("Test")]
12+
[InlineData("Foo")]
13+
private void ShouldSetMessageFromConstructor(string message)
14+
{
15+
var exception = new UnauthorizedException(message);
16+
17+
exception.Message.Should().Be(message);
18+
}
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)