Пример #1
0
        /// <summary>
        /// Recursively builds entity graph hierarchy.
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="parent"></param>
        /// <param name="relationship"></param>
        private EntityGraph(Type entityType, EntityGraph parent, Relationship relationship)
        {
            _repos = MapRepository.Instance;

            _entityType       = entityType;
            _parent           = parent;
            _relationship     = relationship;
            IsParentReference = !IsRoot && AnyParentsAreOfType(entityType);
            if (!IsParentReference)
            {
                _columns = _repos.GetColumns(entityType);
            }

            _relationships    = _repos.GetRelationships(entityType);
            _children         = new List <EntityGraph>();
            Member            = relationship != null ? relationship.Member : null;
            _entityReferences = new Dictionary <string, EntityReference>();

            if (IsParentReference)
            {
                return;
            }

            // Create a new EntityGraph for each child relationship that is not lazy loaded
            foreach (Relationship childRelationship in Relationships)
            {
                if (!childRelationship.IsLazyLoaded)
                {
                    _children.Add(new EntityGraph(childRelationship.RelationshipInfo.EntityType, this, childRelationship));
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Returns an entity of type T.
        /// </summary>
        /// <typeparam name="T">The type of entity that is to be instantiated and loaded with values.</typeparam>
        /// <param name="sql">The SQL command to execute.</param>
        /// <returns>An instantiated and loaded entity of type T.</returns>
        public T Find <T>(string sql, T ent)
        {
            if (string.IsNullOrEmpty(sql))
            {
                throw new ArgumentNullException("sql", "A stored procedure name has not been specified for 'Find'.");
            }

            Type entityType = typeof(T);

            Command.CommandText = sql;

            MapRepository       repository = MapRepository.Instance;
            ColumnMapCollection mappings   = repository.GetColumns(entityType);

            bool isSimpleType = DataHelper.IsSimpleType(typeof(T));

            try
            {
                OpenConnection();
                var mappingHelper = new MappingHelper(this);

                using (DbDataReader reader = Command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        if (isSimpleType)
                        {
                            return(mappingHelper.LoadSimpleValueFromFirstColumn <T>(reader));
                        }
                        else
                        {
                            if (ent == null)
                            {
                                ent = (T)mappingHelper.CreateAndLoadEntity <T>(mappings, reader, false);
                            }
                            else
                            {
                                mappingHelper.LoadExistingEntity(mappings, reader, ent, false);
                            }
                        }
                    }
                }
            }
            finally
            {
                CloseConnection();
            }

            return(ent);
        }
Пример #3
0
        /// <summary>
        /// Recursively builds entity graph hierarchy.
        /// </summary>
        /// <param name="entityType"></param>
        /// <param name="parent"></param>
        /// <param name="relationship"></param>
        private EntityGraph(Type entityType, EntityGraph parent, Relationship relationship, GraphIndexGenerator indexGen)
        {
            GraphIndex = indexGen.Next();

            _repos = MapRepository.Instance;

            if (relationship != null && relationship.IsLazyLoaded)
            {
                _entityType = relationship.GetLazyLoadedEntityType();
            }
            else
            {
                _entityType = entityType;
            }
            _parent       = parent;
            _relationship = relationship;

            var anyParentsAreOfSameType = AnyParentsAreOfType(_entityType);

            IsParentReference = relationship != null && !relationship.IsEagerLoaded && !relationship.IsLazyLoaded && anyParentsAreOfSameType;
            if (!IsParentReference)
            {
                _columns = _repos.GetColumns(_entityType);
            }
            _relationships    = _repos.GetRelationships(_entityType);
            _children         = new List <EntityGraph>();
            Member            = relationship != null ? relationship.Member : null;
            _entityReferences = new Dictionary <string, EntityReference>();

            if (anyParentsAreOfSameType)
            {
                return;
            }

            // Create a new EntityGraph for each child relationship
            foreach (Relationship childRelationship in this.Relationships)
            {
                _children.Add(new EntityGraph(childRelationship.RelationshipInfo.EntityType, this, childRelationship, indexGen));
            }
        }