mirror of https://github.com/abpframework/abp.git
18 changed files with 233 additions and 3 deletions
@ -0,0 +1,35 @@ |
|||
using System.Collections.Generic; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.ObjectExtending; |
|||
|
|||
namespace AutoMapper |
|||
{ |
|||
public static class AbpAutoMapperExtensibleDtoExtensions |
|||
{ |
|||
public static IMappingExpression<TSource, TDestination> MapExtraProperties<TSource, TDestination>( |
|||
this IMappingExpression<TSource, TDestination> mappingExpression) |
|||
where TDestination : IHasExtraProperties |
|||
where TSource : IHasExtraProperties |
|||
{ |
|||
var properties = ObjectExtensionManager.GetProperties<TDestination>(); |
|||
return mappingExpression |
|||
.ForMember( |
|||
x => x.ExtraProperties, |
|||
y => y.MapFrom( |
|||
(source, dto, extraProps) => |
|||
{ |
|||
var result = extraProps.IsNullOrEmpty() |
|||
? new Dictionary<string, object>() |
|||
: new Dictionary<string, object>(extraProps); |
|||
|
|||
foreach (var property in properties) |
|||
{ |
|||
result[property.Name] = source.ExtraProperties[property.Name]; |
|||
} |
|||
|
|||
return result; |
|||
}) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
using System; |
|||
using Volo.Abp.ObjectExtending; |
|||
|
|||
namespace Volo.Abp.Application.Dtos |
|||
{ |
|||
[Serializable] |
|||
public abstract class ExtensibleEntityDto<TKey> : ExtensibleObject, IEntityDto<TKey> |
|||
{ |
|||
/// <summary>
|
|||
/// Id of the entity.
|
|||
/// </summary>
|
|||
public TKey Id { get; set; } |
|||
|
|||
public override string ToString() |
|||
{ |
|||
return $"[DTO: {GetType().Name}] Id = {Id}"; |
|||
} |
|||
} |
|||
|
|||
[Serializable] |
|||
public abstract class ExtensibleEntityDto : ExtensibleObject, IEntityDto |
|||
{ |
|||
public override string ToString() |
|||
{ |
|||
return $"[DTO: {GetType().Name}]"; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
|||
<ConfigureAwait ContinueOnCapturedContext="false" /> |
|||
</Weavers> |
|||
@ -0,0 +1,30 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
|||
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. --> |
|||
<xs:element name="Weavers"> |
|||
<xs:complexType> |
|||
<xs:all> |
|||
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1"> |
|||
<xs:complexType> |
|||
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" /> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:all> |
|||
<xs:attribute name="VerifyAssembly" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="VerifyIgnoreCodes" type="xs:string"> |
|||
<xs:annotation> |
|||
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="GenerateXsd" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:schema> |
|||
@ -0,0 +1,21 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\configureawait.props" /> |
|||
<Import Project="..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<AssemblyName>Volo.Abp.ObjectExtending</AssemblyName> |
|||
<PackageId>Volo.Abp.ObjectExtending</PackageId> |
|||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.ObjectExtending |
|||
{ |
|||
public class AbpObjectExtendingModule : AbpModule |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Volo.Abp.Data; |
|||
|
|||
namespace Volo.Abp.ObjectExtending |
|||
{ |
|||
[Serializable] |
|||
public class ExtensibleObject : IHasExtraProperties |
|||
{ |
|||
public Dictionary<string, object> ExtraProperties { get; protected set; } |
|||
|
|||
public ExtensibleObject() |
|||
{ |
|||
ExtraProperties = new Dictionary<string, object>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.ObjectExtending |
|||
{ |
|||
public class ObjectExtensionInfo |
|||
{ |
|||
public Dictionary<string, ObjectExtensionPropertyInfo> Properties { get; } |
|||
|
|||
public ObjectExtensionInfo() |
|||
{ |
|||
Properties = new Dictionary<string, ObjectExtensionPropertyInfo>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Collections.Immutable; |
|||
using Volo.Abp.Data; |
|||
|
|||
namespace Volo.Abp.ObjectExtending |
|||
{ |
|||
public static class ObjectExtensionManager |
|||
{ |
|||
//TODO: Concurrent, to allow extend on runtime!
|
|||
private static Dictionary<Type, ObjectExtensionInfo> Extensions { get; } |
|||
|
|||
static ObjectExtensionManager() |
|||
{ |
|||
Extensions = new Dictionary<Type, ObjectExtensionInfo>(); |
|||
} |
|||
|
|||
public static ObjectExtensionPropertyInfo AddProperty<TDto>( |
|||
string propertyName, |
|||
Action<ObjectExtensionPropertyInfo> configureAction = null) |
|||
{ |
|||
var extensionInfo = Extensions.GetOrAdd(typeof(TDto), () => new ObjectExtensionInfo()); |
|||
var propertyInfo = extensionInfo.Properties.GetOrAdd(propertyName, () => new ObjectExtensionPropertyInfo(propertyName)); |
|||
configureAction?.Invoke(propertyInfo); |
|||
return propertyInfo; |
|||
} |
|||
|
|||
public static ImmutableList<ObjectExtensionPropertyInfo> GetProperties<TDto>() |
|||
where TDto : IHasExtraProperties |
|||
{ |
|||
var extensionInfo = Extensions.GetOrDefault(typeof(TDto)); |
|||
if (extensionInfo == null) |
|||
{ |
|||
return new ObjectExtensionPropertyInfo[0].ToImmutableList(); //TODO: Return an empty one!
|
|||
} |
|||
|
|||
return extensionInfo.Properties.Values.ToImmutableList(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System.Collections.Generic; |
|||
using System.ComponentModel.DataAnnotations; |
|||
|
|||
namespace Volo.Abp.ObjectExtending |
|||
{ |
|||
public class ObjectExtensionPropertyInfo |
|||
{ |
|||
public string Name { get; } |
|||
|
|||
public List<ValidationAttribute> ValidationAttributes { get; } |
|||
|
|||
public ObjectExtensionPropertyInfo(string name) |
|||
{ |
|||
Name = name; |
|||
ValidationAttributes = new List<ValidationAttribute>(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue