Skip to content

Commit

Permalink
v1.1.0-b prerelease BREAKING CHANGES
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
Danny Logsdon committed Aug 24, 2024
1 parent 7ab8484 commit 6eaacf0
Show file tree
Hide file tree
Showing 106 changed files with 559 additions and 1,502 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ServiceBricks.Storage.AzureDataTables" Version="1.0.11" />
<PackageReference Include="ServiceBricks.Storage.AzureDataTables" Version="1.1.0-b" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ServiceBricks.Logging.Debug\ServiceBricks.Logging.Debug.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public partial class WebRequestMessage : AzureDataTablesDomainObject<WebRequestM
/// </summary>
public virtual string Server { get; set; }

/// <summary>
/// The User storage key associated with the Request.
/// </summary>
public string UserStorageKey { get; set; }

/// <summary>
/// The Request IP Address.
/// </summary>
Expand Down Expand Up @@ -112,11 +117,6 @@ public partial class WebRequestMessage : AzureDataTablesDomainObject<WebRequestM
/// </summary>
public bool? RequestIsHttps { get; set; }

/// <summary>
/// The User storage key associated with the Request.
/// </summary>
public string RequestUserStorageKey { get; set; }

/// <summary>
/// The status code of the response.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public override IResponse ExecuteRule(IBusinessRuleContext context)
var item = e.DomainObject;
item.Key = Guid.NewGuid();

// AI: Set the PartitionKey to be the year and month so that the data is partitioned
item.PartitionKey = item.CreateDate.ToString("yyyyMM");
// AI: Set the PartitionKey to be the year, month and day so that the data is partitioned
item.PartitionKey = item.CreateDate.ToString("yyyyMMdd");

// AI: Set the RowKey to be the reverse date and time so that the newest items are at the top when querying
var reverseDate = DateTimeOffset.MaxValue.Ticks - item.CreateDate.Ticks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public override IResponse ExecuteRule(IBusinessRuleContext context)
var item = ei.DomainObject;
item.Key = Guid.NewGuid();

// AI: Set the PartitionKey to be the year and month so that the data is partitioned
item.PartitionKey = item.CreateDate.ToString("yyyyMM");
// AI: Set the PartitionKey to be the year, month and day so that the data is partitioned
item.PartitionKey = item.CreateDate.ToString("yyyyMMdd");

// AI: Set the RowKey to be the reverse date and time so that the newest items are at the top when querying
var reverseDate = DateTimeOffset.MaxValue.Ticks - item.CreateDate.Ticks;
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-a</Version>
<Version>1.1.0-b</Version>
<Description>ServiceBricks - The open source microservices foundation. Visit https://ServiceBricks.com to learn more.</Description>
<AssemblyTitle>ServiceBricks.Logging.AzureDataTables</AssemblyTitle>
<Authors>holomodular</Authors>
Expand All @@ -19,7 +19,7 @@
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/holomodular/ServiceBricks</RepositoryUrl>
<IsPackable>False</IsPackable>
<IsPackable>True</IsPackable>
</PropertyGroup>
<ItemGroup>
<None Include="docs\readme.md" Pack="True" PackagePath="\" />
Expand All @@ -29,7 +29,7 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="ServiceBricks.Logging" Version="1.0.11" />
<PackageReference Include="ServiceBricks.Storage.AzureDataTables" Version="1.0.11" />
<PackageReference Include="ServiceBricks.Logging" Version="1.1.0-b" />
<PackageReference Include="ServiceBricks.Storage.AzureDataTables" Version="1.1.0-b" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<Compile Include="..\ServiceBricks.Logging.Cosmos\Model\LoggingCosmosConstants.cs" Link="LoggingCosmosConstants.cs" />
<Compile Include="..\ServiceBricks.Logging.Cosmos\Model\LoggingCosmosContext.cs" Link="LoggingCosmosContext.cs" />
<Compile Include="..\ServiceBricks.Logging.Cosmos\Model\LoggingCosmosModule.cs" Link="LoggingCosmosModule.cs" />
<Compile Include="..\ServiceBricks.Logging.Cosmos\Rule\LogMessageCreateRule.cs" Link="LogMessageCreateRule.cs" />
<Compile Include="..\ServiceBricks.Logging.Cosmos\Rule\WebRequestMessageCreateRule.cs" Link="WebRequestMessageCreateRule.cs" />
<Compile Include="..\ServiceBricks.Logging.Cosmos\Service\LogMessageApiService.cs" Link="LogMessageApiService.cs" />
<Compile Include="..\ServiceBricks.Logging.Cosmos\Service\WebRequestMessageApiService.cs" Link="WebRequestMessageApiService.cs" />
<Compile Include="..\ServiceBricks.Logging.Cosmos\Storage\LoggingStorageRepository.cs" Link="LoggingStorageRepository.cs" />
Expand All @@ -25,15 +27,8 @@
<ProjectReference Include="..\ServiceBricks.Logging.EntityFrameworkCore.Debug\ServiceBricks.Logging.EntityFrameworkCore.Debug.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="7.0.20" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="7.0.20" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="8.0.7" />
<ItemGroup>
<PackageReference Include="ServiceBricks.Storage.Cosmos" Version="1.1.0-b" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/V1/ServiceBricks.Logging.Cosmos/Domain/LogMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public partial class LogMessage : EntityFrameworkCoreDomainObject<LogMessage>, I
/// </summary>
public Guid Key { get; set; }

