/// <summary> /// Recupera o enumerador das entidade com base no resultado preparado. /// </summary> /// <param name="prepareResult"></param> /// <param name="sourceContext"></param> /// <returns></returns> private IEnumerator <IEntity> GetEnumerator(PrepareNestedQueriesResult prepareResult, Colosoft.Query.ISourceContext sourceContext) { if (prepareResult.Exceptions.Count > 0) { throw new AggregateException(prepareResult.Exceptions.FirstOrDefault().Message, prepareResult.Exceptions); } foreach (var i in prepareResult.ParentResult) { IEntity entity = null; LazyDataState lazyDataState = new LazyDataState(); prepareResult.EntityLoader.GetLazyData(i, lazyDataState, sourceContext, prepareResult.UiContext, prepareResult.EntityTypeManager, prepareResult.Exceptions); using (var args = new EntityLoaderCreatorArgs <Model>((Model)i.DataModel, i.RecordKey, i.Children, i.Links, i.References, prepareResult.UiContext, prepareResult.EntityTypeManager)) { entity = ((EntityLoader <TEntity1, Model>)prepareResult.EntityLoader).EntityCreator(args); lazyDataState.Entity = entity; if (entity is IConnectedEntity) { ((IConnectedEntity)entity).Connect(sourceContext); } if (entity is IEntityRecordObserver) { ((IEntityRecordObserver)entity).RegisterObserver(i.RecordKey); } if (entity is ILoadableEntity) { ((ILoadableEntity)entity).NotifyLoaded(); } } i.Dispose(); yield return(entity); } }
internal void CreateQueries(Colosoft.Query.Queryable queryable, string parentUidProperty, string uiContext, IEntityTypeManager entityTypeManager, Queue <Exception> exceptions, EntityLoaderExecuteQueryHandler callBack, Colosoft.Query.SubQueryFailedCallBack failedCallBack) { var loader = entityTypeManager.GetLoader(LinkEntityType); var result = new List <EntityLoaderCreatorArgs>(); var query = queryable.BeginSubQuery((sender, e) => { var bindStrategy = loader.GetBindStrategy(); var objectCreator = loader.GetObjectCreator(); var recordKeyFactory = loader.GetRecordKeyFactory(); var dataModelTypeName = Colosoft.Reflection.TypeName.Get(loader.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)); } var entities = new List <IEntity>(); foreach (var i in result) { IEntity entity = null; LazyDataState lazyDataState = new LazyDataState(); loader.GetLazyData(i, lazyDataState, queryable.SourceContext, uiContext, entityTypeManager, exceptions); entity = loader.Create(uiContext, entityTypeManager, i, queryable.SourceContext); lazyDataState.Entity = entity; entities.Add(entity); i.Dispose(); } var entitiesEnumerable = entities; callBack(this, new EntityLoaderExecuteQueryEventArgs(LinkDataModelType, LinkEntityType, e.ReferenceValues, entitiesEnumerable)); }, failedCallBack).From(new Query.EntityInfo(LinkDataModelType.FullName, "t1")); query.Join(ChildDataModelType.FullName, Query.JoinType.Inner, Query.ConditionalContainer.Parse(string.Format("t1.{0} == t2.{1}", ChildPropertyName, ForeignPropertyName)), "t2"); query.Where(string.Format("t2.{0} == ?parentUid", parentUidProperty)); query.Add("?parentUid", new Query.ReferenceParameter(parentUidProperty)); loader.CreateNestedQueries(query, uiContext, entityTypeManager, result, exceptions); query.EndSubQuery(); }
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(); }
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(); }
/// <summary> /// Recupera os dados de carga tardia . /// </summary> /// <param name="creatorArgs">Argumentos para a criação da entidade.</param> /// <param name="state">Estado dos dados de carga tardia.</param> /// <param name="sourceContext">Contexto de origem dos dados.</param> /// <param name="uiContext">Contexto da interface com o usuário.</param> /// <param name="entityTypeManager">Gerenciador dos tipos de entidades.</param> /// <param name="exceptions">Fila dos erros ocorridos.</param> public abstract void GetLazyData(EntityLoaderCreatorArgs creatorArgs, LazyDataState state, Colosoft.Query.ISourceContext sourceContext, string uiContext, IEntityTypeManager entityTypeManager, Queue <Exception> exceptions);