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

Commit

Permalink
feat(enrolling): use factory to create domain object
Browse files Browse the repository at this point in the history
  • Loading branch information
ratanparai committed Jul 17, 2021
1 parent 0f0dddb commit 4b26049
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 13 deletions.
Expand Up @@ -31,7 +31,7 @@ public sealed class EnrollmentApplicationCommandHandler
throw new ArgumentNullException(nameof(command));
}

var enrollment = new Enrollment(command.Name, command.Email, command.Mobile);
var enrollment = Enrollment.CreateNew(command.Name, command.Email, command.Mobile);
await _context.Enrollments.AddAsync(enrollment, cancellationToken)
.ConfigureAwait(false);
await _context.SaveChangesAsync(cancellationToken)
Expand Down
4 changes: 2 additions & 2 deletions src/Services/Enrolling/Enrolling.API/Enrolling.API.csproj
Expand Up @@ -14,7 +14,7 @@
<PackageReference Include="MediatR" Version="9.0.0"/>
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0"/>
<!-- For validating inputs -->
<PackageReference Include="FluentValidation.AspNetCore" Version="10.2.3"/>
<PackageReference Include="FluentValidation.AspNetCore" Version="10.3.0"/>
<!-- Polly is a .NET resilience and transient-fault-handling library that
allows developers to express policies such as Retry, Circuit Breaker, Timeout,
Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. -->
Expand All @@ -26,7 +26,7 @@
<!-- Swagger -->
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4"/>
<!-- Need this package for generating migration files -->
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.7">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0-preview.6.21352.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Enrolling/Enrolling.API/graphql/Mutation.cs
Expand Up @@ -29,7 +29,7 @@ public class Mutation
throw new ArgumentNullException(nameof(context));
}

var enrollment = new Enrollment(
var enrollment = Enrollment.CreateNew(
input.Name,
input.Email,
input.Mobile);
Expand Down
Expand Up @@ -6,7 +6,7 @@ namespace OpenCodeFoundation.ESchool.Services.Enrolling.Domain.AggregatesModel.E
public class Enrollment
: Entity, IAggregateRoot
{
public Enrollment(
private Enrollment(
string name,
string emailAddress,
string mobileNumber)
Expand All @@ -24,5 +24,13 @@ public class Enrollment
public string EmailAddress { get; private set; }

public string MobileNumber { get; private set; }

public static Enrollment CreateNew(
string name,
string emailAddress,
string mobileNumber)
{
return new(name, emailAddress, mobileNumber);
}
}
}
Expand Up @@ -5,7 +5,7 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.7"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.0-preview.6.21355.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"/>
Expand Down
Expand Up @@ -8,7 +8,7 @@
<ProjectReference Include="..\Enrolling.Domain\Enrolling.Domain.csproj"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.7"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.7"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-preview.6.21352.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-preview.6.21352.1"/>
</ItemGroup>
</Project>
Expand Up @@ -14,7 +14,7 @@ public void NewApplicationShouldSuccessWithValidInput()
.WithDefaults()
.Build();

var enrollment = new Enrollment(dto.Name!, dto.Email!, dto.Mobile!);
var enrollment = Enrollment.CreateNew(dto.Name!, dto.Email!, dto.Mobile!);

Assert.NotNull(enrollment);
Assert.Equal(dto.Name, enrollment.Name);
Expand All @@ -30,18 +30,18 @@ public void ShouldThrowExceptionIfNameIsEmpty()
.WithEmptyName()
.Build();

Assert.Throws<ArgumentNullException>(() => new Enrollment(dto.Name!, dto.Email!, dto.Mobile!));
Assert.Throws<ArgumentNullException>(() => Enrollment.CreateNew(dto.Name!, dto.Email!, dto.Mobile!));
}

[Fact]
public void ShouldThrowExceptionEmtpyEmail()
public void ShouldThrowExceptionEmptyEmail()
{
var dto = new EnrollmentDtoBuilder()
.WithDefaults()
.WithEmail(string.Empty)
.Build();

Assert.Throws<ArgumentNullException>(() => new Enrollment(dto.Name!, dto.Email!, dto.Mobile!));
Assert.Throws<ArgumentNullException>(() => Enrollment.CreateNew(dto.Name!, dto.Email!, dto.Mobile!));
}

[Fact]
Expand All @@ -52,7 +52,7 @@ public void ShouldThrowExceptionEmptyMobile()
.WithMobile(string.Empty)
.Build();

Assert.Throws<ArgumentNullException>(() => new Enrollment(dto.Name!, dto.Email!, dto.Mobile!));
Assert.Throws<ArgumentNullException>(() => Enrollment.CreateNew(dto.Name!, dto.Email!, dto.Mobile!));
}
}
}

0 comments on commit 4b26049

Please sign in to comment.