/// <summary>
/// Internal Partition Key
/// </summary>
public string PartitionKey { get; set; }

/// <summary>
/// The date and time the log message was created in UTC.
/// </summary>
Expand Down
15 changes: 10 additions & 5 deletions src/V1/ServiceBricks.Logging.Cosmos/Domain/WebRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public partial class WebRequestMessage : EntityFrameworkCoreDomainObject<WebRequ
/// </summary>
public Guid Key { get; set; }

/// <summary>
/// Internal Partition Key
/// </summary>
public string PartitionKey { get; set; }

/// <summary>
/// The date and time the message was created in UTC.
/// </summary>
Expand All @@ -28,6 +33,11 @@ public partial class WebRequestMessage : EntityFrameworkCoreDomainObject<WebRequ
/// </summary>
public virtual string Server { get; set; }

/// <summary>
/// The User storage key associated with the Request.
/// </summary>
public string UserStorageKey { get; set; }

/// <summary>
/// The Request IP Address.
/// </summary>
Expand Down Expand Up @@ -113,11 +123,6 @@ public partial class WebRequestMessage : EntityFrameworkCoreDomainObject<WebRequ
/// </summary>
public bool? RequestIsHttps { get; set; }

/// <summary>
/// The User storage key associated with the Request.
/// </summary>
public string RequestUserStorageKey { get; set; }

/// <summary>
/// The status code of the response.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using ServiceBricks.Logging.EntityFrameworkCore;

