Ejemplo n.º 1
0
            public static Func <Func <TCommand, TEntity> > Execute()
            {
                var aa = typeof(CommandReciever <>)
                         .MakeGenericType(ServiceCollectionConfigurer.GetRequestType(typeof(TEntity).Name))
                         .GetProperty(nameof(ICommand.RequestPropertyValues));

                //var aa = typeof(CreateCommand<,>).MakeGenericType(
                //    ServiceCollectionConfigurer.GetRequestType(typeof(TEntity).Name),
                //    ServiceCollectionConfigurer.GetResponseType(typeof(TEntity).Name))
                //    .GetProperty(nameof(ICommand.RequestPropertyValues));

                //var getter = GenerateGetterLambda(aa);

                var hmm = Expression.Parameter(typeof(CommandReciever <>)
                                               .MakeGenericType(ServiceCollectionConfigurer.GetRequestType(typeof(TEntity).Name)), "command");

                var prop = Expression.Property(hmm, nameof(ICommand.RequestPropertyValues)).Member;

                var access = Expression.MakeMemberAccess(hmm, prop);

                var loopVar = Expression.Parameter(typeof(KeyValuePair <string, object>), "loopVar");
                var exprs   = new List <MemberAssignment>();

                //var call = Expression.Call(typeof(StaticModifier<TEntity>)
                //    .GetMethod(nameof(AddTest), new[] { typeof(List<MemberAssignment>), typeof(KeyValuePair<string, object>) }), new Expression[] { exprs, loopVar });

                //var block = Expression.Block()

                //var abc = ExpressionBuilder.ForEach(access,loopVar, )

                //var lambda = Expression.Lambda<Func<TCommand, Dictionary<string, object>>>(access, hmm).Compile();

                var lambda = GetRequestPropertyValues();

                return(() =>
                {
                    //         var props = lambda();


                    var exprs = new List <MemberAssignment>();
                    //foreach (var propertyKeyPair in props)
                    //{
                    //    //if (command.RequestForeignEntities.TryGetValue(propertyKeyPair.Key, out var foreignEntity))
                    //    //{
                    //    //    CreateCollection(exprs, propertyKeyPair, foreignEntity);
                    //    //    continue;
                    //    //}

                    //    exprs.Add(
                    //        Expression.Bind(typeof(TEntity).GetMember(propertyKeyPair.Key)[0],
                    //        Expression.Constant(propertyKeyPair.Value)));
                    //}

                    var now = Expression.Constant(DateTime.UtcNow);
                    exprs.AddRange(new[]
                    {
                        Expression.Bind(_idMember, Expression.Constant(Guid.NewGuid())),
                        Expression.Bind(_createdMember, now),
                        Expression.Bind(_updatedMember, now)
                    });

                    var create = Expression.Lambda <Func <TCommand, TEntity> >(
                        Expression.MemberInit(
                            Expression.New(typeof(TEntity)), exprs)).Compile();
                    return create;
                });
            }
Ejemplo n.º 2
0
            private static Func <TCommand, Dictionary <string, object> > GetRequestPropertyValues()
            {
                var hmm = Expression.Parameter(typeof(CommandReciever <>).MakeGenericType(ServiceCollectionConfigurer.GetRequestType(typeof(TEntity).Name)));

                var prop = Expression.Property(hmm, nameof(ICommand.RequestPropertyValues)).Member;

                var access = Expression.MakeMemberAccess(hmm, prop);

                typeof(Type).GetMethod("GetMember", new Type[] { typeof(string[]) });

                var lambda = Expression.Lambda <Func <TCommand, Dictionary <string, object> > >(access, hmm).Compile();

                return(lambda);
            }