public virtual IList ParseObjectIdentityKeyParts(IClassMap classMap, IList idPropertyMaps, Type type, string identity)
        {
            string sep = classMap.IdentitySeparator;

            if (sep == "")
            {
                sep = "|";
            }
            string[] arrId      = identity.Split(sep.ToCharArray());
            IList    idKeyParts = new ArrayList(1);
            long     i          = 0;

            foreach (IPropertyMap propertyMap in idPropertyMaps)
            {
                if (propertyMap.ReferenceType != ReferenceType.None)
                {
                    //Bad...only works for referenced objects with non-composite ids...
                    IClassMap refClassMap = propertyMap.MustGetReferencedClassMap();
                    Type      refType     = this.Context.AssemblyManager.MustGetTypeFromClassMap(refClassMap);
                    idKeyParts.Add(ConvertValueToType(refType, (IPropertyMap)refClassMap.GetIdentityPropertyMaps()[0], arrId[i]));
                }
                else
                {
                    idKeyParts.Add(ConvertValueToType(type, propertyMap, arrId[i]));
                }
                i += 1;
            }
            return(idKeyParts);
        }
 public virtual void SetObjectIdentity(object obj, KeyStruct keyStruct)
 {
     try
     {
         IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType());
         long      i        = 1;
         Type      refType;
         object    refObj;
         object    val;
         foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps())
         {
             if (propertyMap.ReferenceType != ReferenceType.None)
             {
                 //Bad...only works for referenced objects with non-composite ids...
                 refType = obj.GetType().GetProperty(propertyMap.Name).PropertyType;
                 refObj  = this.Context.GetObjectById(keyStruct.keys[i], refType, true);
                 SetPropertyValue(obj, propertyMap.Name, refObj);
                 SetOriginalPropertyValue(obj, propertyMap.Name, refObj);
                 SetNullValueStatus(obj, propertyMap.Name, false);
             }
             else
             {
                 val = keyStruct.keys[i];
                 SetPropertyValue(false, obj, propertyMap.Name, val);
                 SetOriginalPropertyValue(obj, propertyMap.Name, val);
                 SetNullValueStatus(obj, propertyMap.Name, false);
             }
             i += 1;
         }
     }
     catch (Exception ex)
     {
         throw new NPersistException(string.Format("Could not set Identity '{0}' on object '{1}'", keyStruct.keys[1].ToString(), obj), ex);
     }
 }
        public string GetIdentity(MarshalReference marshalReference, MarshalReferenceValue marshalReferenceValue)
        {
            IClassMap     classMap = Context.DomainMap.MustGetClassMap(marshalReference.Type);
            StringBuilder id       = new StringBuilder();
            string        sep      = classMap.IdentitySeparator;

            if (sep == "")
            {
                sep = "|";
            }
            foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps())
            {
                if (propertyMap.ReferenceType == ReferenceType.None)
                {
                    MarshalProperty mp = marshalReferenceValue.GetReferenceProperty(propertyMap.Name);
                    id.Append(mp.Value + sep);
                }
                else
                {
                    MarshalReference mr = marshalReferenceValue.GetReferenceReference(propertyMap.Name);
                    id.Append(GetIdentity(mr, marshalReferenceValue) + sep);
                }
            }
            if (id.Length > 0)
            {
                id.Length -= sep.Length;
            }
            return(id.ToString());
        }
        public bool HasIdentity(object obj)
        {
            IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType());

            object value;

            foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps())
            {
                value = GetPropertyValue(obj, propertyMap.Name);
                if (value == null || GetNullValueStatus(obj, propertyMap.Name) == true)
                {
                    return(false);
                }
                else if (propertyMap.ReferenceType != ReferenceType.None)
                {
                    //this ensures that a complete id can be created ahead in case of auto-in
                    //m_ObjectManager.GetPropertyValue(obj, propertyMap.Name);
                    if (!HasIdentity(value))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
        public virtual void SetObjectIdentity(object obj, string identity)
        {
            IClassMap classMap = m_ObjectManager.Context.DomainMap.MustGetClassMap(obj.GetType());
            string    sep      = classMap.IdentitySeparator;

            if (sep == "")
            {
                sep = "|";
            }
            string[] arrId = identity.Split(sep.ToCharArray());
            long     i     = 0;
            Type     refType;
            object   refObj;
            object   val;

            foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps())
            {
                if (propertyMap.ReferenceType != ReferenceType.None)
                {
                    refType = obj.GetType().GetProperty(propertyMap.Name).PropertyType;
                    refObj  = m_ObjectManager.Context.GetObjectById(Convert.ToString(arrId[i]), refType, true);
                    m_ObjectManager.SetPropertyValue(obj, propertyMap.Name, refObj);
                    m_ObjectManager.SetOriginalPropertyValue(obj, propertyMap.Name, refObj);
                    m_ObjectManager.SetNullValueStatus(obj, propertyMap.Name, false);
                }
                else
                {
                    val = ConvertValueToType(obj, propertyMap, arrId[i]);
                    m_ObjectManager.SetPropertyValue(obj, propertyMap.Name, val);
                    m_ObjectManager.SetOriginalPropertyValue(obj, propertyMap.Name, val);
                    m_ObjectManager.SetNullValueStatus(obj, propertyMap.Name, false);
                }
                i += 1;
            }
        }
        public virtual MarshalReference FromObjectAsReference(object sourceObject)
        {
            IClassMap        classMap = Context.DomainMap.MustGetClassMap(sourceObject.GetType());
            MarshalReference mr       = new MarshalReference();

            mr.Type = classMap.GetName();
            foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps())
            {
                if (propertyMap.ReferenceType == ReferenceType.None)
                {
                    if (propertyMap.IsCollection)
                    {
                    }
                    else
                    {
                        mr.Value.ReferenceProperties.Add(FromProperty(sourceObject, propertyMap));
                    }
                }
                else
                {
                    if (propertyMap.IsCollection)
                    {
                    }
                    else
                    {
                        mr.Value.ReferenceProperties.Add(FromReference(sourceObject, propertyMap));
                    }
                }
            }

            return(mr);
        }
        protected virtual XmlNode FindNodeForObject(XmlNode xmlRoot, object obj, IClassMap classMap)
        {
            IObjectManager om      = this.Context.ObjectManager;
            string         element = classMap.GetDocElement();

            string xpath = "";

            foreach (IPropertyMap idPropertyMap in classMap.GetIdentityPropertyMaps())
            {
                if (xpath.Length > 0)
                {
                    xpath += " and ";                     // do not localize
                }
                if (idPropertyMap.DocElement.Length > 0)
                {
                    xpath += idPropertyMap.Name + " = \"" + om.GetPropertyValue(obj, idPropertyMap.Name).ToString() + "\"";                     // do not localize
                }
                else
                {
                    xpath += "@" + idPropertyMap.GetDocAttribute() + " = \"" + om.GetPropertyValue(obj, idPropertyMap.Name).ToString() + "\"";                     // do not localize
                }
            }

            xpath = element + "[" + xpath + "]";

            XmlNode xmlNode = xmlRoot.SelectSingleNode(xpath);

            return(xmlNode);
        }
        private bool GuidMustBeTemporaryIdentity(Type type, IClassMap classMap)
        {
            IList idPropertyMaps = classMap.GetIdentityPropertyMaps();

            if (idPropertyMaps.Count > 1)
            {
                return(true);
            }

            //should only be one id prop..
            foreach (IPropertyMap propertyMap in idPropertyMaps)
            {
                Type propType = type.GetProperty(propertyMap.Name).PropertyType;
                if (propertyMap.ReferenceType != ReferenceType.None)
                {
                    IClassMap refClassMap = this.Context.DomainMap.MustGetClassMap(propType);
                    return(GuidMustBeTemporaryIdentity(propType, refClassMap));
                }
                else
                {
                    if (!propType.IsAssignableFrom(typeof(Guid)))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        private IList BuildObjectIdentityKeyParts(object obj, IPropertyMap newPropertyMap, object newValue)
        {
            IIdentityHelper identityHelper = obj as IIdentityHelper;

            if (identityHelper != null)
            {
                if (identityHelper.HasIdentityKeyParts())
                {
                    return(identityHelper.GetIdentityKeyParts());
                }
            }

            IList idKeyParts = new ArrayList(1);

            IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType());

            object value;

            foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps())
            {
                if (propertyMap == newPropertyMap)
                {
                    value = newValue;
                    if (propertyMap.ReferenceType != ReferenceType.None)
                    {
                        value = GetObjectIdentity(value);
                    }
                }
                else
                {
                    value = GetPropertyValue(obj, propertyMap.Name);
                    if (value == null || GetNullValueStatus(obj, propertyMap.Name) == true)
                    {
                        idKeyParts.Clear();
                        idKeyParts.Add(GetTempId(obj));
                        return(idKeyParts);
                    }
                    else if (propertyMap.ReferenceType != ReferenceType.None)
                    {
                        //this ensures that a complete id can be created ahead in case of auto-in
                        //m_ObjectManager.GetPropertyValue(obj, propertyMap.Name);
                        value = GetObjectIdentity(value);
                    }
                }
                idKeyParts.Add(value);
            }

            if (identityHelper != null)
            {
                IList cached = identityHelper.GetIdentityKeyParts();
                foreach (object keyPart in idKeyParts)
                {
                    cached.Add(keyPart);
                }
            }

            return(idKeyParts);
        }
        public string GetIdentityPropertyName(string className)
        {
            IClassMap classMap = GetDomainMap().MustGetClassMap(className);

            foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps())
            {
                return(propertyMap.Name);
            }
            return("");
        }
