Exemplo n.º 1
0
 private void AddEmbeddedType(FieldInfo parent)
 {
     FieldInfo[] fis = parent.FieldType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
     foreach (FieldInfo fi in fis)
     {
         if (fi.Name.StartsWith("_ndo"))
         {
             continue;
         }
         Type     t          = fi.FieldType;
         object[] attributes = fi.GetCustomAttributes(typeof(NDOTransientAttribute), false);
         if (attributes.Length > 0)
         {
             continue;
         }
         if (StorableTypes.Contains(t))
         {
             string name = parent.Name + "." + fi.Name;
             myEmbeddedTypes.Add(name);
             if (!myPersistentFields.ContainsKey(name))
             {
                 myPersistentFields.Add(name, fi);
             }
         }
     }
 }
Exemplo n.º 2
0
 protected void Initialize()
 {
     lock (_initLock)
     {
         if (!isInitialized)
         {
             if (!StorableTypes.Any())
             {
                 throw new InvalidOperationException("No types were specified.  Call AddType for each type to store.");
             }
             isInitialized = true;
             SchemaDefinitionCreateResult = TypeSchemaGenerator.CreateSchemaDefinition(StorableTypes);
         }
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Add the specified type as a storable type.
 /// When the underlying schema is generated for the
 /// specified type it will be analyzed for its
 /// relationships to other types as necessary
 /// and those types will be included in the
 /// resulting schema
 /// </summary>
 /// <param name="type"></param>
 public override void AddType(Type type)
 {
     if (type.GetProperty("Id") == null &&
         type.GetFirstProperyWithAttributeOfType <KeyAttribute>() == null)
     {
         throw new NoIdPropertyException(type);
     }
     if (StorableTypes.Count() == 0)
     {
         SetDaoNamespace(type);
     }
     base.AddType(type);
     if (NullifyDaoAssemblyOnTypeAdd)
     {
         _daoAssembly = null;
     }
 }
Exemplo n.º 4
0
 public virtual void Initialize()
 {
     if (!isInitialized)
     {
         lock (_initLock)
         {
             if (!isInitialized)
             {
                 if (!StorableTypes.Any())
                 {
                     throw new InvalidOperationException("No types were specified.  Call AddType for each type to store.");
                 }
                 isInitialized = true;
                 TypeDaoGenerator.AddTypes(StorableTypes);
             }
         }
     }
 }
Exemplo n.º 5
0
        private void AddValueType(FieldInfo parent)
        {
            Type t = parent.FieldType;

            List <FieldInfo>    publicFields = new List <FieldInfo>();
            List <PropertyInfo> publicProps  = new List <PropertyInfo>();

            PropertyInfo[] pis = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo pi in pis)
            {
                if (pi.CanRead && pi.CanWrite && StorableTypes.Contains(pi.PropertyType))
                {
                    publicProps.Add(pi);
                }
            }
            FieldInfo[] fis = t.GetFields(BindingFlags.Public | BindingFlags.Instance);
            foreach (FieldInfo fi in fis)
            {
                if (StorableTypes.Contains(fi.FieldType))
                {
                    publicFields.Add(fi);
                }
            }
            if (publicProps.Count > 0 || publicFields.Count > 0)
            {
                foreach (PropertyInfo pi in publicProps)
                {
                    myFields.Add(parent.Name + "." + pi.Name);
                    if (!myPersistentFields.ContainsKey(parent.Name + "." + pi.Name))
                    {
                        myPersistentFields.Add(parent.Name + "." + pi.Name, pi);
                    }
                }
                foreach (FieldInfo fi in publicFields)
                {
                    myFields.Add(parent.Name + "." + fi.Name);
                    if (!myPersistentFields.ContainsKey(parent.Name + "." + fi.Name))
                    {
                        myPersistentFields.Add(parent.Name + "." + fi.Name, fi);
                    }
                }
            }
        }
Exemplo n.º 6
0
        private void AnalyzeFields()
        {
            IList relations = new ArrayList();
            var   fis       = this.classType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(fi => !fi.IsInitOnly);

            foreach (FieldInfo fi in fis)
            {
                string fname = fi.Name;
                if (fname.StartsWith("_ndo"))
                {
                    continue;
                }
                if (fi.FieldType.IsSubclassOf(typeof(System.Delegate)))
                {
                    continue;
                }

                object[] attributes = fi.GetCustomAttributes(false);
                bool     cont       = false;
                foreach (System.Attribute attr in attributes)
                {
                    if (attr is NDOTransientAttribute)
                    {
                        cont = true;
                        break;
                    }
                    if (attr is NDORelationAttribute)
                    {
                        this.relations.Add(new RelationNode(fi, (NDORelationAttribute)attr, this));
                        cont = true;
                        break;
                    }
                }
                if (cont)
                {
                    continue;
                }

                // Field type is persistent - assume relation with element multiplicity.
                if (typeof(IPersistenceCapable).IsAssignableFrom(fi.FieldType))
                {
                    NDORelationAttribute nra = new NDORelationAttribute(fi.FieldType, RelationInfo.Default);
                    this.relations.Add(new RelationNode(fi, nra, this));
                    continue;
                }

                // Field is a collection - assume that it is either a relation or a transient field.
                if (GenericIListReflector.IsGenericIList(fi.FieldType))
                {
                    if (typeof(IPersistenceCapable).IsAssignableFrom(fi.FieldType.GetGenericArguments()[0]))
                    {
                        NDORelationAttribute nra = new NDORelationAttribute(fi.FieldType.GetGenericArguments()[0], RelationInfo.Default);
                        this.relations.Add(new RelationNode(fi, nra, this));
                    }
                    else
                    {
                        Console.WriteLine("Warning: The field " + this.name + "." + fi.Name + " is a generic collection to a nonpersistent type. NDO assumes, that this field is transient.");
                    }
                    continue;
                }

                if (!fi.FieldType.IsGenericParameter && fi.FieldType.IsClass && fi.FieldType != typeof(string) && fi.FieldType != typeof(byte[]))
                {
                    this.embeddedTypes.Add(new EmbeddedTypeNode(fi));
                }
                else
                {
                    FieldNode fn = new FieldNode(fi);
                    this.fields.Add(fn);
                    Type ft = fi.FieldType;
                    if (ft.IsValueType && !ft.IsEnum && !StorableTypes.Contains(ft))
                    {
                        ValueTypes.Instance.Add(new ValueTypeNode(ft));
                    }
                }
            }

            // If there is more than one relation to the same target type
            // without relation name, assign a relation name automatically
            foreach (var group in this.relations.GroupBy(r => r.RelatedType))
            {
                if (group.Count() < 2)
                {
                    continue;
                }
                int countWithoutName = 0;
                foreach (var rel in group)
                {
                    if (string.IsNullOrEmpty(rel.RelationName))
                    {
                        if (countWithoutName > 0)
                        {
                            string relname = rel.Name;
                            if (relname[0] == '<')
                            {
                                int q = relname.IndexOf('>');
                                if (q == -1)
                                {
                                    q = relname.Length;
                                }
                                relname = relname.Substring(1, q - 1);
                            }
                            rel.RelationName = relname;
                        }
                        countWithoutName++;
                    }
                }
            }
        }
Exemplo n.º 7
0
        private void AddFields(Type t)
        {
            int startind = myFields.Count;

            if (!IsPersistentType(t))
            {
                return;
            }
            FieldInfo[] finfos = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            foreach (FieldInfo fi in finfos)
            {
                string fname;
                if ((fname = fi.Name).StartsWith("_ndo"))
                {
                    continue;
                }

                if (!fi.IsPrivate)
                {
                    continue;
                }

                if (fi.IsInitOnly)
                {
                    continue;
                }

                Type ft = fi.FieldType;

                // Typen, die nicht speicherbar sind.
                if (ft.IsInterface)
                {
                    continue;
                }

                object[] attributes = fi.GetCustomAttributes(false);
                bool     cont       = false;
                foreach (System.Attribute attr in attributes)
                {
                    string name;
                    if ((name = attr.GetType().Name) == "NDOTransientAttribute")
                    {
                        cont = true;
                    }
                    if (name == "NDORelationAttribute")
                    {
                        cont = true;
                    }
                }
                if (cont)
                {
                    continue;
                }

                if (StorableTypes.Contains(ft))
                {
                    this.myFields.Add(fname);
                    if (!myPersistentFields.ContainsKey(fname))
                    {
                        myPersistentFields.Add(fname, fi);
                    }
                }
                else if (ft.IsValueType)
                {
                    AddValueType(fi);
                }
                else if (ft.IsClass)
                {
                    AddEmbeddedType(fi);
                }
                // Alle anderen Fälle werden ignoriert
            }
            // Pro Klasse werden die Strings sortiert
            // Sortiert wird ohne Länderberücksichtigung, sonst sortiert die
            // Anwendung auf norwegischen Systemen anders als auf deutschen.
            if (myFields.Count - startind > 0)
            {
                FieldSorter fs = new FieldSorter();
                myFields.Sort(startind, myFields.Count - startind, fs);
            }
        }