public void Should_Deserialize_AnonymousType()
        {
            var originalObject = new AnonymousTypeObject(new { Id = 1, Name = "John Doe" });

            // serialize to binary data
            var bytes = originalObject.Serialize(SerializerOptions.EmbedTypes); // we need to embed types here as we don't know the inner type

            // restore the object from binary data
            var restoredObject = bytes.Deserialize <AnonymousTypeObject>();

            Assert.AreEqual(originalObject.AnonymousType, restoredObject.AnonymousType);
        }
Beispiel #2
0
        public List <TView> FindAll <TEntity, TView>(Func <TEntity, TView> select, ICommandTunnel tunnel = null)
        {
            if (select == null)
            {
                return(this.FindAll <TEntity, TView>(tunnel));
            }
            var atObj    = new AnonymousTypeObject <TView>();
            var command  = this._engine.Provider.SqlFactory.CreateQueryCommand(TypeMapper.Instance <TEntity> .Mapper, atObj.Fields, this._where, 0, tunnel);
            var entities = this._engine.Execute(command).ToEntities();

            return((from item in entities select atObj.Create(item as DynamicEntityValue)).ToList());
        }
Beispiel #3
0
        public PageData <TView> FindAll <TEntity, TView>(Func <TEntity, TView> select, IPagination page, ICommandTunnel tunnel = null)
        {
            if (select == null)
            {
                return(this.FindAll <TEntity, TView>(page, tunnel));
            }
            var atObj    = new AnonymousTypeObject <TView>();
            var command  = this._engine.Provider.SqlFactory.CreateQueryCommand(TypeMapper.Instance <TEntity> .Mapper, atObj.Fields, this._where, 0, tunnel);
            var pageData = this._engine.Execute(command).ToEntities(page);

            return(new PageData <TView>()
            {
                Rows = (from item in pageData.Rows select atObj.Create(item as DynamicEntityValue)).ToArray(),
                Total = pageData.Total
            });
        }
Beispiel #4
0
        public TView FindOne <TEntity, TView>(Func <TEntity, TView> select, ICommandTunnel tunnel = null)
        {
            if (select == null)
            {
                return(this.FindOne <TEntity, TView>(tunnel));
            }
            var atObj   = new AnonymousTypeObject <TView>();
            var command = this._engine.Provider.SqlFactory.CreateQueryCommand(TypeMapper.Instance <TEntity> .Mapper, atObj.Fields, this._where, 1, tunnel);
            var entity  = this._engine.Execute(command).ToEntity();

            if (entity == null)
            {
                return(default(TView));
            }
            return(atObj.Create(entity as DynamicEntityValue));
        }
        public Task <TView> FindOneAsync <TEntity, TView>(Func <TEntity, TView> select, ICommandTunnel tunnel = null)
        {
            if (select == null)
            {
                return(this.FindOneAsync <TEntity, TView>(tunnel));
            }

            var atObj   = new AnonymousTypeObject <TView>();
            var command = this._engine.Provider.SqlFactory.CreateQueryCommand(TypeMapper.Instance <TEntity> .Mapper, atObj.Fields, this._where, 1, tunnel);

            return(this._engine.Execute(command).ToEntityAsync().ContinueWith(t =>
            {
                if (t.Result == null)
                {
                    return default(TView);
                }
                return atObj.Create(t.Result as DynamicEntityValue);
            }));
        }
Beispiel #6
0
        IExecutorMetadata <TCommand> IExecutorFactory.Create <TCommand>(TCommand command)
        {
            var executor = Executors.TryGetValue(typeof(TCommand)) as IExecutorMetadata <TCommand>;

            if (executor == null)
            {
                if (_queues.Count == 0)
                {
                    throw new NotSupportedException($"命令{typeof(TCommand).FullName}没有模拟执行器。");
                }
                var handler = _queues.Dequeue();
                if (handler != null)
                {
                    return(new ExecutorMetadata <TCommand>(new MockExecutor <TCommand>((cont, cmd) =>
                    {
                        var result = handler(cmd);
                        if (result == null)
                        {
                            return;
                        }
                        var type = cmd.GetType();
                        foreach (var interfaceType in type.GetInterfaces())
                        {
                            if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(ICommand <>))
                            {
                                var resultType = interfaceType.GetGenericArguments()[0];
                                if (resultType.IsAnonymous())
                                {
                                    result = new AnonymousTypeObject(resultType).CreateAny(result);
                                }
                                break;
                            }
                        }
                        TypeMapper.Create(type)["Result"].SetValue(cmd, result);
                    })));
                }
                throw new NotSupportedException($"没有模拟实现命令 { typeof(TCommand).FullName } 的执行器。");
            }
            return(executor);
        }