Example #11
0
        /// <summary>
        /// Gets the primary key of a class.
        /// </summary>
        /// <param name="classMap">The class map.</param>
        /// <returns></returns>
        public static IPropertyMap GetPrimaryKey(IClassMap classMap)
        {
            ArrayList primary = classMap.GetIdentityPropertyMaps();

            if (primary.Count != 1)
            {
                throw new Exception("Wrong primary keys count for class " + classMap.Name + ": " + primary.Count);
            }

            return((IPropertyMap)primary[0]);
        }
        public virtual void SetObjectIdentity(object obj, string identity)
        {
            try
            {
                IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType());
                if (Util.IsGuid(identity))
                {
                    //figure out if it is a temporary identity
                    if (GuidMustBeTemporaryIdentity(obj.GetType(), classMap))
                    {
                        GetTempId(obj, identity);
                        return;
                    }
                }

                string sep = classMap.IdentitySeparator;
                if (sep == "")
                {
                    sep = "|";
                }
                string[] arrId = identity.Split(sep.ToCharArray());
                long     i     = 0;
                Type     refType;
                object   refObj;

                foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps())
                {
                    if (propertyMap.ReferenceType != ReferenceType.None)
                    {
                        //Bad...only works for referenced objects with non-composite ids...
                        refType = obj.GetType().GetProperty(propertyMap.Name).PropertyType;
                        refObj  = this.Context.GetObjectById(Convert.ToString(arrId[i]), refType, true);
                        SetPropertyValue(obj, propertyMap.Name, refObj);
                        SetOriginalPropertyValue(obj, propertyMap.Name, refObj);
                        SetNullValueStatus(obj, propertyMap.Name, false);
                    }
                    else
                    {
                        object val = ConvertValueToType(obj, propertyMap, arrId[i]);
                        SetPropertyValue(obj, propertyMap.Name, val);
                        SetOriginalPropertyValue(obj, propertyMap.Name, val);
                        SetNullValueStatus(obj, propertyMap.Name, false);
                    }
                    i += 1;
                }
            }
            catch (Exception ex)
            {
                throw new NPersistException(string.Format("Could not set Identity '{0}' on object '{1}'", identity, obj), ex);
            }
        }
        private string BuildObjectIdentity(object obj, IPropertyMap newPropertyMap, object newValue)
        {
            string    id       = "";
            IClassMap classMap = m_ObjectManager.Context.DomainMap.MustGetClassMap(obj.GetType());
            string    sep      = classMap.IdentitySeparator;

            //			bool gotObjectStatus = false;
            //			ObjectStatus objStatus = ObjectStatus.Clean;
            if (sep == "")
            {
                sep = "|";
            }
            object value;

            foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps())
            {
                if (propertyMap == newPropertyMap)
                {
                    value = newValue;
                    if (propertyMap.ReferenceType != ReferenceType.None)
                    {
                        value = m_ObjectManager.GetObjectIdentity(value);
                    }
                }
                else
                {
                    value = m_ObjectManager.GetPropertyValue(obj, propertyMap.Name);
                    if (value == null || m_ObjectManager.GetNullValueStatus(obj, propertyMap.Name) == true)
                    {
                        if (!(m_hashTempIds.ContainsKey(obj)))
                        {
                            m_hashTempIds[obj] = Guid.NewGuid().ToString();
                        }
                        return((string)m_hashTempIds[obj]);
                    }
                    else if (propertyMap.ReferenceType != ReferenceType.None)
                    {
                        //this ensures that a complete id can be created ahead in case of auto-in
                        //m_ObjectManager.GetPropertyValue(obj, propertyMap.Name);
                        value = m_ObjectManager.GetObjectIdentity(value);
                    }
                }

                id += Convert.ToString(value) + sep;
            }
            if (id.Length > sep.Length)
            {
                id = id.Substring(0, id.Length - sep.Length);
            }
            return(id);
        }
