Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor entity instance and categories service #130

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
407 changes: 198 additions & 209 deletions .editorconfig

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions CloudFabric.EAV.Domain/CloudFabric.EAV.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CloudFabric.EventSourcing.Domain" Version="0.2.0" />
<PackageReference Include="CloudFabric.Projections" Version="0.2.0" />
<PackageReference Include="CloudFabric.EventSourcing.Domain" Version="0.2.1" />
<PackageReference Include="CloudFabric.Projections" Version="0.2.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
Expand All @@ -39,4 +40,8 @@
<None Remove="obj-localpackages\**" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
</ItemGroup>

</Project>
30 changes: 0 additions & 30 deletions CloudFabric.EAV.Domain/Events/Instance/CategoryCreated.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace CloudFabric.EAV.Domain.Events.Instance.Entity;

public record EntityCategoryPathChanged : Event
public record EntityInstanceCategoryPathUpdated : Event
{
// ReSharper disable once UnusedMember.Global
// This constructor is required for Event Store to properly deserialize from json
public EntityCategoryPathChanged()
public EntityInstanceCategoryPathUpdated()
{
}

public EntityCategoryPathChanged(Guid id,
public EntityInstanceCategoryPathUpdated(Guid id,
Guid entityConfigurationId,
Guid categoryTreeId,
string categoryPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,35 @@ namespace CloudFabric.EAV.Domain.Events.Instance.Entity;

public record EntityInstanceCreated : Event
{
public string? MachineName { get; set; }

public IReadOnlyCollection<CategoryPath>? CategoryPaths { get; set; }

public Guid EntityConfigurationId { get; set; }

public IReadOnlyCollection<AttributeInstance> Attributes { get; set; }

public Guid? TenantId { get; set; }

// ReSharper disable once UnusedMember.Global
// This constructor is required for Event Store to properly deserialize from json
public EntityInstanceCreated()
{
}

public EntityInstanceCreated(Guid id, Guid entityConfigurationId, List<AttributeInstance> attributes,
Guid? tenantId)
{
TenantId = tenantId;
Attributes = attributes;
EntityConfigurationId = entityConfigurationId;
public EntityInstanceCreated(
Guid id,
Guid entityConfigurationId,
List<AttributeInstance> attributes,
string? machineName,
Guid? tenantId,
List<CategoryPath>? categoryPaths
) {
AggregateId = id;
EntityConfigurationId = entityConfigurationId;
MachineName = machineName;
Attributes = attributes;
TenantId = tenantId;
CategoryPaths = categoryPaths;
}

public Guid EntityConfigurationId { get; set; }
public List<AttributeInstance> Attributes { get; set; }
public Guid? TenantId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CloudFabric.EAV.Domain.Models;
using CloudFabric.EventSourcing.EventStore;

namespace CloudFabric.EAV.Domain.Events.Instance.Entity;

public record EntityInstanceMachineNameUpdated : Event
{
// ReSharper disable once UnusedMember.Global
// This constructor is required for Event Store to properly deserialize from json
public EntityInstanceMachineNameUpdated()
{
}

public EntityInstanceMachineNameUpdated(Guid id, Guid entityConfigurationId, string newMachineName)
{
EntityConfigurationId = entityConfigurationId;
AggregateId = id;
NewMachineName = newMachineName;
}

public Guid EntityConfigurationId { get; set; }

public string NewMachineName { get; set; }
}
11 changes: 0 additions & 11 deletions CloudFabric.EAV.Domain/Models/AttributeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,6 @@ public virtual List<string> Validate()
return errors;
}

public virtual List<string> ValidateInstance(AttributeInstance? instance, bool requiredAttributesCanBeNull = false)
{
if (!IsRequired || requiredAttributesCanBeNull)
{
return new List<string>();
}
return instance?.GetValue() == null ? new List<string> { "Attribute is Required" } : new List<string>();
}

#endregion

public void UpdateName(string newName)
Expand Down Expand Up @@ -186,8 +177,6 @@ private bool Equals(AttributeConfiguration obj)

#endregion



#region EventHandlers

public void On(AttributeConfigurationCreated @event)
Expand Down
31 changes: 31 additions & 0 deletions CloudFabric.EAV.Domain/Models/AttributeInstance.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text.Json.Serialization;

using CloudFabric.EAV.Json.Utilities;
using CloudFabric.EventSourcing.Domain;

namespace CloudFabric.EAV.Domain.Models;

Expand All @@ -10,4 +11,34 @@ public abstract class AttributeInstance
public string ConfigurationAttributeMachineName { get; protected set; }

public abstract object? GetValue();

public virtual Task<List<string>> Validate(
EntityConfiguration entityConfiguration,
AttributeConfiguration attributeConfiguration,
IServiceProvider serviceProvider,
bool requiredAttributesCanBeNull = false
) {
if (!attributeConfiguration.IsRequired || requiredAttributesCanBeNull)
{
return Task.FromResult(new List<string>());
}
return Task.FromResult(GetValue() == null ? new List<string> { "Attribute is Required" } : new List<string>());
}

public virtual Task OnBeforeSave(
Guid entityConfigurationId,
AttributeConfiguration attributeConfiguration,
IServiceProvider serviceProvider
)
{
return Task.CompletedTask;
}

public virtual Task OnSave(
Guid entityConfigurationId,
AttributeConfiguration attributeConfiguration,
IServiceProvider serviceProvider)
{
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,6 @@ public override List<string> Validate()
return errors;
}

public override List<string> ValidateInstance(AttributeInstance? instance, bool requiredAttributesCanBeNull = false)
{
List<string> errors = base.ValidateInstance(instance, requiredAttributesCanBeNull);

if (instance == null)
{
return errors;
}

if (instance is not BooleanAttributeInstance)
{
errors.Add("Cannot validate attribute. Expected attribute type: Boolean");
return errors;
}

return errors;
}

#endregion
public override void UpdateAttribute(AttributeConfiguration updatedAttribute)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,24 @@ public class BooleanAttributeInstance : AttributeInstance
{
return Value;
}

public override async Task<List<string>> Validate(
EntityConfiguration entityConfiguration,
AttributeConfiguration attributeConfiguration,
IServiceProvider serviceProvider,
bool requiredAttributesCanBeNull = false
) {
List<string> errors = await base.Validate(
entityConfiguration, attributeConfiguration,
serviceProvider, requiredAttributesCanBeNull
);

if (attributeConfiguration is not BooleanAttributeConfiguration)
{
errors.Add("Cannot validate attribute. Expected attribute configuration type: Boolean");
return errors;
}

return errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,6 @@ public override List<string> Validate()
return errors;
}

public override List<string> ValidateInstance(AttributeInstance? instance, bool requiredAttributesCanBeNull = false)
{
List<string> errors = base.ValidateInstance(instance, requiredAttributesCanBeNull);

if (instance == null)
{
return errors;
}

if (instance is not DateRangeAttributeInstance dateRangeAttribute)
{
errors.Add("Cannot validate attribute. Expected attribute type: DateRange");
return errors;
}

if (dateRangeAttribute.Value != null
&& (DateRangeAttributeType == DateRangeAttributeType.DateRange && dateRangeAttribute.Value.To == null)
)
{
errors.Add("DateRange attribute value To is missing");
return errors;
}

return errors;
}
#endregion

public override void UpdateAttribute(AttributeConfiguration updatedAttribute)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using CloudFabric.EAV.Enums;

namespace CloudFabric.EAV.Domain.Models.Attributes;

public class DateRangeAttributeInstanceValue
Expand All @@ -14,4 +16,25 @@ public class DateRangeAttributeInstance : AttributeInstance
{
return Value;
}

public override async Task<List<string>> Validate(
EntityConfiguration entityConfiguration,
AttributeConfiguration attributeConfiguration,
IServiceProvider serviceProvider,
bool requiredAttributesCanBeNull = false
) {
List<string> errors = await base.Validate(
entityConfiguration, attributeConfiguration,
serviceProvider, requiredAttributesCanBeNull
);

if (Value != null
&& ((attributeConfiguration as DateRangeAttributeConfiguration).DateRangeAttributeType == DateRangeAttributeType.DateRange && Value.To == null)
) {
errors.Add("DateRange attribute value To is missing");
return errors;
}

return errors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,6 @@ public override void UpdateAttribute(AttributeConfiguration updatedAttribute)
}
}

#region Validation
public override List<string> ValidateInstance(AttributeInstance? instance, bool requiredAttributesCanBeNull = false)
{
List<string> errors = base.ValidateInstance(instance, requiredAttributesCanBeNull);

if (instance == null)
{
return errors;
}

if (instance is not FileAttributeInstance)
{
errors.Add("Cannot validate attribute. Expected attribute type: File");
return errors;
}

return errors;
}

#endregion

#region Equality

public override bool Equals(object obj)
Expand Down
28 changes: 25 additions & 3 deletions CloudFabric.EAV.Domain/Models/Attributes/FileAttributeInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,39 @@ namespace CloudFabric.EAV.Domain.Models.Attributes;

public class FileAttributeInstance : AttributeInstance
{
public FileAttributeValue Value { get; set; }
public FileAttributeValue? Value { get; set; }

public override object? GetValue()
{
return Value;
}

#region Validation
public override async Task<List<string>> Validate(
EntityConfiguration entityConfiguration,
AttributeConfiguration attributeConfiguration,
IServiceProvider serviceProvider,
bool requiredAttributesCanBeNull = false
) {
List<string> errors = await base.Validate(
entityConfiguration, attributeConfiguration,
serviceProvider, requiredAttributesCanBeNull
);

if (attributeConfiguration is not FileAttributeConfiguration)
{
errors.Add("Cannot validate attribute. Expected attribute type: File");
return errors;
}

return errors;
}
#endregion
}

public class FileAttributeValue
{
public string Url { get; set; }
public string? Url { get; set; }

public string FileName { get; set; }
public string? FileName { get; set; }
}
Loading
Loading