Beispiel #1
0
        public virtual object GetObjectByKey(string keyPropertyName, object keyValue, Type type, bool ignoreObjectNotFound)
        {
            type = AssemblyManager.GetBaseType(type);
            object obj;

            obj = this.Context.AssemblyManager.CreateInstance(type);
            ObjectCancelEventArgs e = new ObjectCancelEventArgs(obj);

            this.Context.EventManager.OnGettingObject(this, e);
            if (e.Cancel)
            {
                return(null);
            }
            this.Context.ObjectManager.SetPropertyValue(obj, keyPropertyName, keyValue);
            this.Context.PersistenceEngine.LoadObjectByKey(ref obj, keyPropertyName, keyValue);
            if (obj == null)
            {
                if (ignoreObjectNotFound == false)
                {
                    throw new ObjectNotFoundException("Object not found!");                     // do not localize
                }
            }
            else
            {
                IList        idKeyParts = this.Context.ObjectManager.GetObjectIdentityKeyParts(obj);
                KeyStruct    key        = GetKey(type, idKeyParts);
                IObjectCache cache      = GetObjectCache();
                obj = cache.LoadedObjects[key];
                ObjectEventArgs e2 = new ObjectEventArgs(obj);
                this.Context.EventManager.OnGotObject(this, e2);
            }
            return(obj);
        }
Beispiel #2
0
        public virtual object GetObject(object identity, Type type, bool lazy, bool ignoreObjectNotFound)
        {
            type = AssemblyManager.GetBaseType(type);
            KeyStruct             key = GetKey(type, identity);
            object                obj;
            ObjectCancelEventArgs e;

            IObjectCache cache = GetObjectCache();

            obj = cache.LoadedObjects[key];
            if (obj != null)
            {
                e = new ObjectCancelEventArgs(obj);
                this.Context.EventManager.OnGettingObject(this, e);
                if (e.Cancel)
                {
                    return(null);
                }
            }
            else
            {
                obj = cache.UnloadedObjects[key];
                if (obj != null)
                {
                    e = new ObjectCancelEventArgs(obj);
                    this.Context.EventManager.OnGettingObject(this, e);
                    if (e.Cancel)
                    {
                        return(null);
                    }
                }
                else
                {
                    obj = this.Context.AssemblyManager.CreateInstance(type);
                    this.Context.ObjectManager.SetObjectIdentity(obj, key);
                    e = new ObjectCancelEventArgs(obj);
                    this.Context.EventManager.OnGettingObject(this, e);
                    if (e.Cancel)
                    {
                        return(null);
                    }
                    if (lazy)
                    {
                        RegisterLazyLoadedObject(obj);
                        cache.UnloadedObjects[key] = obj;
                    }
                    else
                    {
                        string strIdentity = ToStringIdentity(identity);
                        LoadObject(strIdentity, ref obj, ignoreObjectNotFound, type, key);
                    }
                }
            }
            ObjectEventArgs e2 = new ObjectEventArgs(obj);

            this.Context.EventManager.OnGotObject(this, e2);
            return(obj);
        }
Beispiel #3
0
        public virtual object GetObjectByKey(string keyPropertyName, object keyValue, Type type, bool ignoreObjectNotFound)
        {
            type = this.Context.AssemblyManager.GetType(type);
            object obj;
            string key;
            string identity;

            obj = this.Context.AssemblyManager.CreateInstance(type);
            ObjectCancelEventArgs e = new ObjectCancelEventArgs(obj);

            this.Context.EventManager.OnGettingObject(this, e);
            if (e.Cancel)
            {
                return(null);
            }
            this.Context.ObjectManager.SetPropertyValue(obj, keyPropertyName, keyValue);
            //this.Context.SqlEngineManager.LoadObjectByKey(obj, keyPropertyName, keyValue);
            this.Context.PersistenceEngine.LoadObjectByKey(ref obj, keyPropertyName, keyValue);
            if (obj == null)
            {
                if (ignoreObjectNotFound == false)
                {
                    throw new ObjectNotFoundException("Object not found!");                     // do not localize
                }
            }
            else
            {
                identity = this.Context.ObjectManager.GetObjectIdentity(obj);
//				key = type.ToString() + "." + identity;
                key = GetKey(type, identity);
                IObjectCache cache = GetObjectCache();
                obj = cache.LoadedObjects[key];
//				if (m_hashLoadedObjects.ContainsKey(key))
//				{
//					obj = m_hashLoadedObjects[key];
//				}
                ObjectEventArgs e2 = new ObjectEventArgs(obj);
                this.Context.EventManager.OnGotObject(this, e2);
            }
            return(obj);
        }
Beispiel #4
0
        public void NotifyInstantiatingObject(object obj, ref bool cancel)
        {
            if (this.isDisposed)
            {
                return;
            }
            //this.Context.PersistenceManager.SetupObject(obj);
            this.Context.PersistenceManager.InitializeObject(obj);

            if (notification == Notification.Disabled)
            {
                return;
            }
            ObjectCancelEventArgs e = new ObjectCancelEventArgs(obj);

            this.Context.EventManager.OnInstantiatingObject(this, e);
            if (e.Cancel)
            {
                cancel = true;
                return;
            }
        }
Beispiel #5
0
 public virtual void OnRemovingObject(object sender, ObjectCancelEventArgs e)
 {
 }