Example #14
0
        public ArrayList GetPathPropertyMaps(string propertyPath, ref string special)
        {
            ArrayList    propertyMaps      = new ArrayList();
            IPropertyMap propertyMap       = null;
            IClassMap    classMap          = sqlEmitter.RootClassMap;
            bool         skip              = false;
            bool         first             = true;
            int          cnt               = 1;
            IList        pathPropertyNames = GetPathPropertyNames(propertyPath);

            foreach (string name in pathPropertyNames)
            {
                if (name.Length > 0)
                {
                    skip = false;
                    if (first)
                    {
                        if (name.ToLower(CultureInfo.InvariantCulture) == sqlEmitter.RootClassMap.Name.ToLower())
                        {
                            skip = true;
                        }
                    }
                    first = false;
                    if (!(skip))
                    {
                        if (!(name.Equals("*")))
                        {
                            propertyMap = classMap.MustGetPropertyMap(name);
                            propertyMaps.Add(propertyMap);

                            if (cnt < pathPropertyNames.Count)
                            {
                                classMap = propertyMap.MustGetReferencedClassMap();
                            }
                        }
                        else
                        {
                            //HACK: hmmm * => ID ??
                            special = name;
                            //special - we trade for the first id property
                            propertyMap = (IPropertyMap)classMap.GetIdentityPropertyMaps()[0];
                            propertyMaps.Add(propertyMap);
                        }
                    }
                }
                cnt++;
            }
            return(propertyMaps);
        }
