コード例 #1
0
        //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);
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        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);
                    }
                }
            }
        }