Beispiel #1
0
        internal void CreateQueries(Colosoft.Query.Queryable queryable, string uiContext, IEntityLoader parentLoader, IEntityTypeManager entityTypeManager, Queue <Exception> exceptions, EntityLoaderExecuteQueryHandler callBack, Colosoft.Query.SubQueryFailedCallBack failedCallBack)
        {
            if (_entityLoader == null)
            {
                _entityLoader = entityTypeManager.GetLoader(EntityType);
            }
            var result = new List <EntityLoaderCreatorArgs>();
            var query  = queryable.BeginSubQuery((sender, e) => {
                List <IEntity> entities = null;
                try
                {
                    var bindStrategy      = _entityLoader.GetBindStrategy();
                    var objectCreator     = _entityLoader.GetObjectCreator();
                    var recordKeyFactory  = _entityLoader.GetRecordKeyFactory();
                    var dataModelTypeName = Colosoft.Reflection.TypeName.Get(_entityLoader.DataModelType);
                    result.Clear();
                    foreach (var record in e.Result)
                    {
                        var data = objectCreator.Create();
                        if (!bindStrategy.Bind(record, Query.BindStrategyMode.All, ref data).Any())
                        {
                            throw new Exception(string.Format("Not found scheme for bind record data to type '{0}'", data.GetType().FullName));
                        }
                        var recordKey = recordKeyFactory.Create(dataModelTypeName, record);
                        result.Add(new EntityLoaderCreatorArgs((Data.IModel)data, recordKey, new EntityLoaderChildContainer(), new EntityLoaderLinksContainer(), new EntityLoaderReferenceContainer(), uiContext, entityTypeManager));
                    }
                    entities = new List <IEntity>();
                    foreach (var i in result)
                    {
                        IEntity entity = null;
                        LazyDataState lazyDataState = new LazyDataState();
                        _entityLoader.GetLazyData(i, lazyDataState, queryable.SourceContext, uiContext, entityTypeManager, exceptions);
                        entity = _entityLoader.Create(uiContext, entityTypeManager, i, queryable.SourceContext);
                        lazyDataState.Entity = entity;
                        entities.Add(entity);
                        i.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    failedCallBack(sender, new Query.SubQueryCallBackFailedArgs(e.Info, new Query.QueryFailedInfo(Query.QueryFailedReason.Error, ex.Message.GetFormatter(), ex)));
                    return;
                }
                callBack(this, new EntityLoaderExecuteQueryEventArgs(DataModelType, EntityType, e.ReferenceValues, entities));
            }, failedCallBack).From(new Query.EntityInfo(DataModelType.FullName));

            if (ExecutePredicate != null)
            {
                query.ExecutePredicate = (Colosoft.Query.QueryExecutePredicate)ExecutePredicate.Clone();
            }
            string conditionalExpression = null;

            if (parentLoader.HasUid)
            {
                query.Add("?parentUid", new Query.ReferenceParameter(parentLoader.UidPropertyName));
                conditionalExpression = string.Format("{0} == ?parentUid", _foreignPropertyName);
            }
            if (Conditional != null)
            {
                if (!string.IsNullOrEmpty(conditionalExpression))
                {
                    conditionalExpression = string.Format("{0} && ({1})", conditionalExpression, this.Conditional.Expression);
                }
                else
                {
                    conditionalExpression = this.Conditional.Expression;
                }
                foreach (var i in this.Conditional.Parameters)
                {
                    query.Add(i.Name, i.Value);
                }
            }
            if (string.IsNullOrEmpty(conditionalExpression))
            {
                throw new InvalidOperationException("Not support child, because not found conditional expression.");
            }
            query.Where(conditionalExpression);
            if (_entityLoader != parentLoader)
            {
                _entityLoader.CreateNestedQueries(query, uiContext, entityTypeManager, result, exceptions);
            }
            query.EndSubQuery();
        }
Beispiel #2
0
        internal void CreateQueries(Colosoft.Query.Queryable queryable, string uiContext, IEntityTypeManager entityTypeManager, Queue <Exception> exceptions, EntityLoaderExecuteQueryHandler callBack, Colosoft.Query.SubQueryFailedCallBack failedCallBack)
        {
            if (_entityLoader == null)
            {
                _entityLoader = entityTypeManager.GetLoader(EntityType);
            }
            var result = new List <EntityLoaderCreatorArgs>();
            var query  = queryable.BeginSubQuery((sender, e) => {
                List <IEntity> entities = null;
                try
                {
                    var bindStrategy     = _entityLoader.GetBindStrategy();
                    var objectCreator    = _entityLoader.GetObjectCreator();
                    var recordKeyFactory = _entityLoader.GetRecordKeyFactory();
                    Colosoft.Reflection.TypeName dataModelTypeName = null;
                    try
                    {
                        dataModelTypeName = Colosoft.Reflection.TypeName.Get(_entityLoader.DataModelType);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("An error ocurred when get DataModelType", ex);
                    }
                    result.Clear();
                    foreach (var record in e.Result)
                    {
                        var data       = objectCreator.Create();
                        var bindResult = bindStrategy.Bind(record, Query.BindStrategyMode.All, ref data);
                        if (!bindResult.Any())
                        {
                            throw new Exception(string.Format("Not found scheme for bind record data to type '{0}'", data.GetType().FullName));
                        }
                        Query.RecordKey recordKey = null;
                        try
                        {
                            recordKey = recordKeyFactory.Create(dataModelTypeName, record);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("An error ocurred when create record key", ex);
                        }
                        result.Add(new EntityLoaderCreatorArgs((Data.IModel)data, recordKey, new EntityLoaderChildContainer(), new EntityLoaderLinksContainer(), new EntityLoaderReferenceContainer(), uiContext, entityTypeManager));
                    }
                    entities = new List <IEntity>();
                    foreach (var i in result)
                    {
                        try
                        {
                            IEntity entity = null;
                            LazyDataState lazyDataState = new LazyDataState();
                            _entityLoader.GetLazyData(i, lazyDataState, queryable.SourceContext, uiContext, entityTypeManager, exceptions);
                            entity = _entityLoader.Create(uiContext, entityTypeManager, i, queryable.SourceContext);
                            lazyDataState.Entity = entity;
                            entities.Add(entity);
                            i.Dispose();
                        }
                        catch (Exception ex)
                        {
                            throw new AggregateException(ex.Message, exceptions);
                        }
                    }
                }
                catch (Exception ex)
                {
                    failedCallBack(sender, new Query.SubQueryCallBackFailedArgs(e.Info, new Query.QueryFailedInfo(Query.QueryFailedReason.Error, ex.Message.GetFormatter(), ex)));
                    return;
                }
                callBack(this, new EntityLoaderExecuteQueryEventArgs(DataModelType, EntityType, e.ReferenceValues, entities));
                entities.Clear();
            }, failedCallBack).From(new Query.EntityInfo(DataModelType.FullName));
            var conditional = this.GetConditional();

            if (conditional == null)
            {
                throw new InvalidOperationException("Conditional not found");
            }
            query.Where(conditional.Expression);
            foreach (var i in conditional.Parameters)
            {
                query.Add(i.Name, i.Value);
            }
            _entityLoader.CreateNestedQueries(query, uiContext, entityTypeManager, result, exceptions);
            query.EndSubQuery();
        }