Example #15
0
        private object[] GetKeyParts(Type type, string identity)
        {
            IClassMap classMap     = this.Context.DomainMap.MustGetClassMap(type);
            IList     idProperties = classMap.GetIdentityPropertyMaps();

            if (idProperties.Count < 1)
            {
                throw new NPersistException(string.Format("Type {0} has no known identity properties!", type.ToString()));
            }
            if (idProperties.Count > 1)
            {
                return(GetKeyParts(type, identity, classMap, idProperties));
            }

            IPropertyMap idPropertyMap = (IPropertyMap)idProperties[0];

            object[] keyParts = new object[2];
            keyParts[0] = type;
            bool found = false;

            if (idPropertyMap.GetIsAssignedBySource())
            {
                IColumnMap columnMap = idPropertyMap.GetColumnMap();
                if (columnMap != null)
                {
                    if (columnMap.IsAutoIncrease)
                    {
                        if (Util.IsNumeric(identity))
                        {
                            keyParts[1] = this.Context.ObjectManager.ConvertValueToType(type, ((IPropertyMap)idProperties[0]), identity);
                        }
                        else
                        {
                            keyParts[1] = identity;
                        }
                        found = true;
                    }
                }
            }
            if (!found)
            {
                keyParts[1] = this.Context.ObjectManager.ConvertValueToType(type, ((IPropertyMap)idProperties[0]), identity);
            }
            return(keyParts);
        }