namespace ServiceBricks.Logging.Cosmos
{
Expand Down Expand Up @@ -31,8 +32,7 @@ public static IApplicationBuilder StartServiceBricksLoggingCosmos(this IApplicat
ModuleStarted = true;

// AI: Start the parent module.
// AI: If the primary keys of the Cosmos models do not match the EFC module, we can't use it rules, so skip EFC and call start on the core module instead.
applicationBuilder.StartServiceBricksLogging(); // Skip EFC
applicationBuilder.StartServiceBricksLoggingEntityFrameworkCore();

return applicationBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceBricks.Logging.EntityFrameworkCore;
using ServiceBricks.Storage.EntityFrameworkCore;

namespace ServiceBricks.Logging.Cosmos
Expand All @@ -22,8 +23,7 @@ public static IServiceCollection AddServiceBricksLoggingCosmos(this IServiceColl
ModuleRegistry.Instance.RegisterItem(typeof(LoggingCosmosModule), new LoggingCosmosModule());

// AI: Add the parent module
// AI: If the primary keys of the Cosmos models do not match the EFC module, we can't use EFC rules, so skip EFC and call start on the core module instead.
services.AddServiceBricksLogging(configuration); // Skip EFC
services.AddServiceBricksLoggingEntityFrameworkCore(configuration);

// AI: Register the database for the module
var builder = new DbContextOptionsBuilder<LoggingCosmosContext>();
Expand All @@ -40,22 +40,22 @@ public static IServiceCollection AddServiceBricksLoggingCosmos(this IServiceColl
services.AddScoped<IStorageRepository<LogMessage>, LoggingStorageRepository<LogMessage>>();
services.AddScoped<IStorageRepository<WebRequestMessage>, LoggingStorageRepository<WebRequestMessage>>();

// AI: Register business rules for the module
// AI: If the primary keys of the Cosmos models match the EFC module, we can use the EFC rules
DomainCreateDateRule<LogMessage>.RegisterRule(BusinessRuleRegistry.Instance);
DomainQueryPropertyRenameRule<LogMessage>.RegisterRule(BusinessRuleRegistry.Instance, "StorageKey", "Key");

DomainCreateDateRule<WebRequestMessage>.RegisterRule(BusinessRuleRegistry.Instance);
DomainQueryPropertyRenameRule<WebRequestMessage>.RegisterRule(BusinessRuleRegistry.Instance, "StorageKey", "Key");

// AI: Add API services for the module. Each DTO should have two registrations, one for the generic IApiService<> and one for the named interface
// AI: If the primary keys of the Cosmos models match the EFC module, we can use the EFC rules
services.AddScoped<IApiService<LogMessageDto>, LogMessageApiService>();
services.AddScoped<ILogMessageApiService, LogMessageApiService>();

services.AddScoped<IApiService<WebRequestMessageDto>, WebRequestMessageApiService>();
services.AddScoped<IWebRequestMessageApiService, WebRequestMessageApiService>();

// AI: Register business rules for the module
DomainCreateDateRule<LogMessage>.RegisterRule(BusinessRuleRegistry.Instance);
DomainQueryPropertyRenameRule<LogMessage>.RegisterRule(BusinessRuleRegistry.Instance, "StorageKey", "Key");
LogMessageCreateRule.RegisterRule(BusinessRuleRegistry.Instance);

DomainCreateDateRule<WebRequestMessage>.RegisterRule(BusinessRuleRegistry.Instance);
DomainQueryPropertyRenameRule<WebRequestMessage>.RegisterRule(BusinessRuleRegistry.Instance, "StorageKey", "Key");
WebRequestMessageCreateRule.RegisterRule(BusinessRuleRegistry.Instance);

return services;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public LogMessageMappingProfile()
// AI: Add mappings for LogMessageDto and LogMessage
CreateMap<LogMessageDto, LogMessage>()
.ForMember(x => x.CreateDate, y => y.Ignore())
.ForMember(x => x.PartitionKey, y => y.Ignore())
.ForMember(x => x.Key, y => y.MapFrom<KeyResolver>());

CreateMap<LogMessage, LogMessageDto>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public WebRequestMessageMappingProfile()
// AI: Add mappings for WebRequestMessageDto and WebRequestMessage
CreateMap<WebRequestMessageDto, WebRequestMessage>()
.ForMember(x => x.CreateDate, y => y.Ignore())
.ForMember(x => x.PartitionKey, y => y.Ignore())
.ForMember(x => x.Key, y => y.MapFrom<KeyResolver>());

CreateMap<WebRequestMessage, WebRequestMessageDto>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public static partial class LoggingCosmosConstants
/// <summary>
/// Default Container Name.
/// </summary>
public const string DEFAULT_CONTAINER_NAME = "Logging";
public const string CONTAINER_PREFIX = "Logging";

/// <summary>
/// Get the container name for the given table name.
/// </summary>
/// <param name="tableName"></param>
/// <returns></returns>
public static string GetContainerName(string tableName)
{
return CONTAINER_PREFIX + tableName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

// AI: Set the default container name
builder.Model.SetDefaultContainer(LoggingCosmosConstants.DEFAULT_CONTAINER_NAME);

// AI: Create the model for each table
builder.Entity<LogMessage>().HasKey(p => p.Key);
builder.Entity<LogMessage>().Property(p => p.Key).ValueGeneratedOnAdd();
builder.Entity<LogMessage>().HasPartitionKey(p => p.PartitionKey);
builder.Entity<LogMessage>().HasIndex(key => new { key.Application, key.Level, key.CreateDate });
builder.Entity<LogMessage>().ToContainer(LoggingCosmosConstants.GetContainerName(nameof(LogMessage)));

builder.Entity<WebRequestMessage>().HasKey(p => p.Key);
builder.Entity<WebRequestMessage>().Property(p => p.Key).ValueGeneratedOnAdd();
builder.Entity<WebRequestMessage>().HasPartitionKey(p => p.PartitionKey);
builder.Entity<WebRequestMessage>().HasIndex(key => new { key.Application, key.UserStorageKey, key.CreateDate });
builder.Entity<WebRequestMessage>().ToContainer(LoggingCosmosConstants.GetContainerName(nameof(WebRequestMessage)));
}

/// <summary>
Expand Down
76 changes: 76 additions & 0 deletions src/V1/ServiceBricks.Logging.Cosmos/Rule/LogMessageCreateRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.Extensions.Logging;
using ServiceBricks.Storage.EntityFrameworkCore;

namespace ServiceBricks.Logging.Cosmos
{
/// <summary>
/// This is a business rule for creating a LogMessage domain object. It will set the Key and PartitionKey.
/// </summary>
public sealed class LogMessageCreateRule : BusinessRule
{
private readonly ILogger _logger;

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

/// <summary>
/// Register the business rule to the DomainCreateBeforeEvent.
/// </summary>
/// <param name="registry"></param>
public static void RegisterRule(IBusinessRuleRegistry registry)
{
registry.RegisterItem(
typeof(DomainCreateBeforeEvent<LogMessage>),
typeof(LogMessageCreateRule));
}

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

try
{
// AI: Make sure the context object is the correct type
if (context.Object is DomainCreateBeforeEvent<LogMessage> e)
{
// AI: Set the Key and PartitionKey
var item = e.DomainObject;
item.Key = Guid.NewGuid();

// AI: Set the PartitionKey to be the year, month and day so that the data is partitioned
item.PartitionKey = item.CreateDate.ToString("yyyyMMdd");
}
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
response.AddMessage(ResponseMessage.CreateError(LocalizationResource.ERROR_BUSINESS_RULE));
}

return response;
}

/// <summary>
/// Execute the business rule.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override Task<IResponse> ExecuteRuleAsync(IBusinessRuleContext context)
{
// AI: There is no async work, so just call the sync method
return Task.FromResult<IResponse>(ExecuteRule(context));
}
}
}
Loading

0 comments on commit 6eaacf0

Please sign in to comment.