RSCG – DynamicsMapper
RSCG – DynamicsMapper
name | DynamicsMapper |
nuget | https://www.nuget.org/packages/YC.DynamicsMapper/ |
link | https://github.com/YonatanCohavi/DynamicsMapper/ |
author | Yonatan Cohavi |
Mapper for Dataverse client – generates also column names from properties
This is how you can use DynamicsMapper .
The code that you start with is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | < Project Sdk = "Microsoft.NET.Sdk" > < PropertyGroup > < OutputType >Exe</ OutputType > < TargetFramework >net7.0</ TargetFramework > < ImplicitUsings >enable</ ImplicitUsings > < Nullable >enable</ Nullable > </ PropertyGroup > < PropertyGroup > < EmitCompilerGeneratedFiles >true</ EmitCompilerGeneratedFiles > < CompilerGeneratedFilesOutputPath >$(BaseIntermediateOutputPath)\GX</ CompilerGeneratedFilesOutputPath > </ PropertyGroup > < ItemGroup > < PackageReference Include = "Microsoft.PowerPlatform.Dataverse.Client" Version = "1.1.14" /> < PackageReference Include = "YC.DynamicsMapper" Version = "1.0.8" /> </ ItemGroup > </ Project > |
The code that you will use is
1 2 3 | using NextGenMapperDemo; var pm = new PersonMapper(); Console.WriteLine(pm.Entityname); |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | using DynamicsMapper.Abstractions; namespace NextGenMapperDemo; [CrmEntity( "person" )] public class Person { [CrmField( "personid" , Mapping = MappingType.PrimaryId)] public Guid ID { get ; set ; } [CrmField( "name" )] public string ? Name { get ; set ; } } |
The code that is generated is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | // <auto-generated /> #nullable enable using Microsoft.Xrm.Sdk; using System.Linq; namespace DynamicsMapper.Extension { public static class EntityExtension { public static Entity? GetAliasedEntity( this Entity entity, string alias) { var attributes = entity.Attributes.Where(e => e.Key.StartsWith(alias)).ToArray(); if (!attributes.Any()) return null ; var aliasEntity = new Entity(); foreach ( var attribute in attributes) { if (!(attribute.Value is AliasedValue aliasedValued)) continue ; if ( string .IsNullOrEmpty(aliasEntity.LogicalName)) aliasEntity.LogicalName = aliasedValued.EntityLogicalName; aliasEntity[aliasedValued.AttributeLogicalName] = aliasedValued.Value; } return aliasEntity; } } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | // <auto-generated /> #nullable enable using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; namespace DynamicsMapper.Mappers { public interface IEntityMapper<T> { public string Entityname { get ; } public ColumnSet Columns { get ; } public T Map(Entity entity); public T? Map(Entity entity, string alias); public Entity Map(T model); } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | // <auto-generated /> #nullable enable using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using DynamicsMapper.Extension; using DynamicsMapper.Mappers; using System; namespace NextGenMapperDemo { public class PersonMapper : IEntityMapper<Person> { private static readonly string [] columns = new [] { "personid" , "name" }; public ColumnSet Columns => new ColumnSet(columns); private const string entityname = "person" ; public string Entityname => entityname; public Entity Map(Person person) { var entity = new Entity(entityname); entity.Id = person.ID; entity[ "name" ] = person.Name; return entity; } public Person? Map(Entity entity, string alias) => InternalMap(entity, alias); public Person Map(Entity entity) => InternalMap(entity)!; private static Person? InternalMap(Entity source, string ? alias = null ) { Entity? entity; if ( string .IsNullOrEmpty(alias)) { entity = source; } else { entity = source.GetAliasedEntity(alias); if (entity is null ) return null ; } if (entity?.LogicalName != entityname) throw new ArgumentException($ "entity LogicalName expected to be {entityname} recived: {entity?.LogicalName}" , "entity" ); var person = new Person(); person.ID = entity.GetAttributeValue<Guid>( "personid" ); person.Name = entity.GetAttributeValue< string ?>( "name" ); return person; } } } |
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/DynamicsMapper
Leave a Reply