Example #16
0
		/// <summary>
		/// Gets the primary key of a class.
		/// </summary>
		/// <param name="classMap">The class map.</param>
		/// <returns></returns>
		public static IPropertyMap GetPrimaryKey(IClassMap classMap)
		{
			ArrayList primary = classMap.GetIdentityPropertyMaps();
			if (primary.Count != 1)
			{
				throw new Exception("Wrong primary keys count for class " + classMap.Name + ": " + primary.Count);
			}

			return (IPropertyMap) primary[0];
		}
        public static void GenerateRepositoryGetByIdentityMethod(IClassMap classMap, CodeTypeDeclaration classDecl, bool tryMethod)
        {
            IList propertyMaps = classMap.GetIdentityPropertyMaps();

            CodeMemberMethod methodMember = new CodeMemberMethod() ;
            if (tryMethod)
                methodMember.Name = GetRepositoryGetByIdentityMethodName(classMap);
            else
                methodMember.Name = GetRepositoryTryGetByIdentityMethodName(classMap);

            CodeTypeReference typeReference = new CodeTypeReference(classMap.GetName());
            methodMember.ReturnType = typeReference;

            methodMember.Attributes = MemberAttributes.Public;

            foreach(IPropertyMap propertyMap in propertyMaps)
            {
                CodeParameterDeclarationExpression parameter = new CodeParameterDeclarationExpression(new CodeTypeReference(propertyMap.DataType), MakeCamelCase(propertyMap.Name));
                methodMember.Parameters.Add(parameter);
            }

            CodeThisReferenceExpression thisExp = new CodeThisReferenceExpression();
            CodeFieldReferenceExpression contextVar = new CodeFieldReferenceExpression(thisExp, "context");

            CodeVariableDeclarationStatement dummyObjectVarDecl = new CodeVariableDeclarationStatement(classMap.GetName(), "identityObject") ;
            CodeVariableReferenceExpression dummyObjectVar = new CodeVariableReferenceExpression("identityObject");

            CodeObjectCreateExpression dummyObjectCreateExpr = new CodeObjectCreateExpression(typeReference, new CodeExpression[] {} );

            CodeAssignStatement assignDummyStatement = new CodeAssignStatement(dummyObjectVar, dummyObjectCreateExpr);

            methodMember.Statements.Add(dummyObjectVarDecl);
            methodMember.Statements.Add(assignDummyStatement);

            foreach(IPropertyMap propertyMap in propertyMaps)
            {
                CodeArgumentReferenceExpression argExp = new CodeArgumentReferenceExpression(MakeCamelCase(propertyMap.Name));
                CodeVariableReferenceExpression propExp = new CodeVariableReferenceExpression("identityObject" + "." + propertyMap.Name);
                CodeAssignStatement assignStatement = new CodeAssignStatement(propExp, argExp);
                methodMember.Statements.Add(assignStatement);
            }

            CodeTypeOfExpression typeOfExp = new CodeTypeOfExpression(typeReference) ;
            CodeMethodInvokeExpression newObjectInit = null;
            if (tryMethod)
                newObjectInit = new CodeMethodInvokeExpression(contextVar, "GetObjectById", new CodeExpression[] { dummyObjectVar, typeOfExp } ) ;
            else
                newObjectInit = new CodeMethodInvokeExpression(contextVar, "TryGetObjectById", new CodeExpression[] { dummyObjectVar, typeOfExp } ) ;

            CodeCastExpression castExp = new CodeCastExpression(typeReference, newObjectInit) ;

            CodeVariableDeclarationStatement newObjectVarDecl = new CodeVariableDeclarationStatement(classMap.GetName(), MakeCamelCase(classMap.GetName()), castExp) ;
            CodeVariableReferenceExpression newObjectVar = new CodeVariableReferenceExpression(MakeCamelCase(classMap.GetName()));

            methodMember.Statements.Add(newObjectVarDecl);

            CodeMethodReturnStatement returnStmt = new CodeMethodReturnStatement(newObjectVar) ;

            methodMember.Statements.Add(returnStmt);

            classDecl.Members.Add(methodMember);
        }
        public static void GenerateRepositoryUpdateObjectMethod(IClassMap classMap, CodeTypeDeclaration classDecl)
        {
            IList propertyMaps = classMap.GetAllPropertyMaps();
            IList idPropertyMaps = classMap.GetIdentityPropertyMaps();

            CodeMemberMethod methodMember = new CodeMemberMethod() ;
            methodMember.Name = GetRepositoryUpdateObjectMethodName(classMap);

            CodeTypeReference typeReference = new CodeTypeReference(classMap.GetName());
            methodMember.ReturnType = typeReference;

            methodMember.Attributes = MemberAttributes.Public;

            foreach(IPropertyMap propertyMap in propertyMaps)
            {
                if (!propertyMap.IsCollection)
                {
                    CodeParameterDeclarationExpression parameter = new CodeParameterDeclarationExpression(new CodeTypeReference(propertyMap.DataType), MakeCamelCase(propertyMap.Name));
                    methodMember.Parameters.Add(parameter);
                }
            }

            CodeThisReferenceExpression thisExp = new CodeThisReferenceExpression();
            CodeFieldReferenceExpression contextVar = new CodeFieldReferenceExpression(thisExp, "context");

            CodeExpression[] createParams = new CodeExpression[idPropertyMaps.Count];

            int i = 0;
            foreach(IPropertyMap idPropertyMap in idPropertyMaps)
            {
                CodeArgumentReferenceExpression argExp = new CodeArgumentReferenceExpression(MakeCamelCase(idPropertyMap.Name));
                createParams[i] = argExp;
                i++;
            }

            CodeMethodInvokeExpression newObjectInit = new CodeMethodInvokeExpression(thisExp, GetRepositoryGetByIdentityMethodName(classMap), createParams ) ;

            CodeVariableDeclarationStatement newObjectVarDecl = new CodeVariableDeclarationStatement(classMap.GetName(), MakeCamelCase(classMap.GetName()), newObjectInit) ;
            CodeVariableReferenceExpression newObjectVar = new CodeVariableReferenceExpression(MakeCamelCase(classMap.GetName()));

            methodMember.Statements.Add(newObjectVarDecl);

            foreach(IPropertyMap propertyMap in propertyMaps)
            {
                if (!propertyMap.IsIdentity)
                {
                    if (!propertyMap.IsCollection)
                    {
                        CodeArgumentReferenceExpression argExp = new CodeArgumentReferenceExpression(MakeCamelCase(propertyMap.Name));
                        CodeVariableReferenceExpression propExp = new CodeVariableReferenceExpression(MakeCamelCase(classMap.Name) + "." + propertyMap.Name);
                        CodeAssignStatement assignStatement = new CodeAssignStatement(propExp, argExp);
                        methodMember.Statements.Add(assignStatement);
                    }
                }
            }

            CodeMethodInvokeExpression commitCtx = new CodeMethodInvokeExpression(contextVar, "Commit", new CodeExpression[] {} ) ;

            methodMember.Statements.Add(commitCtx);

            CodeMethodReturnStatement returnStmt = new CodeMethodReturnStatement(newObjectVar) ;

            methodMember.Statements.Add(returnStmt);

            classDecl.Members.Add(methodMember);
        }
