示例#1
0
 private void LoadFromDatabase()
 {
     if (this.buffer == null)
     {
         //go to the database and get the buffer;
         this.buffer = entity.GetPropertyValue(propName, true) as byte[];
     }
 }
示例#2
0
        /// <summary>
        /// Converts a datatable into a set of instances of baseClass.
        /// </summary>
        /// <param name="table">A System.Data.DataTable with raw database data.</param>
        /// <param name="lazyLoading">Indicates whether to enable the lazyLoading on created instances.</param>
        /// <param name="baseClass">Defines which type to load.</param>
        /// <exception cref="System.ArgumentNullException">Occurs when table or baseClass parameters are null.</exception>
        /// <returns>An array of entities.</returns>
        internal static EntityBase[] BindRows(DataTable table, SearchOptions searchOptions, string baseAlias)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }
            if (searchOptions == null)
            {
                throw new ArgumentNullException("searchOptions");
            }
            if (searchOptions.eagerLoading.Count == 0)
            {
                //TODO: Check if this is necessary by performance needs.
                EntityBase[] instances = (EntityBase[])(Array.CreateInstance(searchOptions.baseType, table.Rows.Count));
                for (int i = 0; i <= instances.Length - 1; i++)
                {
                    instances[i] = (EntityBase)(Activator.CreateInstance(searchOptions.baseType));
                    instances[i].Bind(searchOptions.LazyLoading, baseAlias, null, table.Rows[i]);
                }
                return(instances);
            }
            else
            {
                Type  listof    = typeof(List <>).MakeGenericType(searchOptions.baseType);
                IList instances = (IList)Activator.CreateInstance(listof);
                //lets get metadata for baseClass and other things requested by the user.

                List <FieldInfo[]> meta = new List <FieldInfo[]>();
                meta.Add(EntityBase.GetPrimaryKeys(searchOptions.baseType));

                List <ForeignKeyInfo> keys = new List <ForeignKeyInfo>(); //i need this to get fks by index
                foreach (ForeignKeyInfo fkInfo in searchOptions.eagerLoading.Keys)
                {
                    keys.Add(fkInfo);
                    meta.Add(EntityBase.GetPrimaryKeys(fkInfo.ElementType)); //pks of each eagerloading
                }

                List <string>[] lastPk = new List <string> [meta.Count];
                for (int i = 0; i < meta.Count; i++)
                {
                    lastPk[i] = new List <string>();
                }

                foreach (DataRow dr in table.Rows)
                {
                    string basePk = string.Empty;
                    foreach (FieldInfo fi in meta[0])
                    {
                        string alias = fi.DataFieldName;
                        basePk += "||" + dr[alias].ToString();
                    }

                    for (int i = 0; i < meta.Count; i++)
                    {
                        ForeignKeyInfo key = (i == 0 ? null : keys[i - 1]);

                        string currentPKs = basePk;
                        if (i > 0)
                        {
                            foreach (FieldInfo fi in meta[i])
                            {
                                string alias = (i == 0 ? fi.DataFieldName : searchOptions.eagerLoading[key] + fi.DataFieldName);
                                currentPKs += "||" + dr[alias].ToString();
                            }
                        }

                        if (!string.IsNullOrEmpty(currentPKs) && !lastPk[i].Contains(currentPKs))
                        {
                            lastPk[i].Add(currentPKs);
                            EntityBase instance = (EntityBase)Activator.CreateInstance((i == 0 ? searchOptions.baseType : key.ElementType));
                            instance.Bind(searchOptions.LazyLoading, (i == 0 ? null : searchOptions.eagerLoading[key]), null, dr);

                            if (key == null)
                            {
                                instances.Add(instance);
                            }
                            else
                            {
                                int        index        = lastPk[0].IndexOf(basePk);
                                EntityBase baseInstance = (EntityBase)instances[index];
                                if (key.IsArray)
                                {
                                    baseInstance.lazyEnabled = false;
                                    IList value = (IList)baseInstance.GetPropertyValue(key.RelatedProperty.Name, false);
                                    baseInstance.lazyEnabled = true;
                                    value.Add(instance);
                                }
                                else
                                {
                                    baseInstance.SetPropertyValue(key.RelatedProperty.Name, instance);
                                }
                            }
                        }
                    }
                }

                return((EntityBase[])instances.GetType().GetMethod("ToArray").Invoke(instances, null));
            }
        }