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

Fluent mapping auth and configuration schemas #529

Merged
merged 6 commits into from
Jan 20, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
<Compile Include="..\..\__SolutionItems\CommonAssemblyInfo.cs" Link="Properties\CommonAssemblyInfo.cs" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Mapping\Generated.Framework.Authorization.Domain.hbm.xml" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Framework.Authorization.Domain\Framework.Authorization.Domain.csproj" />
<ProjectReference Include="..\..\_DomainDriven\Framework.DomainDriven.NHibernate\Framework.DomainDriven.NHibernate.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using FluentNHibernate.Mapping;

using Framework.Authorization.Domain;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

public abstract class AuthBaseMap<TEntity> : ClassMap<TEntity>
where TEntity : AuditPersistentDomainObjectBase
{
protected AuthBaseMap()
{
this.Schema("auth");

this.DynamicUpdate();

this.Id(x => x.Id).GeneratedBy.GuidComb();

this.Map(x => x.CreatedBy);
this.Map(x => x.CreateDate);
this.Map(x => x.ModifiedBy);
this.Map(x => x.ModifyDate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Framework.Authorization.Domain;
using Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping;

public class BusinessRoleMap : AuthBaseMap<BusinessRole>
{
public BusinessRoleMap()
{
this.Map(x => x.Name).Unique().Not.Nullable();
this.Map(x => x.Description);
this.HasMany(x => x.Permissions).AsSet().Inverse().Cascade.None();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Framework.Authorization.Domain;
using Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping;

public class PermissionMap : AuthBaseMap<Permission>
{
public PermissionMap()
{
this.Map(x => x.Comment).Length(int.MaxValue);
this.References(x => x.DelegatedFrom).Column($"{nameof(Permission.DelegatedFrom)}Id");
this.References(x => x.Principal).Column($"{nameof(Permission.Principal)}Id").Not.Nullable();
this.References(x => x.Role).Column($"{nameof(Permission.Role)}Id").Not.Nullable();
this.Component(
x => x.Period,
part =>
{
part.Map(x => x.EndDate).Column("periodendDate");
part.Map(x => x.StartDate).Column("periodstartDate");
});

this.HasMany(x => x.DelegatedTo).AsSet().Inverse().Cascade.AllDeleteOrphan();
this.HasMany(x => x.Restrictions).AsSet().Inverse().Cascade.AllDeleteOrphan();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Framework.Authorization.Domain;
using Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping;

public class PermissionRestrictionMap : AuthBaseMap<PermissionRestriction>
{
public PermissionRestrictionMap()
{
this.Map(x => x.SecurityContextId).Not.Nullable()
.UniqueKey("UIX_permission_securityContextId_securityContextTypePermissionRestriction");
this.References(x => x.Permission).Column($"{nameof(PermissionRestriction.Permission)}Id").Not.Nullable()
.UniqueKey("UIX_permission_securityContextId_securityContextTypePermissionRestriction");
this.References(x => x.SecurityContextType).Column($"{nameof(PermissionRestriction.SecurityContextType)}Id").Not.Nullable()
.UniqueKey("UIX_permission_securityContextId_securityContextTypePermissionRestriction");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Framework.Authorization.Domain;
using Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping;

public class PrincipalMap : AuthBaseMap<Principal>
{
public PrincipalMap()
{
this.Map(x => x.Name).Not.Nullable().UniqueKey("UIX_namePrincipal");
this.References(x => x.RunAs).Column($"{nameof(Principal.RunAs)}Id");
this.HasMany(x => x.Permissions).AsSet().Inverse().Cascade.AllDeleteOrphan();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Framework.Authorization.Domain;
using Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping;

public class SecurityContextTypeMap : AuthBaseMap<SecurityContextType>
{
public SecurityContextTypeMap() => this.Map(x => x.Name).Not.Nullable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public ServerGenerationEnvironment(DatabaseName databaseName)
/// DatabaseName - Берётся из namespace'а сборки, которая сдержит тип PersistentDomainObjectBase (метод GetTargetSystemName);
/// Types - Список доменных объектов. Это все типы наследованные от PersistentDomainObjectBase той сборки, в которой содеержится PersistentDomainObjectBase.
/// </summary>
public MappingSettings MappingSettings => new MappingSettings<PersistentDomainObjectBase>(this.DAL.GetMappingGenerators().Select(mg => mg.Generate()), this.DatabaseName, this.DatabaseName.ToDefaultAudit());
public MappingSettings MappingSettings => new MappingSettings<PersistentDomainObjectBase>(this.DatabaseName, this.DatabaseName.ToDefaultAudit());


public DatabaseName DatabaseName { get; }


public MappingSettings GetMappingSettings(DatabaseName dbName, AuditDatabaseName dbAuditName)
{
return new MappingSettings<PersistentDomainObjectBase>(this.DAL.GetMappingGenerators().Select(mg => mg.Generate()), dbName, dbAuditName);
return new MappingSettings<PersistentDomainObjectBase>(dbName, dbAuditName);
}

public MappingSettings GetMappingSettingsWithoutAudit(DatabaseName dbName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

<ProjectReference Include="..\Framework.Authorization.Domain.Models\Framework.Authorization.Domain.Models.csproj" />

<ProjectReference Include="..\Framework.Authorization.Generated.DAL.NHibernate\Framework.Authorization.Generated.DAL.NHibernate.csproj" />

<ProjectReference Include="..\Framework.Authorization.LegacyAttributes\Framework.Authorization.LegacyAttributes.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Framework.DomainDriven;
using Framework.Authorization.Generated.DAL.NHibernate;
using Framework.DomainDriven;
using Framework.DomainDriven.DBGenerator;
using Framework.DomainDriven.NHibernate;

namespace Framework.Authorization.TestGenerate;

Expand Down Expand Up @@ -37,17 +39,30 @@ public string GenerateDB(
bool preserveSchemaDatabase = false,
DbUserCredential credentials = null)
{
var generator = new DBGenerator(this.Environment.GetMappingSettings(databaseName, auditDatabaseName));
var generator = new DBGenerator(this.GetAuthMappingSettings(serverName, databaseName, auditDatabaseName));
var result = generator.Generate(
serverName,
mode: mode,
generatorMode: generatorMode,
migrationScriptFolderPaths: migrationScriptFolderPaths,
auditMigrationScriptFolderPaths: auditMigrationScriptFolderPaths,
preserveSchemaDatabase: preserveSchemaDatabase,
credentials: credentials);
serverName,
mode: mode,
generatorMode: generatorMode,
migrationScriptFolderPaths: migrationScriptFolderPaths,
auditMigrationScriptFolderPaths: auditMigrationScriptFolderPaths,
preserveSchemaDatabase: preserveSchemaDatabase,
credentials: credentials);

var lines = result.ToNewLinesCombined();
return lines;
}

private MappingSettings GetAuthMappingSettings(string serverName, DatabaseName dbName, AuditDatabaseName dbAuditName) =>
this.Environment.GetMappingSettings(dbName, dbAuditName).AddInitializer(
new DefaultConfigurationInitializer(
new ManualDefaultConnectionStringSource(
$"Data Source={serverName};Initial Catalog={dbName}"),
new DefaultConfigurationInitializerSettings
{
FluentAssemblyList =
[
typeof(AuthorizationMappingSettings).Assembly
]
}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public IEnumerable<FileInfo> GenerateMain()
{
return this.GenerateBLLCore()
.Concat(this.GenerateBLL())
.Concat(this.GenerateServerDTO())
.Concat(this.GenerateDAL());
.Concat(this.GenerateServerDTO());
}

[TestMethod]
Expand Down Expand Up @@ -77,17 +76,4 @@ private IEnumerable<FileInfo> GenerateServerDTO()
"Authorization.Generated",
this.CheckOutService);
}

[TestMethod]
public void GenerateDALTest()
{
this.GenerateDAL().ToList();
}

private IEnumerable<FileInfo> GenerateDAL()
{
var generator = new DALFileGenerator(this.Environment.DAL);

return generator.Generate(this.GeneratePath + @"/Framework.Authorization.Generated.DAL.NHibernate/Mapping", this.CheckOutService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,4 @@
<ProjectReference Include="..\..\_DomainDriven\Framework.DomainDriven.NHibernate\Framework.DomainDriven.NHibernate.csproj" />
<ProjectReference Include="..\Framework.Configuration.Domain\Framework.Configuration.Domain.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Mapping\Generated.Framework.Configuration.Domain.hbm.xml" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using FluentNHibernate.Mapping;

using Framework.Configuration.Domain;

namespace Framework.Configuration.Generated.DAL.NHibernate.Mapping.Base;

public abstract class ConfigurationBaseMap<TEntity> : ClassMap<TEntity>
where TEntity : AuditPersistentDomainObjectBase
{
protected ConfigurationBaseMap()
{
this.Schema("configuration");

this.DynamicUpdate();

this.Id(x => x.Id).GeneratedBy.GuidComb();

this.Map(x => x.Active);
this.Map(x => x.CreatedBy);
this.Map(x => x.CreateDate);
this.Map(x => x.ModifiedBy);
this.Map(x => x.ModifyDate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Framework.Configuration.Domain;
using Framework.Configuration.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Configuration.Generated.DAL.NHibernate.Mapping;

public class CodeFirstSubscriptionMap : ConfigurationBaseMap<CodeFirstSubscription>
{
public CodeFirstSubscriptionMap() =>
this.Map(x => x.Code).Length(512).UniqueKey("UIX_codeCodeFirstSubscription").Not.Nullable();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Framework.Configuration.Domain;
using Framework.Configuration.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Configuration.Generated.DAL.NHibernate.Mapping;

public class ControlSettingsMap : ConfigurationBaseMap<ControlSettings>
{
public ControlSettingsMap()
{
this.Map(x => x.Name).Length(int.MaxValue).Not.Nullable();
this.Map(x => x.AccountName);
this.Map(x => x.Type);
this.References(x => x.Parent).Column($"{nameof(ControlSettings.Parent)}Id");
this.HasMany(x => x.Children).AsSet().Inverse().Cascade.None();
this.HasMany(x => x.ControlSettingsParams).AsSet().Inverse().Cascade.AllDeleteOrphan();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Framework.Configuration.Domain;
using Framework.Configuration.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Configuration.Generated.DAL.NHibernate.Mapping;

public class ControlSettingsParamMap : ConfigurationBaseMap<ControlSettingsParam>
{
public ControlSettingsParamMap()
{
this.Map(x => x.Type);
this.References(x => x.ControlSettings).Column($"{nameof(ControlSettingsParam.ControlSettings)}Id").Not.Nullable();
this.HasMany(x => x.ControlSettingsParamValues).AsSet().Inverse().Cascade.AllDeleteOrphan();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Framework.Configuration.Domain;
using Framework.Configuration.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Configuration.Generated.DAL.NHibernate.Mapping;

public class ControlSettingsParamValueMap : ConfigurationBaseMap<ControlSettingsParamValue>
{
public ControlSettingsParamValueMap()
{
this.Map(x => x.Culture);
this.Map(x => x.Value).Length(int.MaxValue);
this.Map(x => x.ValueTypeName);
this.References(x => x.ControlSettingsParam).Column($"{nameof(ControlSettingsParamValue.ControlSettingsParam)}Id").Not.Nullable();
}
}
Loading
Loading