Example #19
0
        public static void GenerateRepositoryUpdateObjectMethod(IClassMap classMap, CodeTypeDeclaration classDecl)
        {
            IList propertyMaps   = classMap.GetAllPropertyMaps();
            IList idPropertyMaps = classMap.GetIdentityPropertyMaps();

            CodeMemberMethod methodMember = new CodeMemberMethod();

            methodMember.Name = GetRepositoryUpdateObjectMethodName(classMap);

            CodeTypeReference typeReference = new CodeTypeReference(classMap.GetName());

            methodMember.ReturnType = typeReference;

            methodMember.Attributes = MemberAttributes.Public;

            foreach (IPropertyMap propertyMap in propertyMaps)
            {
                if (!propertyMap.IsCollection)
                {
                    CodeParameterDeclarationExpression parameter = new CodeParameterDeclarationExpression(new CodeTypeReference(propertyMap.DataType), MakeCamelCase(propertyMap.Name));
                    methodMember.Parameters.Add(parameter);
                }
            }

            CodeThisReferenceExpression  thisExp    = new CodeThisReferenceExpression();
            CodeFieldReferenceExpression contextVar = new CodeFieldReferenceExpression(thisExp, "context");

            CodeExpression[] createParams = new CodeExpression[idPropertyMaps.Count];

            int i = 0;

            foreach (IPropertyMap idPropertyMap in idPropertyMaps)
            {
                CodeArgumentReferenceExpression argExp = new CodeArgumentReferenceExpression(MakeCamelCase(idPropertyMap.Name));
                createParams[i] = argExp;
                i++;
            }

            CodeMethodInvokeExpression newObjectInit = new CodeMethodInvokeExpression(thisExp, GetRepositoryGetByIdentityMethodName(classMap), createParams);

            CodeVariableDeclarationStatement newObjectVarDecl = new CodeVariableDeclarationStatement(classMap.GetName(), MakeCamelCase(classMap.GetName()), newObjectInit);
            CodeVariableReferenceExpression  newObjectVar     = new CodeVariableReferenceExpression(MakeCamelCase(classMap.GetName()));

            methodMember.Statements.Add(newObjectVarDecl);

            foreach (IPropertyMap propertyMap in propertyMaps)
            {
                if (!propertyMap.IsIdentity)
                {
                    if (!propertyMap.IsCollection)
                    {
                        CodeArgumentReferenceExpression argExp          = new CodeArgumentReferenceExpression(MakeCamelCase(propertyMap.Name));
                        CodeVariableReferenceExpression propExp         = new CodeVariableReferenceExpression(MakeCamelCase(classMap.Name) + "." + propertyMap.Name);
                        CodeAssignStatement             assignStatement = new CodeAssignStatement(propExp, argExp);
                        methodMember.Statements.Add(assignStatement);
                    }
                }
            }

            CodeMethodInvokeExpression commitCtx = new CodeMethodInvokeExpression(contextVar, "Commit", new CodeExpression[] {});

            methodMember.Statements.Add(commitCtx);

            CodeMethodReturnStatement returnStmt = new CodeMethodReturnStatement(newObjectVar);

            methodMember.Statements.Add(returnStmt);

            classDecl.Members.Add(methodMember);
        }
