Esempio n. 1
0
        public void RemoveFromCollectionReference <TModel, TItem>(TModel model, TItem item, string collectionName)
        {
            Persistence.ModelMaps.ModelMap modelMap = Persistence.ModelMaps.ModelMap.GetModelMap(typeof(TModel));
            Persistence.ModelMaps.CollectionReferenceMapInfo collReferenceMap = modelMap.GetCollectionReferenceMap(collectionName);
            if (collReferenceMap != null)
            {
                Persistence.ModelMaps.ModelMap itemModelMap = Persistence.ModelMaps.ModelMap.GetModelMap(collReferenceMap.CollectionItemType);
                if (string.IsNullOrEmpty(collReferenceMap.AssociationTableName))
                {
                    typeof(TItem).GetProperty(collReferenceMap.ItemReferenceKey).SetValue(item, null, null);
                    Update(item);
                }
                else
                {
                    object modelKey = typeof(TModel).GetProperty(modelMap.GetKeys()[0]).GetValue(model, null);
                    object itemKey  = typeof(TItem).GetProperty(itemModelMap.GetKeys()[0]).GetValue(item, null);

                    Dictionary <string, object> filters = new Dictionary <string, object>();
                    filters.Add(collReferenceMap.MainColumnKey, modelKey);
                    filters.Add(collReferenceMap.SecundaryColumnKey, itemKey);

                    Persistence.Repository repository = GetDefaultRepository(modelMap);
                    string     sql     = repository.DataBaseConnection.GetDeleteSQL(collReferenceMap.AssociationTableName, filters: filters);
                    IDbCommand command = repository.DataBaseConnection.GetCommand(sql, Persistence.DatabaseCommandType.NONQUERY);
                    repository.DataBaseConnection.ExecuteScalarCommand(command);
                }
            }
        }
Esempio n. 2
0
        public void ClearCollectionReference <TModel>(TModel model, string collectionName)
        {
            //TODO:Tratar exceções
            Persistence.ModelMaps.ModelMap modelMap = Persistence.ModelMaps.ModelMap.GetModelMap(typeof(TModel));
            Persistence.ModelMaps.CollectionReferenceMapInfo collReferenceMap = modelMap.GetCollectionReferenceMap(collectionName);
            if (collReferenceMap != null)
            {
                Persistence.ModelMaps.ModelMap itemModelMap = Persistence.ModelMaps.ModelMap.GetModelMap(collReferenceMap.CollectionItemType);

                object modelKey = typeof(TModel).GetProperty(modelMap.GetKeys()[0]).GetValue(model, null);

                Persistence.Criteria   criteria   = new Persistence.Criteria();
                Persistence.Repository repository = GetDefaultRepository(itemModelMap);

                string table  = string.IsNullOrEmpty(collReferenceMap.AssociationTableName) ? itemModelMap.Table : collReferenceMap.AssociationTableName;
                string column = string.IsNullOrEmpty(collReferenceMap.AssociationTableName) ? itemModelMap.GetColumnName(collReferenceMap.ItemReferenceKey) : collReferenceMap.MainColumnKey;
                string sql    = repository.DataBaseConnection.GetDeleteSQL(table);

                criteria.AddEqualTo(column, modelKey);

                sql = criteria.CompleteSQL(sql);
                IDbCommand command = repository.DataBaseConnection.GetCommand(sql, Persistence.DatabaseCommandType.SQL);
                repository.DataBaseConnection.ExecuteNonQueryCommand(command);

                Type   collectionType = typeof(ModelCollection <>).MakeGenericType(collReferenceMap.CollectionItemType);
                object collection     = Activator.CreateInstance(collectionType);
                typeof(TModel).GetProperty(collectionName).SetValue(model, collection, null);
            }
        }
Esempio n. 3
0
        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) });
            }
        }