Skip to content

Commit

Permalink
v1.1.0-k prerelease
Browse files Browse the repository at this point in the history
Business rules and modules
  • Loading branch information
Danny Logsdon committed Oct 1, 2024
1 parent c12eb1a commit 49bf740
Show file tree
Hide file tree
Showing 138 changed files with 1,106 additions and 880 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

<ItemGroup>
<Compile Include="..\ServiceBricks.Cache.AzureDataTables\Domain\CacheData.cs" Link="CacheData.cs" />
<Compile Include="..\ServiceBricks.Cache.AzureDataTables\Extensions\ApplicationBuilderExtensions.cs" Link="ApplicationBuilderExtensions.cs" />
<Compile Include="..\ServiceBricks.Cache.AzureDataTables\Extensions\ServiceCollectionExtensions.cs" Link="ServiceCollectionExtensions.cs" />
<Compile Include="..\ServiceBricks.Cache.AzureDataTables\Mapping\CacheDataMappingProfile.cs" Link="CacheDataMappingProfile.cs" />
<Compile Include="..\ServiceBricks.Cache.AzureDataTables\Model\CacheAzureDataTablesConstants.cs" Link="CacheAzureDataTablesConstants.cs" />
<Compile Include="..\ServiceBricks.Cache.AzureDataTables\Model\CacheAzureDataTablesModule.cs" Link="CacheAzureDataTablesModule.cs" />
<Compile Include="..\ServiceBricks.Cache.AzureDataTables\Rule\CacheAzureDataTablesModuleAddRule.cs" Link="CacheAzureDataTablesModuleAddRule.cs" />
<Compile Include="..\ServiceBricks.Cache.AzureDataTables\Rule\CacheAzureDataTablesModuleStartRule.cs" Link="CacheAzureDataTablesModuleStartRule.cs" />
<Compile Include="..\ServiceBricks.Cache.AzureDataTables\Rule\CacheDataCreateRule.cs" Link="CacheDataCreateRule.cs" />
<Compile Include="..\ServiceBricks.Cache.AzureDataTables\Service\CacheDataApiService.cs" Link="CacheDataApiService.cs" />
<Compile Include="..\ServiceBricks.Cache.AzureDataTables\Storage\CacheStorageRepository.cs" Link="CacheStorageRepository.cs" />
Expand All @@ -22,6 +23,6 @@
<ProjectReference Include="..\ServiceBricks.Cache.Debug\ServiceBricks.Cache.Debug.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ServiceBricks.Storage.AzureDataTables" Version="1.1.0-g" />
<PackageReference Include="ServiceBricks.Storage.AzureDataTables" Version="1.1.0-k" />
</ItemGroup>
</Project>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,16 @@ public static partial class ServiceCollectionExtensions
/// <returns></returns>
public static IServiceCollection AddServiceBricksCacheAzureDataTables(this IServiceCollection services, IConfiguration configuration)
{
// AI: Add the module to the ModuleRegistry
ModuleRegistry.Instance.RegisterItem(typeof(CacheAzureDataTablesModule), new CacheAzureDataTablesModule());

// AI: Add parent module
// AI: Add the parent module first
services.AddServiceBricksCache(configuration);

// AI: Configure all options for the module

// AI: Add storage services for the module. Each domain object should have its own storage repository
services.AddScoped<IStorageRepository<CacheData>, CacheStorageRepository<CacheData>>();

// AI: Add API services for the module. Each DTO should have two registrations, one for the generic IApiService<> and one for the named interface
services.AddScoped<IApiService<CacheDataDto>, CacheDataApiService>();
services.AddScoped<ICacheDataApiService, CacheDataApiService>();
// AI: Add this module to the ModuleRegistry
ModuleRegistry.Instance.Register(CacheAzureDataTablesModule.Instance);

// AI: Register business rules for the module
DomainCreateUpdateDateRule<CacheData>.Register(BusinessRuleRegistry.Instance);
DomainDateTimeOffsetRule<CacheData>.Register(BusinessRuleRegistry.Instance, nameof(CacheData.ExpirationDate));
ApiConcurrencyByUpdateDateRule<CacheData, CacheDataDto>.Register(BusinessRuleRegistry.Instance);
CacheDataCreateRule.Register(BusinessRuleRegistry.Instance);
DomainQueryPropertyRenameRule<CacheData>.Register(BusinessRuleRegistry.Instance, "StorageKey", "PartitionKey");
// AI: Add module business rules
CacheAzureDataTablesModuleAddRule.Register(BusinessRuleRegistry.Instance);
CacheAzureDataTablesModuleStartRule.Register(BusinessRuleRegistry.Instance);
ModuleSetStartedRule<CacheAzureDataTablesModule>.Register(BusinessRuleRegistry.Instance);

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ namespace ServiceBricks.Cache.AzureDataTables
/// <summary>
/// The module definition for the ServiceBricks Cache Azure Data Tables module.
/// </summary>
public partial class CacheAzureDataTablesModule : IModule
public partial class CacheAzureDataTablesModule : ServiceBricks.Module
{
/// <summary>
/// Instance
/// </summary>
public static CacheAzureDataTablesModule Instance = new CacheAzureDataTablesModule();

/// <summary>
/// Constructor.
/// </summary>
Expand All @@ -21,20 +26,5 @@ public CacheAzureDataTablesModule()
new CacheModule()
};
}

