Use Mapster as mapper (#39)

* mapster

* ,

* .

* ...

* .

* .

* .

* .

* .

* .

* int

* .

* ;
This commit is contained in:
Stef Heyenrath
2022-09-05 21:26:02 +02:00
committed by GitHub
parent 4b9b47ba9d
commit 0e57de17c3
39 changed files with 378 additions and 405 deletions
@@ -1,7 +0,0 @@
namespace ProxyInterfaceConsumer
{
// [ProxyInterfaceGenerator.Proxy(typeof(Microsoft.CodeAnalysis.GeneratorExecutionContext))]
public partial interface IGeneratorExecutionContext
{
}
}
@@ -1,9 +0,0 @@
namespace ProxyInterfaceConsumer
{
[ProxyInterfaceGenerator.Proxy(typeof(ProxyInterfaceConsumer.PersonTT<,>))]
public partial interface IPersonTT<T1, T2>
where T1 : struct
where T2 : class, new()
{
}
}
@@ -0,0 +1,64 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
// ReSharper disable once CheckNamespace
namespace ProxyInterfaceConsumerForPnP.Interfaces;
[SuppressMessage("ReSharper", "InconsistentNaming")]
public partial class ClientContextProxy
{
public Task ExecuteQueryRetryAsync(Interfaces.IClientRuntimeContext clientContext, int retryCount = 10, string? userAgent = null)
{
ClientRuntimeContext clientObject_ = Mapster.TypeAdapter.Adapt<ClientRuntimeContext>(clientContext);
return clientObject_.ExecuteQueryRetryAsync(retryCount, userAgent);
}
public void LoadOriginal<T>(T clientObject, params Expression<Func<T, object>>[] retrievals)
where T : ClientObject
{
T clientObject_ = clientObject;
Expression<Func<T, object>>[] retrievals_ = retrievals;
_Instance.Load<T>(clientObject_, retrievals_);
}
public void Load<TSource, TTarget>(Interfaces.IClientObject clientObject, params Expression<Func<TSource, object>>[] retrievals)
where TSource : Interfaces.IClientObject
where TTarget : ClientObject
{
TTarget clientObject_ = Mapster.TypeAdapter.Adapt<TTarget>(clientObject);
Expression<Func<TTarget, object>>[] retrievals_ = retrievals.Select(MapExpression<TSource, TTarget>).ToArray();
_Instance.Load(clientObject_, retrievals_);
}
private static Expression<Func<TTarget, object>> MapExpression<TSource, TTarget>(Expression<Func<TSource, object>> expression)
where TSource : Interfaces.IClientObject
where TTarget : ClientObject
{
var parameterExpression = Expression.Parameter(typeof(TTarget));
Expression memberAccessExpression;
switch (expression.Body)
{
case MemberExpression memberExpression:
memberAccessExpression = Expression.PropertyOrField(parameterExpression, memberExpression.Member.Name);
break;
case UnaryExpression unaryExpression:
var expressionOperand = (MemberExpression)unaryExpression.Operand;
memberAccessExpression = Expression.PropertyOrField(parameterExpression, expressionOperand.Member.Name);
memberAccessExpression = Expression.Convert(memberAccessExpression, typeof(object));
break;
default:
throw new NotSupportedException();
}
return Expression.Lambda<Func<TTarget, object>>(memberAccessExpression, parameterExpression);
}
}
@@ -0,0 +1,14 @@
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using ProxyInterfaceConsumerForPnP.Interfaces;
namespace ProxyInterfaceConsumerForPnP.Implementations;
public static class ClientRuntimeContextExtensions
{
public static Task ExecuteQueryRetryAsync(this IClientRuntimeContext clientContext, int retryCount = 10, string? userAgent = null)
{
ClientRuntimeContext clientObject_ = Mapster.TypeAdapter.Adapt<ClientRuntimeContext>(clientContext);
return clientObject_.ExecuteQueryRetryAsync(retryCount, userAgent);
}
}
@@ -0,0 +1,14 @@
using System;
using System.Linq.Expressions;
using Microsoft.SharePoint.Client;
namespace ProxyInterfaceConsumerForPnP.Interfaces
{
[ProxyInterfaceGenerator.Proxy(typeof(ClientContext))]
public partial interface IClientContext : IClientRuntimeContext
{
void Load<TSource, TTarget>(IClientObject clientObject, params Expression<Func<TSource, object>>[] retrievals)
where TSource : IClientObject
where TTarget : ClientObject;
}
}
@@ -1,4 +1,4 @@
namespace ProxyInterfaceConsumer.PnP
namespace ProxyInterfaceConsumerForPnP.Interfaces
{
[ProxyInterfaceGenerator.Proxy(typeof(Microsoft.SharePoint.Client.ClientObject))]
public partial interface IClientObject
@@ -1,4 +1,4 @@
namespace ProxyInterfaceConsumer.PnP
namespace ProxyInterfaceConsumerForPnP.Interfaces
{
[ProxyInterfaceGenerator.Proxy(typeof(Microsoft.SharePoint.Client.ClientRuntimeContext))]
public partial interface IClientRuntimeContext
@@ -0,0 +1,10 @@
//using System.Collections;
//using System.Linq;
//namespace ProxyInterfaceConsumerForPnP.Interfaces
//{
// [ProxyInterfaceGenerator.Proxy(typeof(Microsoft.SharePoint.Client.ListCollection))]
// public partial interface IProxyListCollection: IClientObject, IEnumerable, IQueryable
// {
// }
//}
@@ -1,4 +1,4 @@
namespace ProxyInterfaceConsumer.PnP
namespace ProxyInterfaceConsumerForPnP.Interfaces
{
[ProxyInterfaceGenerator.Proxy(typeof(Microsoft.SharePoint.Client.SecurableObject))]
public partial interface ISecurableObject : IClientObject
@@ -0,0 +1,7 @@
namespace ProxyInterfaceConsumerForPnP.Interfaces
{
[ProxyInterfaceGenerator.Proxy(typeof(Microsoft.SharePoint.Client.User))]
public partial interface IUser
{
}
}
@@ -1,4 +1,4 @@
namespace ProxyInterfaceConsumer.PnP
namespace ProxyInterfaceConsumerForPnP.Interfaces
{
[ProxyInterfaceGenerator.Proxy(typeof(Microsoft.SharePoint.Client.Web))]
public partial interface IWeb: ISecurableObject
@@ -1,15 +0,0 @@
namespace ProxyInterfaceConsumer
{
public class PersonTT<T1, T2>
where T1 : struct
where T2 : class, new()
{
public T1 TVal1 { get; set; }
public T2 TVal2 { get; set; }
public void Call(int x, T1 t1, T2 t2)
{
}
}
}
@@ -1,188 +0,0 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography.X509Certificates;
using AutoMapper;
using Mapster;
using Microsoft.SharePoint.Client;
namespace ProxyInterfaceConsumer.PnP
{
[ProxyInterfaceGenerator.Proxy(typeof(Microsoft.SharePoint.Client.ClientContext))]
public partial interface IClientContext: IClientRuntimeContext
{
// public virtual void X();
}
}
namespace ProxyInterfaceConsumer.PnP
{
public class CustomResolver : IValueResolver<object, object, object>
{
public object Resolve(object source, object destination, object member, ResolutionContext context)
{
try
{
return member;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
public class MyConverter : ITypeConverter<object, object>
{
public object Convert(object source, object destination, ResolutionContext context)
{
return System.Convert.ToDateTime(source);
}
}
public class MyWebConverter : ITypeConverter<Web, IWeb>
{
public IWeb Convert(Web source, IWeb destination, ResolutionContext context)
{
return new ProxyInterfaceConsumer.PnP.WebProxy(source);
}
}
public partial class ClientContextProxy
{
public void Test()
{
var mapper = new MapperConfiguration(cfg =>
{
//cfg.ForAllMaps((map, expression) =>
//{
// expression.ForAllMembers(configurationExpression =>
// configurationExpression.PreCondition((o, o1, arg3) =>
// {
// return true;
// })
// );
//});
// CreateMap<TSource, TDestination>();
cfg.CreateMap<Microsoft.SharePoint.Client.Web, ProxyInterfaceConsumer.PnP.IWeb>()
.ConstructUsing((instance_841809920, context) =>
{
try
{
var p = new ProxyInterfaceConsumer.PnP.WebProxy(instance_841809920);
return p;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
})
//.ConvertUsing<MyWebConverter>()
.ForAllMembers(opt =>
{
//opt.MapFrom(x =>x , );
//opt.ConvertUsing<MyConverter, IWeb>( x=> x);
opt.PreCondition((src, dest, context) =>
{
try
{
var x = src != null;
return true;
}
catch
{
return false;
}
});
opt.MapAtRuntime();
opt.MapFrom<CustomResolver>();
})
//.ConstructUsing(instance_841809920 => new ProxyInterfaceConsumer.PnP.WebProxy(instance_841809920))
//.ForAllMembers(opt => {
// opt.PreCondition((src, dest, context) =>
// {
// try
// {
// var x = src != null;
// return true;
// }
// catch
// {
// return false;
// }
// });
//})
;
//cfg.CreateMap<ProxyInterfaceConsumer.PnP.IWeb, Microsoft.SharePoint.Client.Web>()
// .ConstructUsing(proxy1898650104 => ((ProxyInterfaceConsumer.PnP.WebProxy)proxy1898650104)._Instance)
// .ForAllMembers(opt => {
// opt.PreCondition((src, dest, context) =>
// {
// try
// {
// var x = src != null;
// return true;
// }
// catch
// {
// return false;
// }
// });
// });
}).CreateMapper();
//var web = _mapper.Map<Web>(Web);
//Load(web, w => w.Lists);
TypeAdapterConfig<Web, IWeb>.NewConfig().ConstructUsing(instance_841809920 => new ProxyInterfaceConsumer.PnP.WebProxy(instance_841809920));
TypeAdapterConfig<IWeb, Web>.NewConfig().MapWith(proxy1898650104 => ((ProxyInterfaceConsumer.PnP.WebProxy)proxy1898650104)._Instance);
var iweb = _Instance.Web.Adapt<IWeb>();
var web = iweb.Adapt<Web>();
//var mapped = mapper.Map<ProxyInterfaceConsumer.PnP.IWeb>(_Instance.Web);
Load3(Web, w => w.Lists);
}
public void LoadOriginal<T>(T clientObject, params System.Linq.Expressions.Expression<System.Func<T, object>>[] retrievals) where T : Microsoft.SharePoint.Client.ClientObject
{
T clientObject_ = clientObject;
System.Linq.Expressions.Expression<System.Func<T, object>>[] retrievals_ = retrievals;
_Instance.Load<T>(clientObject_, retrievals_);
}
//public void Load2(IClientObject clientObject, params Expression<Func<IClientObject, object>>[] retrievals)
//{
// ClientObject clientObject_ = _mapper.Map<ClientObject>(clientObject);
// Expression<Func<ClientObject, object>>[] retrievals_ = _mapper.Map<Expression<Func<ClientObject, object>>[]>(retrievals);
// _Instance.Load(clientObject_, retrievals_);
//}
public void Load3(IWeb clientObject, params System.Linq.Expressions.Expression<System.Func<IWeb, object>>[] retrievals)
{
var clientObject_ = (WebProxy) clientObject;
//Expression<Func<WebProxy, object>>[] retrievals_ = _mapper.Map<Expression<Func<WebProxy, object>>[]>(retrievals);
Load(clientObject_._Instance, null);
}
}
}
@@ -1,14 +1,59 @@
using System;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using ProxyInterfaceConsumer.PnP;
using PnP.Core.Model.SharePoint;
using PnP.Framework;
using ProxyInterfaceConsumerForPnP.Implementations;
using ProxyInterfaceConsumerForPnP.Interfaces;
using IWeb = ProxyInterfaceConsumerForPnP.Interfaces.IWeb;
namespace ProxyInterfaceConsumerForPnP
namespace ProxyInterfaceConsumerForPnP;
public class Program
{
public class Program
public static async Task Main()
{
public static void Main()
try
{
var cp = new ClientContextProxy(new ClientContext("https://heyenrath.nl"));
cp.Test();
var authManager = new AuthenticationManager(
"15b347bf-90a2-4c16-aa76-5a3263476b59",
"Test.pfx",
Environment.GetEnvironmentVariable("Test.pfx_PWD"),
"s7gb6.onmicrosoft.com");
using var clientContext = await authManager.GetContextAsync("https://s7gb6.sharepoint.com/sites/Test");
clientContext.Load(clientContext.Web, p => p.Title);
await clientContext.ExecuteQueryRetryAsync();
Console.WriteLine(clientContext.Web.Title);
IClientContext cp = new ClientContextProxy(clientContext);
cp.Load<IWeb, Web>(cp.Web, w => w.Lists, w => w.Language, w => w.Author);
await cp.ExecuteQueryRetryAsync();
Console.WriteLine(cp.Web.Title + "," + cp.Web.Language + "," + cp.Web.Author.Email);
foreach (var list in cp.Web.Lists)
{
Console.WriteLine(" list : {0}", list.Title);
}
foreach (var list in cp.Web.Lists)
{
cp._Instance.Load(list, l => l.Author.Email);
}
await cp.ExecuteQueryRetryAsync();
Console.WriteLine(new string('-', 80));
foreach (var list in cp.Web.Lists)
{
Console.WriteLine(" list : {0} '{1}'", list.Title, list.Author.Email);
}
}
catch (Exception ex)
{
Console.WriteLine("Error Message: " + ex.Message);
}
}
}
@@ -1,19 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="IPersonTT.cs" />
<Compile Remove="PersonTT.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<!--<PackageReference Include="AutoMapper" Version="10.1.1" />-->
<PackageReference Include="Mapster" Version="7.3.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
<PrivateAssets>all</PrivateAssets>
@@ -22,6 +16,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" />
<PackageReference Include="PnP.Framework" Version="1.10.0" />
<PackageReference Include="TinyMapper" Version="3.0.3" />
</ItemGroup>
<ItemGroup>
@@ -29,21 +24,33 @@
</ItemGroup>
<ItemGroup>
<Compile Update="PnP\ISecurableObject.cs">
<Compile Update="Interfaces\ISecurableObject.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="PnP\IClientObject.cs">
<Compile Update="Interfaces\IClientObject.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="PnP\IWeb.cs">
<Compile Update="Interfaces\IListCollection.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="Interfaces\IUser.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="Interfaces\IWeb.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="PnP\IClientContext.cs">
<Compile Update="Interfaces\IClientContext.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="PnP\IClientRuntimeContext.cs">
<Compile Update="Interfaces\IClientRuntimeContext.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="Test.pfx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>