Beispiel #1
0
        public Dictionary <Guid, KeyValuePair <PropertyInfo, object> > AddInsertScript(StringBuilder persistScript)
        {
            Dictionary <Guid, KeyValuePair <PropertyInfo, object> > inserts = new Dictionary <Guid, KeyValuePair <PropertyInfo, object> >();

            foreach (object addObj in Changes[ChangeType.Add])
            {
                if (addObj != null)
                {
                    Type addType = addObj.GetType();
                    if (Tables.ContainsKey(addType))
                    {
                        TableData tableData = Tables[addType];
                        Dictionary <string, object> columnValues =
                            tableData.ColumnMappings.Where(kvp => string.Compare(kvp.Key, tableData.KeyColumn, true) != 0)
                            .ToDictionary(cm => cm.Key, cm => cm.Value.GetValue(addObj, null));

                        if (inserts.Count == 0)
                        {
                            persistScript.Append(SQLBuilder.OutputTableScript);
                        }

                        Guid insertKey = Guid.NewGuid();
                        persistScript.AppendLine(SQLBuilder.BuildInsert(tableData.TableName, insertKey.ToString(), tableData.KeyColumn, columnValues));

                        inserts.Add(insertKey, new KeyValuePair <PropertyInfo, object>(tableData.KeyProperty, addObj));

                        // invalidate cache data
                        if (CachedData.ContainsKey(addType))
                        {
                            CachedData.Remove(addType);
                        }
                    }
                }
            }

            if (inserts.Count > 0)
            {
                persistScript.AppendLine(SQLBuilder.OutputSelectScript);
            }

            return(inserts);
        }
Beispiel #2
0
        public void Save()
        {
            using (TransactionScope transaction = new TransactionScope())
            {
                StringBuilder persistScript = new StringBuilder();

                // add insert portion to script
                Dictionary <Guid, KeyValuePair <PropertyInfo, object> > inserts = AddInsertScript(persistScript);

                #region Updates

                foreach (object updateObj in Changes[ChangeType.Update])
                {
                    if (updateObj != null)
                    {
                        Type updateType = updateObj.GetType();
                        if (Tables.ContainsKey(updateType))
                        {
                            TableData tableData = Tables[updateType];

                            // Add Update script building

                            // invalidate cache data
                            if (CachedData.ContainsKey(updateType))
                            {
                                CachedData.Remove(updateType);
                            }
                        }
                    }
                }

                #endregion

                #region Deletes

                foreach (object deleteObj in Changes[ChangeType.Delete])
                {
                    if (deleteObj != null)
                    {
                        Type deleteType = deleteObj.GetType();
                        if (Tables.ContainsKey(deleteType))
                        {
                            TableData tableData = Tables[deleteType];

                            // Add Delete script building

                            // invalidate cache data
                            if (CachedData.ContainsKey(deleteType))
                            {
                                CachedData.Remove(deleteType);
                            }
                        }
                    }
                }

                #endregion

                ExecutePersist(persistScript.ToString(), inserts);

                transaction.Complete();
            }

            // clear out lists of changes
            foreach (List <object> changeList in Changes.Values)
            {
                changeList.Clear();
            }
        }