Beispiel #6
0
 public virtual void OnInsertingObject(object sender, ObjectCancelEventArgs e)
 {
 }
Beispiel #7
0
 public virtual void OnCommittingObject(object sender, ObjectCancelEventArgs e)
 {
 }
        public override void InsertObject(object obj, IList stillDirty)
        {
            IList        parameters    = new ArrayList();
            ArrayList    propertyNames = new ArrayList();
            IPropertyMap propertyMap;

            ArrayList             nonPrimaryPropertyMaps = new ArrayList();
            ArrayList             collectionPropertyMaps = new ArrayList();
            ArrayList             sqlStatements          = new ArrayList();
            int                   rowsAffected;
            object                autoID;
            IPropertyMap          autoProp;
            IColumnMap            autoPropCol;
            string                autoPropName;
            string                prevId;
            bool                  originalKeepOpen;
            IContext              ctx = SqlEngineManager.Context;
            ObjectCancelEventArgs e   = new ObjectCancelEventArgs(obj);

            ctx.EventManager.OnInsertingObject(this, e);
            if (e.Cancel)
            {
                return;
            }
            IObjectManager om        = ctx.ObjectManager;
            IListManager   lm        = ctx.ListManager;
            IClassMap      classMap  = ctx.DomainMap.MustGetClassMap(obj.GetType());
            string         sqlSelect = "";
            string         sql;

            if (classMap.HasSingleIdAutoIncreaser())
            {
                sql = GetInsertStatement(obj, propertyNames, stillDirty, nonPrimaryPropertyMaps, collectionPropertyMaps, ref sqlSelect, parameters);
            }
            else
            {
                sql = GetInsertStatement(obj, propertyNames, stillDirty, nonPrimaryPropertyMaps, collectionPropertyMaps, parameters);
            }
            IDataSource ds = ctx.DataSourceManager.GetDataSource(obj);

            if (classMap.HasSingleIdAutoIncreaser())
            {
                originalKeepOpen      = ds.KeepConnectionOpen;
                ds.KeepConnectionOpen = true;
                rowsAffected          = ctx.SqlExecutor.ExecuteNonQuery(sql, ds, parameters);
                if (!(rowsAffected == 1))
                {
                    ds.KeepConnectionOpen = originalKeepOpen;
                    throw new NPersistException("An exception occurred when inserting the row for a new object into the data source. Exactly one row should have been affected by the insert operation. Instead " + rowsAffected + " rows were affected!");                     // do not localize
                }
                object[,] result = (object[, ])ctx.SqlExecutor.ExecuteArray(sqlSelect, ds, parameters);
                if (Util.IsArray(result))
                {
                    prevId       = om.GetObjectIdentity(obj);
                    autoProp     = classMap.GetAutoIncreasingIdentityPropertyMap();
                    autoPropName = autoProp.Name;
                    autoPropCol  = autoProp.GetColumnMap();
                    if (autoPropCol.DataType == DbType.Int64)
                    {
                        autoID = Convert.ToInt64(result[0, 0]);
                    }
                    else if (autoPropCol.DataType == DbType.Int16)
                    {
                        autoID = Convert.ToInt16(result[0, 0]);
                    }
                    else
                    {
                        autoID = Convert.ToInt32(result[0, 0]);
                    }
                    om.SetPropertyValue(obj, autoPropName, autoID);
                    //om.SetOriginalPropertyValue(obj, autoPropName, autoID);
                    om.SetNullValueStatus(obj, autoPropName, false);
                    ctx.IdentityMap.UpdateIdentity(obj, prevId);
                }
                else
                {
                    ds.KeepConnectionOpen = originalKeepOpen;
                    throw new FailedFetchingDbGeneratedValueException("Could not find auto-increasing ID for new object!");                     // do not localize
                }
                ds.KeepConnectionOpen = originalKeepOpen;
            }
            else
            {
                rowsAffected = ctx.SqlExecutor.ExecuteNonQuery(sql, ds, parameters);
                if (!(rowsAffected == 1))
                {
                    throw new RowNotInsertedException("A new row was not inserted in the data source for a new object.");                     // do not localize
                }
            }
//			foreach (string propName in propertyNames)
//			{
//				om.SetOriginalPropertyValue(obj, propName, om.GetPropertyValue(obj, propName));
//			}
            InsertNonPrimaryProperties(obj, nonPrimaryPropertyMaps, stillDirty);
            foreach (IPropertyMap iPpropertyMap in collectionPropertyMaps)
            {
                propertyMap = iPpropertyMap;
                sqlStatements.Clear();
                GetInsertCollectionPropertyStatements(obj, propertyMap, sqlStatements, stillDirty);
                ds = ctx.DataSourceManager.GetDataSource(obj, propertyMap.Name);
                foreach (SqlStatementAndDbParameters sqlStatementAndDbParameters in sqlStatements)
                {
                    sql          = sqlStatementAndDbParameters.SqlStatement;
                    rowsAffected = ctx.SqlExecutor.ExecuteNonQuery(sql, ds, sqlStatementAndDbParameters.DbParameters);
                    if (!(rowsAffected == 1))
                    {
                        throw new RowNotInsertedException("A new row was not inserted in the data source for a collection property of a new object.");                         // do not localize
                    }
                }
            }
            ctx.InverseManager.NotifyCreate(obj);
            ObjectEventArgs e2 = new ObjectEventArgs(obj);

            ctx.EventManager.OnInsertedObject(this, e2);
        }