Skip to content

Commit

Permalink
use null object pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterKneale committed Apr 30, 2024
1 parent ca13f23 commit d8d6c01
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/Micro.Common/Domain/OrganisationId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

public record OrganisationId(Guid Value)
{
public static OrganisationId Empty => new(Guid.Empty);
public static OrganisationId Create() => new(Guid.NewGuid());
public static OrganisationId Create(Guid guid) => new(guid);
public static OrganisationId Create(Guid? guid) => guid == null ? Empty : Create(guid.Value);
public static implicit operator Guid(OrganisationId d) => d.Value;
}
2 changes: 2 additions & 0 deletions src/Micro.Common/Domain/ProjectId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

public record ProjectId(Guid Value)
{
public static ProjectId Empty => new(Guid.Empty);
public static ProjectId Create() => new(Guid.NewGuid());
public static ProjectId Create(Guid guid) => new(guid);
public static ProjectId Create(Guid? guid) => guid == null ? Empty : Create(guid.Value);
public static implicit operator Guid(ProjectId d) => d.Value;
}
2 changes: 2 additions & 0 deletions src/Micro.Common/Domain/UserId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

public record UserId(Guid Value)
{
public static UserId Empty => new(Guid.Empty);
public static UserId Create() => new(Guid.NewGuid());
public static UserId Create(Guid guid) => new(guid);
public static UserId Create(Guid? guid) => guid == null ? Empty : Create(guid.Value);
public static implicit operator Guid(UserId d) => d.Value;
}
40 changes: 11 additions & 29 deletions src/Micro.Common/Infrastructure/Context/ExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,26 @@ namespace Micro.Common.Infrastructure.Context;

public class ExecutionContext : IExecutionContext
{
private readonly UserId? _userId;
private readonly OrganisationId? _organisationId;
private readonly ProjectId? _projectId;
private readonly UserId _userId;
private readonly OrganisationId _organisationId;
private readonly ProjectId _projectId;

private ExecutionContext(UserId? userId = null, OrganisationId? organisationId = null, ProjectId? projectId = null)
private ExecutionContext(UserId userId, OrganisationId organisationId, ProjectId projectId)
{
_userId = userId;
_organisationId = organisationId;
_projectId = projectId;
}

private ExecutionContext(Guid? userId = null, Guid? organisationId = null, Guid? projectId = null)
{
if (userId != null)
{
_userId = UserId.Create(userId.Value);
}

if (organisationId != null)
{
_organisationId = OrganisationId.Create(organisationId.Value);
}

if (projectId != null)
{
_projectId = ProjectId.Create(projectId.Value);
}
}

public static ExecutionContext Create(UserId? userId = null, OrganisationId? organisationId = null, ProjectId? projectId = null) =>
new(userId, organisationId, projectId);

public static ExecutionContext Create(Guid? userId = null, Guid? organisationId = null, Guid? projectId = null) =>
new(userId, organisationId, projectId);
new(UserId.Create(userId), OrganisationId.Create(organisationId), ProjectId.Create(projectId));

public UserId UserId => _userId ?? throw new ExecutionContextException("User ID is not set in the execution context");
public UserId UserId =>
_userId != UserId.Empty ? _userId : throw new ExecutionContextException("User ID is not set in the execution context");

public OrganisationId OrganisationId => _organisationId ?? throw new ExecutionContextException("Organisation ID is not set in the execution context");
public OrganisationId OrganisationId =>
_organisationId != OrganisationId.Empty ? _organisationId : throw new ExecutionContextException("Organisation ID is not set in the execution context");

public ProjectId ProjectId => _projectId ?? throw new ExecutionContextException("Project ID is not set in the execution context");
public ProjectId ProjectId =>
_projectId != ProjectId.Empty ? _projectId : throw new ExecutionContextException("Project ID is not set in the execution context");
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public IExecutionContext ExecutionContext
var authContext = new AuthContext(httpContextAccessor);
var pageContext = new PageContextAccessor(httpContextAccessor);

var userId = authContext.IsAuthenticated
? UserId.Create(authContext.UserId)
Guid? userId = authContext.IsAuthenticated
? authContext.UserId
: null;

var organisationId = pageContext.HasOrganisation
? OrganisationId.Create(pageContext.Organisation.Id)
Guid? organisationId = pageContext.HasOrganisation
? pageContext.Organisation.Id
: null;

var projectId = pageContext.HasProject
? ProjectId.Create(pageContext.Project.Id)
Guid? projectId = pageContext.HasProject
? pageContext.Project.Id
: null;

return Create(userId, organisationId, projectId);
Expand Down

0 comments on commit d8d6c01

Please sign in to comment.