/// <summary>
/// The list of dependent modules.
/// </summary>
public List<IModule> DependentModules { get; }

/// <summary>
/// The list of assemblies that contain automapper profiles.
/// </summary>
public List<Assembly> AutomapperAssemblies { get; }

/// <summary>
/// The list of assemblies that contain views.
/// </summary>
public List<Assembly> ViewAssemblies { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Microsoft.Extensions.DependencyInjection;

namespace ServiceBricks.Cache.AzureDataTables
{
/// <summary>
/// This rule is executed when the ServiceBricks module is added.
/// </summary>
public sealed class CacheAzureDataTablesModuleAddRule : BusinessRule
{
/// <summary>
/// Constructor.
/// </summary>
public CacheAzureDataTablesModuleAddRule()
{
}

/// <summary>
/// Register the rule
/// </summary>
public static void Register(IBusinessRuleRegistry registry)
{
registry.Register(
typeof(ModuleAddEvent<CacheAzureDataTablesModule>),
typeof(CacheAzureDataTablesModuleAddRule));
}

/// <summary>
/// UnRegister the rule.
/// </summary>
public static void UnRegister(IBusinessRuleRegistry registry)
{
registry.UnRegister(
typeof(ModuleAddEvent<CacheAzureDataTablesModule>),
typeof(CacheAzureDataTablesModuleAddRule));
}

/// <summary>
/// Execute the business rule.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override IResponse ExecuteRule(IBusinessRuleContext context)
{
var response = new Response();

// AI: Make sure the context object is the correct type
if (context == null || context.Object == null)
{
response.AddMessage(ResponseMessage.CreateError(LocalizationResource.PARAMETER_MISSING, "context"));
return response;
}
var e = context.Object as ModuleAddEvent<CacheAzureDataTablesModule>;
if (e == null || e.DomainObject == null || e.ServiceCollection == null)
{
response.AddMessage(ResponseMessage.CreateError(LocalizationResource.PARAMETER_MISSING, "context"));
return response;
}

// AI: Perform logic
var services = e.ServiceCollection;
//var configuration = e.Configuration;

// AI: Configure all options for the module

// AI: Add storage services for the module. Each domain object should have its own storage repository
services.AddScoped<IStorageRepository<CacheData>, CacheStorageRepository<CacheData>>();

// AI: Add API services for the module. Each DTO should have two registrations, one for the generic IApiService<> and one for the named interface
services.AddScoped<IApiService<CacheDataDto>, CacheDataApiService>();
services.AddScoped<ICacheDataApiService, CacheDataApiService>();

// AI: Register business rules for the module
DomainCreateUpdateDateRule<CacheData>.Register(BusinessRuleRegistry.Instance);
DomainDateTimeOffsetRule<CacheData>.Register(BusinessRuleRegistry.Instance, nameof(CacheData.ExpirationDate));
ApiConcurrencyByUpdateDateRule<CacheData, CacheDataDto>.Register(BusinessRuleRegistry.Instance);
CacheDataCreateRule.Register(BusinessRuleRegistry.Instance);
DomainQueryPropertyRenameRule<CacheData>.Register(BusinessRuleRegistry.Instance, "StorageKey", "PartitionKey");

return response;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Azure.Data.Tables;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceBricks.Storage.AzureDataTables;

namespace ServiceBricks.Cache.AzureDataTables
{
/// <summary>
/// This rule is executed when the ServiceBricks module is added.
/// </summary>
public sealed class CacheAzureDataTablesModuleStartRule : BusinessRule
{
/// <summary>
/// Constructor.
/// </summary>
public CacheAzureDataTablesModuleStartRule()
{
}

/// <summary>
/// Register the rule
/// </summary>
public static void Register(IBusinessRuleRegistry registry)
{
registry.Register(
typeof(ModuleStartEvent<CacheAzureDataTablesModule>),
typeof(CacheAzureDataTablesModuleStartRule));
}

/// <summary>
/// UnRegister the rule.
/// </summary>
public static void UnRegister(IBusinessRuleRegistry registry)
{
registry.UnRegister(
typeof(ModuleStartEvent<CacheAzureDataTablesModule>),
typeof(CacheAzureDataTablesModuleStartRule));
}

/// <summary>
/// Execute the business rule.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override IResponse ExecuteRule(IBusinessRuleContext context)
{
var response = new Response();

// AI: Make sure the context object is the correct type
if (context == null || context.Object == null)
{
response.AddMessage(ResponseMessage.CreateError(LocalizationResource.PARAMETER_MISSING, "context"));
return response;
}
var e = context.Object as ModuleStartEvent<CacheAzureDataTablesModule>;
if (e == null || e.DomainObject == null || e.ApplicationBuilder == null)
{
response.AddMessage(ResponseMessage.CreateError(LocalizationResource.PARAMETER_MISSING, "context"));
return response;
}

// AI: Perform logic

// AI: Get the connection string
var configuration = e.ApplicationBuilder.ApplicationServices.GetRequiredService<IConfiguration>();
var connectionString = configuration.GetAzureDataTablesConnectionString(
CacheAzureDataTablesConstants.APPSETTING_CONNECTION_STRING);

// AI: Create each table in the module
TableClient tableClient = new TableClient(
connectionString,
CacheAzureDataTablesConstants.GetTableName(nameof(CacheData)));
tableClient.CreateIfNotExists();

return response;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
using Microsoft.Extensions.Logging;

namespace ServiceBricks.Cache.AzureDataTables
namespace ServiceBricks.Cache.AzureDataTables
{
/// <summary>
/// This is a business rule for creating a CacheData domain object. It will set the PartitionKey and RowKey.
/// </summary>
public sealed class CacheDataCreateRule : BusinessRule
{
private readonly ILogger _logger;

/// <summary>
/// Constructor.
/// </summary>
/// <param name="loggerFactory"></param>
public CacheDataCreateRule(ILoggerFactory loggerFactory)
public CacheDataCreateRule()
{
_logger = loggerFactory.CreateLogger<CacheDataCreateRule>();
Priority = PRIORITY_LOW;
}

Expand Down Expand Up @@ -49,23 +43,24 @@ public override IResponse ExecuteRule(IBusinessRuleContext context)
{
var response = new Response();

try
// AI: Make sure the context object is the correct type
if (context == null || context.Object == null)
{
// AI: Make sure the context object is the correct type
if (context.Object is DomainCreateBeforeEvent<CacheData> e)
{
// AI: Set the PartitionKey and RowKey
var item = e.DomainObject;
item.PartitionKey = item.CacheKey;
item.RowKey = string.Empty;
}
response.AddMessage(ResponseMessage.CreateError(LocalizationResource.PARAMETER_MISSING, "context"));
return response;
}
catch (Exception ex)
var e = context.Object as DomainCreateBeforeEvent<CacheData>;
if (e == null || e.DomainObject == null)
{
_logger.LogError(ex, ex.Message);
response.AddMessage(ResponseMessage.CreateError(LocalizationResource.ERROR_BUSINESS_RULE));
response.AddMessage(ResponseMessage.CreateError(LocalizationResource.PARAMETER_MISSING, "context"));
return response;
}

// AI: Set the PartitionKey and RowKey
var item = e.DomainObject;
item.PartitionKey = item.CacheKey;
item.RowKey = string.Empty;

return response;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.1.0-g</Version>
<Version>1.1.0-k</Version>
<Description>The open source microservices platform. Visit https://ServiceBricks.com to learn more.</Description>
<PackageId>ServiceBricks.Cache.AzureDataTables</PackageId>
<Title>ServiceBricks.Cache.AzureDataTables</Title>
Expand Down Expand Up @@ -33,9 +33,9 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="ServiceBricks.Cache" Version="1.1.0-g" />
<PackageReference Include="ServiceBricks.Cache" Version="1.1.0-k" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ServiceBricks.Storage.AzureDataTables" Version="1.1.0-g" />
<PackageReference Include="ServiceBricks.Storage.AzureDataTables" Version="1.1.0-k" />
</ItemGroup>
</Project>
Loading

0 comments on commit 49bf740

Please sign in to comment.