-
Notifications
You must be signed in to change notification settings - Fork 686
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
Annotations extensions #636
Open
zhoupan
wants to merge
7
commits into
nhibernate:main
Choose a base branch
from
zhoupan:annotations-extensions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ccbff3d
Create FluentNHibernateAnnotationsExtensions.cs
fitzhoupan 577eba2
Update IdentityStep.cs
fitzhoupan d6a346e
Update PropertyStep.cs
fitzhoupan 7f28cdc
Update FluentNHibernate.csproj
fitzhoupan 73d98cf
Update DefaultAutomappingConfiguration.cs
fitzhoupan 07358e2
Update FluentNHibernate.csproj
fitzhoupan 6fd48cf
Update FluentNHibernateAnnotationsExtensions.cs
fitzhoupan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net461;netcoreapp2.0</TargetFrameworks> | ||
<TargetFrameworks>netstandard2.0;net461;netcoreapp2.0;net6.0</TargetFrameworks> | ||
<PlatformTarget>AnyCpu</PlatformTarget> | ||
<OutputType>Library</OutputType> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>../FluentKey.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="NHibernate" Version="5.3.3" /> | ||
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\FluentKey.snk"> | ||
<Link>FluentKey.snk</Link> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
197 changes: 197 additions & 0 deletions
197
src/FluentNHibernate/FluentNHibernateAnnotationsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Reflection; | ||
using System.Text; | ||
using FluentNHibernate.MappingModel; | ||
using FluentNHibernate.MappingModel.Identity; | ||
using FluentNHibernate.Utils; | ||
using NHibernate; | ||
using NHibernate.Hql.Ast.ANTLR.Tree; | ||
using NHibernate.Id; | ||
|
||
namespace FluentNHibernate; | ||
|
||
public static class FluentNHibernateAnnotationsExtensions | ||
{ | ||
private static INHibernateLogger Log { get; set; } | ||
|
||
private static void UsingLog(Action<INHibernateLogger> log) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be completely unrelated to the proposed changes |
||
{ | ||
var currentLog = Log; | ||
if (currentLog == null) | ||
{ | ||
currentLog = NHibernateLogger.For(typeof(FluentNHibernateAnnotationsExtensions)); | ||
if (currentLog != null) | ||
{ | ||
Log = currentLog; | ||
log?.Invoke(currentLog); | ||
} | ||
} | ||
else | ||
{ | ||
log?.Invoke(currentLog); | ||
} | ||
} | ||
|
||
private static void Debug(string format, params object[] args) | ||
{ | ||
UsingLog(log => log.Debug(format, args)); | ||
} | ||
|
||
/// <summary> | ||
/// Try apply an attribute to target. | ||
/// </summary> | ||
/// <typeparam name="TTarget">Target type</typeparam> | ||
/// <typeparam name="TAttribute">Attribute type</typeparam> | ||
/// <param name="target">Target object</param> | ||
/// <param name="attribute">Attribute object</param> | ||
/// <param name="apply">Change the target object based on an attribute. | ||
/// [Apply] function should return true, only when an attribute be applied or the target object be changed. | ||
/// </param> | ||
/// <returns> | ||
/// Returns false where any arguments is null, otherwise return [Apply] function's result. | ||
/// </returns> | ||
public static bool TryApply<TTarget, TAttribute>( | ||
TTarget target, | ||
TAttribute attribute, | ||
Func<TTarget, TAttribute, bool> apply | ||
) | ||
where TAttribute : Attribute | ||
{ | ||
if (target == null || attribute == null || apply == null) | ||
{ | ||
return false; | ||
} | ||
return apply.Invoke(target, attribute); | ||
} | ||
|
||
/// <summary> | ||
/// Set ColumnMapping.NotNull when [Required] present. | ||
/// <see cref="RequiredAttribute"/> | ||
/// <see cref="ColumnMapping.NotNull"/> | ||
/// </summary> | ||
/// <param name="mapping">ColumnMapping</param> | ||
/// <param name="attribute">RequiredAttribute</param> | ||
/// <returns></returns> | ||
public static bool TryApply(this ColumnMapping mapping, RequiredAttribute attribute) | ||
{ | ||
return TryApply( | ||
mapping, | ||
attribute, | ||
(mapping, attribute) => | ||
{ | ||
Debug( | ||
"[{0}] TryApply(mapping={1}, attribute={2}) Set(NotNull=true)", | ||
typeof(FluentNHibernateAnnotationsExtensions), | ||
mapping, | ||
attribute | ||
); | ||
|
||
mapping.Set(x => x.NotNull, Layer.Defaults, true); | ||
return true; | ||
} | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// Set ColumnMapping.Length when [MaxLength] present. | ||
/// </summary> | ||
/// <see cref="ColumnMapping.Length"/> | ||
/// <param name="mapping">ColumnMapping</param> | ||
/// <param name="attribute">MaxLengthAttribute</param> | ||
/// <returns></returns> | ||
public static bool TryApply(this ColumnMapping mapping, MaxLengthAttribute attribute) | ||
{ | ||
return TryApply( | ||
mapping, | ||
attribute, | ||
(mapping, attribute) => | ||
{ | ||
Debug( | ||
"[{0}] TryApply(mapping={1}, attribute={2}) Set(Length={3})", | ||
typeof(FluentNHibernateAnnotationsExtensions), | ||
mapping, | ||
attribute, | ||
attribute.Length | ||
); | ||
|
||
mapping.Set(x => x.Length, Layer.Defaults, attribute.Length); | ||
return true; | ||
} | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// Configure GeneratorMapping.Class to [assinged] when [DatabaseGenerated(DatabaseGeneratedOption.None)] present. | ||
/// Configure GeneratorMapping.Class to [identity] when [DatabaseGenerated(DatabaseGeneratedOption.Identity)] present. | ||
/// </summary> | ||
/// <see cref="GeneratorMapping.Class"/> | ||
/// <see cref="DatabaseGeneratedAttribute"/> | ||
/// <see cref="DatabaseGeneratedOption"/> | ||
/// <param name="mapping">GeneratorMapping</param> | ||
/// <param name="attribute">DatabaseGeneratedAttribute</param> | ||
/// <returns></returns> | ||
public static bool TryApply(this GeneratorMapping mapping, DatabaseGeneratedAttribute attribute) | ||
{ | ||
return TryApply( | ||
mapping, | ||
attribute, | ||
(mapping, attribute) => | ||
{ | ||
var option = attribute.DatabaseGeneratedOption; | ||
if (option == DatabaseGeneratedOption.None) | ||
{ | ||
Debug( | ||
"[{0}] TryApply(mapping={1}, attribute={2}) Set(Class={3})", | ||
typeof(FluentNHibernateAnnotationsExtensions), | ||
mapping, | ||
attribute, | ||
"assigned" | ||
); | ||
|
||
mapping.Set(x => x.Class, Layer.Defaults, "assigned"); | ||
return true; | ||
} | ||
if (option == DatabaseGeneratedOption.Identity) | ||
{ | ||
Debug( | ||
"[{0}] TryApply(mapping={1}, attribute={2}) Set(Class={3})", | ||
typeof(FluentNHibernateAnnotationsExtensions), | ||
mapping, | ||
attribute, | ||
"identity" | ||
); | ||
|
||
mapping.Set(x => x.Class, Layer.Defaults, "identity"); | ||
return true; | ||
} | ||
return false; | ||
} | ||
); | ||
} | ||
|
||
public static void TryApplyAttributesFrom( | ||
this ColumnMapping mapping, | ||
MemberInfo memberInfo, | ||
bool isIdColumn | ||
) | ||
{ | ||
//Prefer RequiredAttribute. | ||
bool requiredApplied = mapping.TryApply(memberInfo.GetCustomAttribute<RequiredAttribute>()); | ||
//GetType().IsNullable. | ||
if ((!requiredApplied) && (!memberInfo.GetType().IsNullable())) | ||
{ | ||
RequiredAttribute requiredAttribute = new (); | ||
Debug( | ||
"[{0}] TryApply(mapping={1}, attribute={2}) Sender=TryApplyAttributesFrom Cause=!GetType().IsNullable()", | ||
typeof(FluentNHibernateAnnotationsExtensions), | ||
mapping, | ||
requiredAttribute | ||
); | ||
mapping.TryApply(requiredAttribute); | ||
} | ||
mapping.TryApply(memberInfo.GetCustomAttribute<MaxLengthAttribute>()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but I don't think I can accept this as a dependency.