Example #20
0
        public static void GenerateRepositoryGetByIdentityMethod(IClassMap classMap, CodeTypeDeclaration classDecl, bool tryMethod)
        {
            IList propertyMaps = classMap.GetIdentityPropertyMaps();

            CodeMemberMethod methodMember = new CodeMemberMethod();

            if (tryMethod)
            {
                methodMember.Name = GetRepositoryGetByIdentityMethodName(classMap);
            }
            else
            {
                methodMember.Name = GetRepositoryTryGetByIdentityMethodName(classMap);
            }

            CodeTypeReference typeReference = new CodeTypeReference(classMap.GetName());

            methodMember.ReturnType = typeReference;

            methodMember.Attributes = MemberAttributes.Public;

            foreach (IPropertyMap propertyMap in propertyMaps)
            {
                CodeParameterDeclarationExpression parameter = new CodeParameterDeclarationExpression(new CodeTypeReference(propertyMap.DataType), MakeCamelCase(propertyMap.Name));
                methodMember.Parameters.Add(parameter);
            }

            CodeThisReferenceExpression  thisExp    = new CodeThisReferenceExpression();
            CodeFieldReferenceExpression contextVar = new CodeFieldReferenceExpression(thisExp, "context");

            CodeVariableDeclarationStatement dummyObjectVarDecl = new CodeVariableDeclarationStatement(classMap.GetName(), "identityObject");
            CodeVariableReferenceExpression  dummyObjectVar     = new CodeVariableReferenceExpression("identityObject");

            CodeObjectCreateExpression dummyObjectCreateExpr = new CodeObjectCreateExpression(typeReference, new CodeExpression[] {});

            CodeAssignStatement assignDummyStatement = new CodeAssignStatement(dummyObjectVar, dummyObjectCreateExpr);

            methodMember.Statements.Add(dummyObjectVarDecl);
            methodMember.Statements.Add(assignDummyStatement);

            foreach (IPropertyMap propertyMap in propertyMaps)
            {
                CodeArgumentReferenceExpression argExp          = new CodeArgumentReferenceExpression(MakeCamelCase(propertyMap.Name));
                CodeVariableReferenceExpression propExp         = new CodeVariableReferenceExpression("identityObject" + "." + propertyMap.Name);
                CodeAssignStatement             assignStatement = new CodeAssignStatement(propExp, argExp);
                methodMember.Statements.Add(assignStatement);
            }

            CodeTypeOfExpression       typeOfExp     = new CodeTypeOfExpression(typeReference);
            CodeMethodInvokeExpression newObjectInit = null;

            if (tryMethod)
            {
                newObjectInit = new CodeMethodInvokeExpression(contextVar, "GetObjectById", new CodeExpression[] { dummyObjectVar, typeOfExp });
            }
            else
            {
                newObjectInit = new CodeMethodInvokeExpression(contextVar, "TryGetObjectById", new CodeExpression[] { dummyObjectVar, typeOfExp });
            }

            CodeCastExpression castExp = new CodeCastExpression(typeReference, newObjectInit);

            CodeVariableDeclarationStatement newObjectVarDecl = new CodeVariableDeclarationStatement(classMap.GetName(), MakeCamelCase(classMap.GetName()), castExp);
            CodeVariableReferenceExpression  newObjectVar     = new CodeVariableReferenceExpression(MakeCamelCase(classMap.GetName()));

            methodMember.Statements.Add(newObjectVarDecl);


            CodeMethodReturnStatement returnStmt = new CodeMethodReturnStatement(newObjectVar);

            methodMember.Statements.Add(returnStmt);

            classDecl.Members.Add(methodMember);
        }
        protected virtual XmlNode FindNodeForObject(XmlNode xmlRoot, object obj, IClassMap classMap)
        {
            IObjectManager om = this.Context.ObjectManager;
            string element = classMap.GetDocElement();

            string xpath = "";

            foreach (IPropertyMap idPropertyMap in classMap.GetIdentityPropertyMaps() )
            {
                if (xpath.Length > 0 )
                    xpath += " and "; // do not localize

                if (idPropertyMap.DocElement.Length > 0)
                    xpath += idPropertyMap.GetDocElement() + " = \"" + om.GetPropertyValue(obj, idPropertyMap.Name).ToString() + "\""; // do not localize
                else
                    xpath += "@" + idPropertyMap.GetDocAttribute() + " = \"" + om.GetPropertyValue(obj, idPropertyMap.Name).ToString() + "\""; // do not localize
            }

            xpath = element + "[" + xpath + "]";

            return xmlRoot.SelectSingleNode(xpath);
        }
        private object InstantiateObject(Type type, IClassMap classMap, XmlNode xmlObject)
        {
            IObjectManager om = this.Context.ObjectManager;
            string element = classMap.GetDocElement();

            string identity = "";
            string separator = classMap.GetIdentitySeparator();

            foreach (IPropertyMap idPropertyMap in classMap.GetIdentityPropertyMaps())
            {
                if (identity.Length > 0)
                    identity += separator;

                if (idPropertyMap.DocElement.Length > 0)
                {
                    XmlNode idNode = xmlObject.SelectSingleNode(idPropertyMap.GetDocElement());
                    if (idNode != null)
                        identity += idNode.InnerText;
                    else
                        throw new NPersistException(String.Format("Could not find id element {0} for property {1} of class {2} in element {3}", idPropertyMap.GetDocElement(), idPropertyMap.Name, type.ToString(), xmlObject.Name));
                }
                else
                {
                    string attribName = idPropertyMap.GetDocAttribute();
                    if (xmlObject.Attributes[attribName] != null)
                        identity += xmlObject.Attributes[attribName].Value;
                    else
                        throw new NPersistException(String.Format("Could not find id attribute {0} for property {1} of class {2} in element {3}", attribName, idPropertyMap.Name, type.ToString(), xmlObject.Name));
                }
            }

            return this.Context.GetObjectById(identity, type, true);
        }
