public ModelCollection <IModel> GetNonTypedCollection() { ModelCollection <IModel> copy = new ModelCollection <IModel>(); ForEach(e => copy.Add(e)); copy.TotalRows = TotalRows; return(copy); }
public void FillCollectionReference <TModel>(TModel model, string collectionName, Type collectionItemType = null) where TModel : class, IModel { //TODO:Tratar exceções Persistence.ModelMaps.ModelMap modelMap = Persistence.ModelMaps.ModelMap.GetModelMap(typeof(TModel)); Persistence.ModelMaps.CollectionReferenceMapInfo collReferenceMap = modelMap.GetCollectionReferenceMap(collectionName); if (collReferenceMap != null) { if (collectionItemType == null) { collectionItemType = collReferenceMap.CollectionItemType; } Persistence.ModelMaps.ModelMap itemModellMap = Persistence.ModelMaps.ModelMap.GetModelMap(collectionItemType); object modelKey = typeof(TModel).GetProperty(modelMap.GetKeys()[0]).GetValue(model, null); Persistence.Criteria criteria = new Persistence.Criteria(); if (string.IsNullOrEmpty(collReferenceMap.AssociationTableName)) { criteria.AddEqualTo("\"" + itemModellMap.GetColumnName(collReferenceMap.ItemReferenceKey) + "\"", modelKey); } else { criteria.AddInnerJoin(collReferenceMap.AssociationTableName, "\"" + itemModellMap.Table + "\".\"" + itemModellMap.GetColumnName(itemModellMap.GetKeys()[0]) + "\"", "\"" + collReferenceMap.AssociationTableName + "\".\"" + collReferenceMap.SecundaryColumnKey + "\""); criteria.AddEqualTo("\"" + collReferenceMap.AssociationTableName + "\".\"" + collReferenceMap.MainColumnKey + "\"", modelKey); } Persistence.Repository repository = GetDefaultRepository(itemModellMap); string sql = repository.DataBaseConnection.GetSelectSQL(itemModellMap.Table); sql = criteria.CompleteSQL(sql); IDbCommand command = repository.DataBaseConnection.GetCommand(sql, Persistence.DatabaseCommandType.SQL); //Type collectionType = typeof(ModelCollection<>).MakeGenericType(collectionItemType); //object collection = Activator.CreateInstance(collectionType); ModelCollection <IModel> result = repository.SearchModels <IModel>(command, collectionItemType); typeof(TModel).GetProperty(collectionName).SetValue(model, Activator.CreateInstance(typeof(TModel).GetProperty(collectionName).PropertyType)); result.ForEach(item => typeof(TModel).GetProperty(collectionName).PropertyType.GetMethod("Add").Invoke(typeof(TModel).GetProperty(collectionName).GetValue(model, null), new object[] { item })); //typeof(TModel).GetProperty(collectionName).PropertyType.GetMethod("Add").Invoke(typeof(TModel).GetProperty(collectionName).GetValue(model, null), new object[] { repository.SearchModels<object>(command, collectionItemType) }); } }
public ModelCollection <TModel> Search <TModel>(Dictionary <string, object> parameters = null, Type modelType = null, string procedureKey = null, bool forceNonProcedureCommand = false) where TModel : class, IModel { //TODO:Tratar exceções modelType = (modelType == null) ? typeof(TModel) : modelType; Persistence.ModelMaps.ModelMap modelMap = Persistence.ModelMaps.ModelMap.GetModelMap(modelType); //Verifica se a entidade foi configurada com uma procedure padronizada de busca procedureKey = string.IsNullOrEmpty(procedureKey) ? DataAnnotations.ERBridge.DefaultProceduresKeys.SEARCH.ToString() : procedureKey; Persistence.ModelMaps.ProcedureMapInfo procedureMap = modelMap.GetProcedureMap(procedureKey); IDbCommand command = (procedureMap == null || forceNonProcedureCommand) ? CreateSQLSelectCommand(modelMap, parameters) : CreateProcedureCommand(modelMap, parameters, procedureKey); Persistence.Repository repository = GetDefaultRepository(modelMap); ModelCollection <TModel> result = repository.SearchModels <TModel>(command, modelType); if (procedureMap != null && procedureMap.Parameters.ContainsKey("TotalRows")) { Dictionary <string, object> returnedValues = repository.ReadOutputParameters(command); if (returnedValues.ContainsKey(procedureMap.Parameters["TotalRows"].Name)) { result.TotalRows = int.Parse(returnedValues[procedureMap.Parameters["TotalRows"].Name].ToString()); } } return(result); }