protected virtual string GetDirectoryForClass(IClassMap classMap)
        {
            ISourceMap sourceMap = classMap.GetDocSourceMap();

            if (sourceMap == null)
            {
                throw new MappingException("Can't find Document Source Map for '" + classMap.GetFullName() + "' in the npersist xml mapping file!");                 // do not localize
            }
            string directory = sourceMap.DocPath;

            if (!(Directory.Exists(directory)))
            {
                throw new NPersistException("Can't find directory: " + directory);                 // do not localize
            }
            directory += @"\" + classMap.GetFullName();

            if (!(Directory.Exists(directory)))
            {
                try
                {
                    Directory.CreateDirectory(directory);
                }
                catch (Exception ex)
                {
                    throw new NPersistException("Could not create directory '" + directory + "': " + ex.Message, ex);                     // do not localize
                }
            }

            return(directory);
        }
		public virtual Type GetTypeFromClassMap(IClassMap classMap)
		{
			Type type;
			type = Type.GetType(classMap.GetFullName() + ", " + classMap.GetAssemblyName());
			if (type == null)
			{
				foreach (Assembly asm in m_LoadedAssemblies.Values)
				{
					type = asm.GetType(classMap.GetFullName());
					if (type != null)
					{
						break;
					}
				}
			}
			return type;
		}
        public virtual Type GetTypeFromClassMap(IClassMap classMap)
        {
            Type type;

            type = Type.GetType(classMap.GetFullName() + ", " + classMap.GetAssemblyName());
            if (type == null)
            {
                foreach (Assembly asm in m_LoadedAssemblies.Values)
                {
                    type = asm.GetType(classMap.GetFullName());
                    if (type != null)
                    {
                        break;
                    }
                }
            }
            return(type);
        }
        public bool CompareListsById(IList newList, IList oldList)
        {
            if (newList == null || oldList == null)
            {
                if (!((newList == null && oldList == null)))
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            if (!(oldList.Count == newList.Count))
            {
                return(false);
            }
            IObjectManager om  = this.Context.ObjectManager;
            IDomainMap     dm  = this.Context.DomainMap;
            Hashtable      ids = new Hashtable();

            foreach (object value in newList)
            {
                IClassMap classMap = dm.MustGetClassMap(value.GetType());
                string    id       = classMap.GetFullName() + om.GetObjectIdentity(value);
                ids[id] = id;
            }

            foreach (object value in oldList)
            {
                IClassMap classMap = dm.MustGetClassMap(value.GetType());
                string    id       = classMap.GetFullName() + om.GetObjectIdentity(value);
                if (!(ids.ContainsKey(id)))
                {
                    return(false);
                }
            }

            ids = new Hashtable();
            foreach (object value in oldList)
            {
                IClassMap classMap = dm.MustGetClassMap(value.GetType());
                string    id       = classMap.GetFullName() + om.GetObjectIdentity(value);
                ids[id] = id;
            }
            foreach (object value in newList)
            {
                IClassMap classMap = dm.MustGetClassMap(value.GetType());
                string    id       = classMap.GetFullName() + om.GetObjectIdentity(value);
                if (!(ids.ContainsKey(id)))
                {
                    return(false);
                }
            }
            return(true);
        }
        protected virtual string GetFileNamePerObject(object obj, IClassMap classMap)
        {
            IObjectManager om = this.Context.ObjectManager;

            string directory = GetDirectoryForClass(classMap);

            string fileName = directory + @"\" + classMap.GetFullName() + "." + om.GetObjectIdentity(obj) + ".xml";

            return(fileName);
        }
        public virtual Type GetTypeFromPropertyMap(IPropertyMap propertyMap)
        {
            Type      type     = null;
            string    dataType = propertyMap.GetDataOrItemType();
            IClassMap classMap = propertyMap.ClassMap.DomainMap.GetClassMap(dataType);

            if (classMap != null)
            {
                type = Type.GetType(classMap.GetFullName() + ", " + classMap.GetAssemblyName());
                if (type == null)
                {
                    foreach (Assembly asm in m_LoadedAssemblies.Values)
                    {
                        type = asm.GetType(classMap.GetFullName());
                        if (type != null)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                type = Type.GetType(dataType);
                if (type == null)
                {
                    IDomainMap domainMap = propertyMap.ClassMap.DomainMap;
                    string     fullName  = dataType;
                    if (domainMap.RootNamespace.Length > 0)
                    {
                        fullName = domainMap.RootNamespace + "." + fullName;
                    }

                    type = Type.GetType(fullName + ", " + domainMap.GetAssemblyName());
                }
            }
            return(type);
        }
        protected virtual string GetDirectoryForDomain(IClassMap classMap)
        {
            ISourceMap sourceMap = classMap.GetDocSourceMap();

            if (sourceMap == null)
            {
                throw new MappingException("Can't find Document Source Map for '" + classMap.GetFullName() + "' in the npersist xml mapping file!");                 // do not localize
            }
            string directory = sourceMap.DocPath;

            if (!(Directory.Exists(directory)))
            {
                throw new NPersistException("Can't find directory: " + directory);                 // do not localize
            }
            return(directory);
        }
        protected virtual void SerializeObject(XmlNode xmlObject, object obj, IClassMap classMap, bool creating, bool specifyClass)
        {
            if (specifyClass)
            {
                SetAttributeValue(xmlObject, "class", classMap.GetFullName());
            }

            foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps())
            {
                if (propertyMap.ReferenceType == ReferenceType.None)
                {
                    if (propertyMap.IsCollection)
                    {
                        ;
                    }
                    else
                    {
                        SerializeInlineProperty(xmlObject, obj, classMap, propertyMap, creating);
                    }
                }
                else
                {
                    if (propertyMap.DocPropertyMapMode == DocPropertyMapMode.Inline)
                    {
                        if (propertyMap.IsCollection)
                        {
                            SerializeInlineObjectList(xmlObject, obj, classMap, propertyMap, creating);
                        }
                        else
                        {
                            SerializeInlineObject(xmlObject, obj, classMap, propertyMap, creating);
                        }
                    }
                    else
                    {
                        if (propertyMap.IsCollection)
                        {
                            SerializeInlineReferenceList(xmlObject, obj, classMap, propertyMap, creating);
                        }
                        else
                        {
                            SerializeInlineReference(xmlObject, obj, classMap, propertyMap, creating);
                        }
                    }
                }
            }
        }
        public virtual void Clear(Type type)
        {
            IClassMap classMap = this.Context.DomainMap.MustGetClassMap(type);
            string    typeName = classMap.GetFullName();

            lock (ReadOnlyObjectCache)
            {
                IList clear = new ArrayList();
                foreach (ReadOnlyClone clone in ReadOnlyObjectCache.ObjectCache)
                {
                    if (clone.Type == typeName)
                    {
                        clear.Add(clone);
                    }
                }
                foreach (ReadOnlyClone clone in clear)
                {
                    ReadOnlyObjectCache.ObjectCache.Remove(clone);
                }
                ReadOnlyObjectCache.ObjectCache.Clear();
            }
        }
示例#10
0
        private CodeNamespace ClassMapToCodeNamespace(IClassMap classMap)
        {
            CodeNamespace       domainNamespace = new CodeNamespace(classMap.GetFullNamespace());
            CodeTypeDeclaration classDecl       = new CodeTypeDeclaration(classMap.GetName());

            if (classMap.ClassType == ClassType.Default || classMap.ClassType == ClassType.Class)
            {
                classDecl.IsClass = true;

                IClassMap super = classMap.GetInheritedClassMap();

                if (super != null)
                {
                    CodeTypeReference superDecl = new CodeTypeReference(super.GetFullName());
                    classDecl.BaseTypes.Add(superDecl);
                }

                foreach (IPropertyMap propertyMap in classMap.PropertyMaps)
                {
                    classDecl.Members.Add(PropertyMapToCodeMemberField(propertyMap));
                    classDecl.Members.Add(PropertyMapToCodeMemberProperty(propertyMap));
                }
            }
            else if (classMap.ClassType == ClassType.Enum)
            {
                classDecl.IsEnum = true;

                foreach (IEnumValueMap enumValueMap in classMap.GetEnumValueMaps())
                {
                    classDecl.Members.Add(EnumValueMapToCodeMemberField(enumValueMap));
                }
            }

            domainNamespace.Types.Add(classDecl);

            return(domainNamespace);
        }
 protected virtual void LoadObjectsPerObject(Type type, RefreshBehaviorType refreshBehavior, IList listToFill, IClassMap classMap)
 {
     string directory = GetDirectoryForClass(classMap);
     string className = classMap.GetFullName().ToLower();
     foreach (string fileName in Directory.GetFiles(directory))
     {
         FileInfo file = new FileInfo(fileName);
         if (file.Extension.ToLower() == ".xml")
         {
             if (file.Name.Length > className.Length)
             {
                 if (file.Name.Substring(0, className.Length).ToLower() == className)
                 {
                     string xml = LoadFile(type, fileName);
                     if (xml != "")
                         DeserializeObject(type, listToFill, classMap, xml);
                 }
             }
         }
     }
 }
        protected virtual string GetFileNamePerObject(object obj, IClassMap classMap)
        {
            IObjectManager om = this.Context.ObjectManager;

            string directory = GetDirectoryForClass(classMap);

            string fileName = directory + @"\" + classMap.GetFullName() + "." + om.GetObjectIdentity(obj) + ".xml";

            return fileName;
        }
        protected virtual string GetDirectoryForDomain(IClassMap classMap)
        {
            ISourceMap sourceMap = classMap.GetDocSourceMap();

            if (sourceMap == null)
                throw new MappingException("Can't find Document Source Map for '" + classMap.GetFullName() + "' in the npersist xml mapping file!"); // do not localize

            string directory = sourceMap.DocPath;

            if (!(Directory.Exists(directory)))
                throw new NPersistException("Can't find directory: " + directory); // do not localize

            return directory;
        }
        protected virtual string GetDirectoryForClass(IClassMap classMap)
        {
            ISourceMap sourceMap = classMap.GetDocSourceMap();

            if (sourceMap == null)
                throw new MappingException("Can't find Document Source Map for '" + classMap.GetFullName() + "' in the npersist xml mapping file!"); // do not localize

            string directory = sourceMap.DocPath;

            if (!(Directory.Exists(directory)))
                throw new NPersistException("Can't find directory: " + directory); // do not localize

            directory += @"\" + classMap.GetFullName();

            if (!(Directory.Exists(directory)))
            {
                try
                {
                    Directory.CreateDirectory(directory);
                }
                catch (Exception ex)
                {
                    throw new NPersistException("Could not create directory '" + directory + "': " + ex.Message, ex); // do not localize
                }
            }

            return directory;
        }
        protected virtual void SerializeObject(XmlNode xmlObject, object obj, IClassMap classMap, bool creating, bool specifyClass)
        {
            if (specifyClass)
                SetAttributeValue(xmlObject, "class", classMap.GetFullName() );

            foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps())
            {
                if (propertyMap.ReferenceType == ReferenceType.None)
                {
                    if (propertyMap.IsCollection)
                    {
                        ;
                    }
                    else
                    {
                        SerializeInlineProperty(xmlObject, obj, classMap, propertyMap, creating);
                    }
                }
                else
                {
                    //if (propertyMap.DocPropertyMapMode == DocPropertyMapMode.Default || propertyMap.DocPropertyMapMode == DocPropertyMapMode.Inline)
                    if (propertyMap.DocPropertyMapMode == DocPropertyMapMode.Inline)
                    {
                        if (propertyMap.IsCollection)
                        {
                            SerializeInlineObjectList(xmlObject, obj, classMap, propertyMap, creating);
                        }
                        else
                        {
                            SerializeInlineObject(xmlObject, obj, classMap, propertyMap, creating);
                        }
                    }
                    else
                    {
                        if (propertyMap.IsCollection)
                        {
                            SerializeInlineReferenceList(xmlObject, obj, classMap, propertyMap, creating);
                        }
                        else
                        {
                            SerializeInlineReference(xmlObject, obj, classMap, propertyMap, creating);
                        }
                    }
                }

            }
        }
        protected virtual void SerializeClone(object obj, ReadOnlyClone clone, string id, string key)
        {
            IObjectManager   om       = this.Context.ObjectManager;
            IDomainMap       dm       = this.Context.DomainMap;
            IAssemblyManager am       = this.Context.AssemblyManager;
            IClassMap        classMap = dm.MustGetClassMap(obj.GetType());
            IListManager     lm       = this.Context.ListManager;

            clone.Identity = id;
            clone.Key      = key;
            clone.Type     = classMap.GetFullName();
            foreach (IPropertyMap propertyMap in classMap.GetAllPropertyMaps())
            {
                if (propertyMap.ReferenceType == ReferenceType.None)
                {
                    if (propertyMap.IsCollection)
                    {
                        IList values = new ArrayList();
                        IList list   = (IList)om.GetPropertyValue(obj, propertyMap.Name);

                        foreach (object value in list)
                        {
                            values.Add(value);
                        }

                        clone.PropertyValues[propertyMap.Name]    = values;
                        clone.NullValueStatuses[propertyMap.Name] = false;
                    }
                    else
                    {
                        object value = om.GetPropertyValue(obj, propertyMap.Name);
                        clone.PropertyValues[propertyMap.Name]    = value;
                        clone.NullValueStatuses[propertyMap.Name] = om.GetNullValueStatus(obj, propertyMap.Name);
                    }
                }
                else
                {
                    IClassMap refClassMap = propertyMap.MustGetReferencedClassMap();
                    if (refClassMap.IsReadOnly)
                    {
                        if (propertyMap.IsCollection)
                        {
                            IList values = new ArrayList();
                            IList list   = (IList)om.GetPropertyValue(obj, propertyMap.Name);

                            foreach (object value in list)
                            {
                                if (value != null)
                                {
                                    refClassMap = dm.MustGetClassMap(value.GetType());
                                    string refIdentity        = om.GetObjectIdentity(value);
                                    SerializedReference refId = new SerializedReference(refIdentity, refClassMap.GetFullName());
                                    values.Add(refId);
                                }
                            }
                            clone.PropertyValues[propertyMap.Name]    = values;
                            clone.NullValueStatuses[propertyMap.Name] = false;
                        }
                        else
                        {
                            object value = om.GetPropertyValue(obj, propertyMap.Name);
                            if (value != null)
                            {
                                refClassMap = dm.MustGetClassMap(value.GetType());
                                string refIdentity        = om.GetObjectIdentity(value);
                                SerializedReference refId = new SerializedReference(refIdentity, refClassMap.GetFullName());
                                value = refId;
                            }
                            clone.PropertyValues[propertyMap.Name]    = value;
                            clone.NullValueStatuses[propertyMap.Name] = om.GetNullValueStatus(obj, propertyMap.Name);
                        }
                    }
                }
            }
        }