Example #23
0
		private void AddTypeColumnToWhereClause(IClassMap classMap, SqlSearchCondition search) 
		{
			IColumnMap typeColumnMap = classMap.GetTypeColumnMap(); 
			if (typeColumnMap != null)
			{
				ITableMap tableMap = classMap.MustGetTableMap();
				SqlTableAlias tbl = null ;
				foreach (IPropertyMap idPropertyMap in classMap.GetIdentityPropertyMaps())
				{
					tbl = GetTableAlias(tableMap, idPropertyMap)  ;
					break;
				}

				SqlColumnAlias column = this.propertyPathTraverser.GetPropertyColumnAlias(tbl, ".NPersistTypeColumn" , typeColumnMap, ".NPersistTypeColumn");

				search.GetSqlComparePredicate(column, SqlCompareOperatorType.Equals, new SqlStringLiteral(classMap.TypeValue));				
				search.OrNext = true;
			}
		}
        public virtual IList ParseObjectIdentityKeyParts(Type type, string identity)
        {
            IClassMap classMap = this.Context.DomainMap.MustGetClassMap(type);

            return(ParseObjectIdentityKeyParts(classMap, classMap.GetIdentityPropertyMaps(), type, identity));
        }
        private string BuildObjectIdentity(object obj, IPropertyMap newPropertyMap, object newValue)
        {
            string          id             = "";
            IIdentityHelper identityHelper = obj as IIdentityHelper;

            if (identityHelper != null)
            {
                id = identityHelper.GetIdentity();
                if (id != null)
                {
                    return(id);
                }
                id = "";
            }

            IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType());
            string    sep      = classMap.IdentitySeparator;

            if (sep == "")
            {
                sep = "|";
            }

            object value;

            foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps())
            {
                if (propertyMap == newPropertyMap)
                {
                    value = newValue;
                    if (propertyMap.ReferenceType != ReferenceType.None)
                    {
                        if (!HasIdentity(value))
                        {
                            return(GetTempId(obj));
                        }

                        value = GetObjectIdentity(value);
                    }
                }
                else
                {
                    value = GetPropertyValue(obj, propertyMap.Name);
                    if (value == null || GetNullValueStatus(obj, propertyMap.Name) == true)
                    {
                        return(GetTempId(obj));
                    }
                    else if (propertyMap.ReferenceType != ReferenceType.None)
                    {
                        //this ensures that a complete id can be created ahead in case of auto-in
                        //m_ObjectManager.GetPropertyValue(obj, propertyMap.Name);
                        if (!HasIdentity(value))
                        {
                            return(GetTempId(obj));
                        }

                        value = GetObjectIdentity(value);
                    }
                }

                id += Convert.ToString(value) + sep;
            }
            if (id.Length > sep.Length)
            {
                id = id.Substring(0, id.Length - sep.Length);
            }
            if (identityHelper != null)
            {
                identityHelper.SetIdentity(id);
                if (identityHelper.HasTemporaryIdentity())
                {
                    this.Context.UnitOfWork.AddPromotedIdentity(obj);
                }
            }

            return(id);
        }
        private bool GuidMustBeTemporaryIdentity(Type type, IClassMap classMap)
        {
            IList idPropertyMaps = classMap.GetIdentityPropertyMaps();
            if (idPropertyMaps.Count > 1)
                return true;

            //should only be one id prop..
            foreach (IPropertyMap propertyMap in idPropertyMaps)
            {
                Type propType = type.GetProperty(propertyMap.Name).PropertyType;
                if (propertyMap.ReferenceType != ReferenceType.None)
                {
                    IClassMap refClassMap = this.Context.DomainMap.MustGetClassMap(propType);
                    return GuidMustBeTemporaryIdentity(propType, refClassMap);
                }
                else
                {
                    if (!propType.IsAssignableFrom(typeof(Guid)))
                        return true;
                }
            }
            return false;
        }