public object LoadObject(string type, object identity, string domainKey)
        {
//			if (useCompression &&  this.compressor != null)
//			{
//				type = this.compressor.Decompress(type);
//				identity = this.compressor.Decompress((string) identity);
//				domainKey = this.compressor.Decompress(domainKey);
//			}

            IContext  ctx      = contextFactory.GetContext(domainKey);
            IClassMap classMap = ctx.DomainMap.MustGetClassMap(type);
            Type      realType = ctx.AssemblyManager.MustGetTypeFromClassMap(classMap);
            object    obj      = ctx.GetObjectById(identity, realType);
            IMarshalingTransformer transformer = new MarshalingTransformer(ctx);
            MarshalObject          mo          = transformer.FromObject(obj);
            object serialized = formater.Serialize(mo);

            ctx.Dispose();

            if (useCompression && this.compressor != null)
            {
                return(this.compressor.Compress((string)serialized));
            }
            else
            {
                return(serialized);
            }
        }
        public object LoadObjects(object query, string domainKey)
        {
            if (useCompression && this.compressor != null)
            {
                query = this.compressor.Decompress((string)query);
                //domainKey = this.compressor.Decompress(domainKey);
            }

            IContext ctx = contextFactory.GetContext(domainKey);
            IMarshalingTransformer transformer = new MarshalingTransformer(ctx);
            MarshalQuery           mq          = (MarshalQuery)formater.Deserialize(query, typeof(MarshalQuery));
            IQuery            queryObject      = transformer.ToQuery(mq);
            IList             objects          = ctx.GetObjectsByQuery(queryObject);
            MarshalObjectList mol        = transformer.FromObjectList(objects);
            string            serialized = (string)formater.Serialize(mol);

            ctx.Dispose();
            if (useCompression && this.compressor != null)
            {
                return(this.compressor.Compress(serialized));
            }
            else
            {
                return(serialized);
            }
        }
        //Refresh issues!!
        public override IList LoadObjects(IQuery query, IList listToFill)
        {
            if (this.url.Length < 1)
            {
                throw new NPersistException("You must specify an url to your NPersist Web Service in your WebServiceRemotingEngine!");
            }
            RemotingService        rs          = new RemotingService(this.Context, url);
            IMarshalingTransformer transformer = new MarshalingTransformer(Context);
            MarshalQuery           mq          = transformer.FromQuery(query);
            string xmlQuery = (string)Formatter.Serialize(mq);

            if (useCompression && this.compressor != null)
            {
                xmlQuery = this.compressor.Compress(xmlQuery);
            }

            bool doUseCompression = this.useCompression;

            if (this.compressor == null)
            {
                doUseCompression = false;
            }

            string result = rs.LoadObjects(xmlQuery, this.domainKey, doUseCompression);

            if (useCompression && this.compressor != null)
            {
                result = this.compressor.Decompress(result);
            }

            MarshalObjectList mol = (MarshalObjectList)Formatter.Deserialize(result, typeof(MarshalObjectList));

            transformer.ToObjectList(mol, query.RefreshBehavior, listToFill);
            return(listToFill);
        }
        //Should have no refresh issues
        public override void LoadObject(ref object obj)
        {
            if (this.url.Length < 1)
            {
                throw new NPersistException("You must specify an url to your NPersist Web Service in your WebServiceRemotingEngine!");
            }
            RemotingService rs       = new RemotingService(this.Context, url);
            IClassMap       classMap = Context.DomainMap.MustGetClassMap(obj.GetType());
            string          id       = Context.ObjectManager.GetObjectIdentity(obj);

            bool doUseCompression = this.useCompression;

            if (this.compressor == null)
            {
                doUseCompression = false;
            }

            string result = rs.LoadObject(classMap.GetName(), id, this.domainKey, doUseCompression);

            if (useCompression && this.compressor != null)
            {
                result = this.compressor.Decompress(result);
            }

            MarshalObject          mo          = (MarshalObject)Formatter.Deserialize(result, typeof(MarshalObject));
            IMarshalingTransformer transformer = new MarshalingTransformer(Context);

            Context.IdentityMap.RegisterLoadedObject(obj);
            transformer.ToObject(mo, ref obj);
        }
        public void UpdateSourceAssigned(MarshalObjectList mol)
        {
            IObjectManager         om          = Context.ObjectManager;
            IMarshalingTransformer transformer = new MarshalingTransformer(Context);
            IClassMap       classMap;
            MarshalProperty mp;
            object          obj;
            object          newValue;

            foreach (MarshalObject mo in mol.Objects)
            {
                if (mo.TempId.Length > 0)
                {
                    obj = GetInsertedObjectWithTemporaryIdentity(mo.TempId);
                    if (obj != null)
                    {
                        classMap = Context.DomainMap.MustGetClassMap(obj.GetType());
                        foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps())
                        {
                            if (propertyMap.GetIsAssignedBySource())
                            {
                                mp = mo.GetProperty(propertyMap.Name);
                                if (mp != null)
                                {
                                    newValue = transformer.ToPropertyValue(obj, mp.Value, mp, propertyMap);
                                    om.SetPropertyValue(obj, propertyMap.Name, newValue);
                                    om.SetOriginalPropertyValue(obj, propertyMap.Name, newValue);
                                    om.SetNullValueStatus(obj, propertyMap.Name, false);
                                }
                            }
                        }
                        Context.IdentityMap.UpdateIdentity(obj, mo.TempId);
                    }
                }
                else
                {
                    classMap = Context.DomainMap.MustGetClassMap(mo.Type);
                    if (classMap.HasAssignedBySource())
                    {
                        obj = GetObjectByMarshalObject(transformer, mo, classMap);
                        foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps())
                        {
                            if (propertyMap.GetIsAssignedBySource())
                            {
                                mp = mo.GetProperty(propertyMap.Name);
                                if (mp != null)
                                {
                                    newValue = transformer.ToPropertyValue(obj, mp.Value, mp, propertyMap);
                                    om.SetPropertyValue(obj, propertyMap.Name, newValue);
                                    om.SetOriginalPropertyValue(obj, propertyMap.Name, newValue);
                                    om.SetNullValueStatus(obj, propertyMap.Name, false);
                                }
                            }
                        }
                    }
                }
            }
        }
        public virtual MarshalUnitOfWork GetUnitOfWork()
        {
            MarshalUnitOfWork      muow        = new MarshalUnitOfWork();
            IMarshalingTransformer transformer = new MarshalingTransformer(Context);
            MarshalObject          mo;

            foreach (object obj in listInsert)
            {
                mo = transformer.FromObject(obj, true);
                muow.InsertObjects.Add(mo);
            }
            foreach (object obj in listUpdate)
            {
                mo = transformer.FromObject(obj);
                muow.UpdateObjects.Add(mo);
            }
            foreach (object obj in listRemove)
            {
                mo = transformer.FromObject(obj);
                muow.RemoveObjects.Add(mo);
            }
            return(muow);
        }
        public object LoadProperty(object obj, string propertyName, string domainKey)
        {
            if (useCompression && this.compressor != null)
            {
                obj = this.compressor.Decompress((string)obj);
                //propertyName = this.compressor.Decompress(propertyName);
                //domainKey = this.compressor.Decompress(domainKey);
            }

            IContext               ctx         = contextFactory.GetContext(domainKey);
            IObjectManager         om          = ctx.ObjectManager;
            IMarshalingTransformer transformer = new MarshalingTransformer(ctx);
            MarshalReference       mr          = (MarshalReference)formater.Deserialize(obj, typeof(MarshalReference));
            string       identity    = transformer.GetIdentity(mr, mr.Value);
            IClassMap    classMap    = ctx.DomainMap.MustGetClassMap(mr.Type);
            IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName);
            Type         realType    = ctx.AssemblyManager.MustGetTypeFromClassMap(classMap);
            //object realObj = ctx.GetObjectById(identity, realType);
            object realObj = ctx.GetObjectById(identity, realType, true);

            ctx.LoadProperty(realObj, propertyName);
            object serialized = null;

            if (propertyMap.IsCollection)
            {
                if (propertyMap.ReferenceType == ReferenceType.None)
                {
//					IList list = (IList) om.GetPropertyValue(realObj, propertyName);
//					MarshalList ml = transformer.FromList(list) ;
//					serialized = formater.Serialize(ml);
                }
                else
                {
                    //TODO: fix transformer.FromReferenceList
                    //Even better: Add MarshalProperty.Object, OriginalObject, List, OriginalList, ReferenceList and OriginalReferenceList!
                    IList             list = (IList)om.GetPropertyValue(realObj, propertyName);
                    MarshalObjectList mol  = transformer.FromObjectList(list);
                    serialized = formater.Serialize(mol);
                }
            }
            else
            {
                if (propertyMap.ReferenceType == ReferenceType.None)
                {
                    MarshalProperty mp = transformer.FromProperty(obj, propertyMap);
                    serialized = formater.Serialize(mp);
                }
                else
                {
                    object value = om.GetPropertyValue(realObj, propertyName);
                    if (value != null)
                    {
                        ObjectStatus objectStatus = ctx.GetObjectStatus(value);
                        if (objectStatus == ObjectStatus.NotLoaded)
                        {
                            ctx.PersistenceEngine.LoadObject(ref value);
                        }

                        if (value != null)
                        {
                            MarshalObject mo = transformer.FromObject(value);
                            serialized = formater.Serialize(mo);
                        }
                    }
                }
            }
            if (serialized == null)
            {
                serialized = "";
            }

            ctx.Dispose();
            if (useCompression && this.compressor != null)
            {
                return(this.compressor.Compress((string)serialized));
            }
            else
            {
                return(serialized);
            }
        }
        public object CommitUnitOfWork(object obj, string domainKey)
        {
            if (useCompression && this.compressor != null)
            {
                obj = this.compressor.Decompress((string)obj);
                //domainKey = this.compressor.Decompress(domainKey);
            }

            IContext ctx = contextFactory.GetContext(domainKey);
            MarshalingTransformer transformer = new MarshalingTransformer(ctx);
            //Deserialize the uow
            MarshalUnitOfWork muow = (MarshalUnitOfWork)formater.Deserialize(obj, typeof(MarshalUnitOfWork));

            string            identity;
            string            serialized = "";
            IClassMap         classMap;
            Type              realType;
            object            ro;
            MarshalObjectList mol = new MarshalObjectList();
            Hashtable         insertedRealObjects = new Hashtable();

            ITransaction tx = null;

            try
            {
                tx = ctx.BeginTransaction();

                foreach (MarshalObject mo in muow.RemoveObjects)
                {
                    identity = transformer.GetIdentity(mo);
                    classMap = ctx.DomainMap.MustGetClassMap(mo.Type);
                    realType = ctx.AssemblyManager.MustGetTypeFromClassMap(classMap);
                    ro       = ctx.GetObjectById(identity, realType);
                    transformer.ToObject(mo, ref ro, 0, RefreshBehaviorType.OverwriteLoaded);
                    ctx.UnitOfWork.RegisterDeleted(ro);
                }

                foreach (MarshalObject mo in muow.UpdateObjects)
                {
                    identity = transformer.GetIdentity(mo);
                    classMap = ctx.DomainMap.MustGetClassMap(mo.Type);
                    realType = ctx.AssemblyManager.MustGetTypeFromClassMap(classMap);
                    ro       = ctx.GetObjectById(identity, realType);
                    transformer.ToObject(mo, ref ro, 0, RefreshBehaviorType.OverwriteLoaded);
                    ctx.UnitOfWork.RegisterDirty(ro);
                }

                foreach (MarshalObject mo in muow.InsertObjects)
                {
                    classMap = ctx.DomainMap.MustGetClassMap(mo.Type);
                    realType = ctx.AssemblyManager.MustGetTypeFromClassMap(classMap);
                    if (mo.TempId.Length > 0)
                    {
                        ro = ctx.CreateObject(realType);
                    }
                    else
                    {
                        identity = transformer.GetIdentity(mo);
                        ro       = ctx.CreateObject(identity, realType);
                    }
                    insertedRealObjects[mo] = ro;
                    transformer.ToObject(mo, ref ro, 0, RefreshBehaviorType.OverwriteDirty);
                }

                //Commit transaction
                tx.Commit();
            }
            catch (Exception ex)
            {
                if (tx != null)
                {
                    tx.Rollback();
                }
                ctx.Dispose();
                throw ex;
            }

            foreach (MarshalObject mo in muow.InsertObjects)
            {
                classMap = ctx.DomainMap.MustGetClassMap(mo.Type);
                if (classMap.HasAssignedBySource())
                {
                    foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps())
                    {
                        if (propertyMap.GetIsAssignedBySource())
                        {
                            MarshalProperty mp = mo.GetProperty(propertyMap.Name);
                            if (mp == null)
                            {
                                mp      = new MarshalProperty();
                                mp.Name = propertyMap.Name;
                                mo.Properties.Add(mp);
                            }
                            ro       = insertedRealObjects[mo];
                            mp.Value = transformer.FromPropertyValue(ro, ctx.ObjectManager.GetPropertyValue(ro, propertyMap.Name), propertyMap);
                            mol.Objects.Add(mo);
                        }
                    }
                }
            }

            ctx.Dispose();

            serialized = (string)formater.Serialize(mol);

            if (useCompression && this.compressor != null)
            {
                return(this.compressor.Compress(serialized));
            }
            else
            {
                return(serialized);
            }
        }
        public override void LoadProperty(object obj, string propertyName)
        {
            if (this.url.Length < 1)
            {
                throw new NPersistException("You must specify an url to your NPersist Web Service in your WebServiceRemotingEngine!");
            }
            RemotingService        rs          = new RemotingService(this.Context, url);
            IMarshalingTransformer transformer = new MarshalingTransformer(Context);
            IObjectManager         om          = Context.ObjectManager;

            IClassMap        classMap    = Context.DomainMap.MustGetClassMap(obj.GetType());
            IPropertyMap     propertyMap = classMap.MustGetPropertyMap(propertyName);
            MarshalReference mr          = transformer.FromObjectAsReference(obj);
            string           xmlObject   = (string)Formatter.Serialize(mr);

            if (useCompression && this.compressor != null)
            {
                xmlObject = this.compressor.Compress(xmlObject);
            }

            bool doUseCompression = this.useCompression;

            if (this.compressor == null)
            {
                doUseCompression = false;
            }

            string result = rs.LoadProperty(xmlObject, propertyName, this.domainKey, doUseCompression);

            if (useCompression && this.compressor != null)
            {
                result = this.compressor.Decompress(result);
            }

            if (propertyMap.IsCollection)
            {
                if (propertyMap.ReferenceType == ReferenceType.None)
                {
                }
                else
                {
                    //Refresh issues!!! (the objects in the list being deserialized may exist in the cache!!)
                    MarshalObjectList mol       = (MarshalObjectList)Formatter.Deserialize(result, typeof(MarshalObjectList));
                    IList             freshList = transformer.ToObjectList(mol, RefreshBehaviorType.DefaultBehavior, new ArrayList());
                    //Context.IdentityMap.RegisterLoadedObject(obj);
                    IList orgList = (IList)om.GetPropertyValue(obj, propertyName);

                    LoadReferenceList(freshList, orgList, om, obj, propertyMap);
                    this.Context.InverseManager.NotifyPropertyLoad(obj, propertyMap, orgList);
                }
            }
            else
            {
                if (propertyMap.ReferenceType == ReferenceType.None)
                {
                    MarshalProperty mp = (MarshalProperty)Formatter.Deserialize(result, typeof(MarshalProperty));
                    transformer.ToProperty(obj, mp, propertyMap, RefreshBehaviorType.DefaultBehavior);
                }
                else
                {
                    if (result.Length > 0)
                    {
                        MarshalObject mo          = (MarshalObject)Formatter.Deserialize(result, typeof(MarshalObject));
                        string        identity    = transformer.GetIdentity(mo);
                        IClassMap     refClassMap = Context.DomainMap.MustGetClassMap(mo.Type);
                        Type          refType     = Context.AssemblyManager.MustGetTypeFromClassMap(refClassMap);

                        object refObject = Context.GetObjectById(identity, refType, true);
                        if (om.GetObjectStatus(refObject) == ObjectStatus.NotLoaded)
                        {
                            transformer.ToObject(mo, ref refObject);
                        }

//						object refObject = Context.TryGetObjectById(identity, refType, true);
//						if (refObject == null)
//						{
//							refObject = Context.GetObjectById(identity, refType, true);
//							transformer.ToObject(mo, ref refObject);
//						}
                        om.SetPropertyValue(obj, propertyName, refObject);
                        om.SetOriginalPropertyValue(obj, propertyName, refObject);
                        om.SetNullValueStatus(obj, propertyName, false);
                        this.Context.InverseManager.NotifyPropertyLoad(obj, propertyMap, refObject);
                    }
                    else
                    {
                        om.SetPropertyValue(obj, propertyName, null);
                        om.SetOriginalPropertyValue(obj, propertyName, null);
                        om.SetNullValueStatus(obj, propertyName, true);
                    }
                }
            }
        }