示例#1
0
        public TEntity GetSingleOrDefaultResult(string statement, ICollection <QueryParameter> parameters)
        {
            TEntity entity = default(TEntity);

            using (var command = this.CreateCommand())
            {
                command.CommandText = statement;
                command.CreateParametersFromQuery(parameters);

                // command.DisplayQuery();
                this.DisplayCommand(command);

                if (this._hydrator != null)
                {
                    entity = _hydrator.HydrateEntity <TEntity>(command);

                    // force lazy loading on hydrated entity (if possible):
                    if (entity != null)
                    {
                        if (typeof(ILazyLoadSpecification).IsAssignableFrom(entity.GetType()))
                        {
                            ((ILazyLoadSpecification)entity).IsLazyLoadingEnabled = true;
                        }
                    }
                }
            }

            return(entity);
        }
示例#2
0
        public TEntity GetById(object id)
        {
            TEntity entity = default(TEntity);

            using (var command = this.CreateCommand())
            {
                var tableInfo = this.MetadataStore.GetTableInfo <TEntity>();

                if (tableInfo.PrimaryKey == null || tableInfo.PrimaryKey.Column == null)
                {
                    throw new InvalidOperationException(
                              string.Format("The following entity '{0}' does not have a primary key assigned to a " +
                                            "data field for retreiving entity instances. Please assign the attribute '{1}' to the data property " +
                                            "that represents the primary key.",
                                            typeof(TEntity).FullName,
                                            typeof(PrimaryKeyAttribute).Name));
                }

                if (id.GetType() != tableInfo.PrimaryKey.Column.PropertyType)
                {
                    throw new InvalidOperationException("The data value for the retreving the entity " + typeof(TEntity).FullName
                                                        + " does not match the type definition of " +
                                                        tableInfo.PrimaryKey.Column.PropertyType.FullName + " for the primary key.");
                }

                var query = tableInfo.GetSelectStatmentForAllFields();
                query = tableInfo.AddWhereClauseById(query, id);
                command.CreateAndAddInputParameterForPrimaryKeyValue(tableInfo, tableInfo.PrimaryKey, id);
                command.CommandText = query;

                // command.DisplayQuery();
                this.DisplayCommand(command);

                if (_hydrator != null)
                {
                    entity = _hydrator.HydrateEntity <TEntity>(command);
                }

                // force lazy loading on hydrated entity (if possible):
                if (entity != null)
                {
                    if (typeof(ILazyLoadSpecification).IsAssignableFrom(entity.GetType()))
                    {
                        ((ILazyLoadSpecification)entity).IsLazyLoadingEnabled = true;
                    }
                }

                return(entity);
            }
        }
示例#3
0
        public TEntity GetUniqueResult(string procedure, IDictionary <string, object> parameters)
        {
            TEntity entity = default(TEntity);

            using (var command = this.CreateCommand())
            {
                command.CommandText = procedure;
                command.CommandType = CommandType.StoredProcedure;
                command.CreateParametersFromDictionary(parameters);

                // command.DisplayQuery();
                this.DisplayCommand(command);

                if (this._hydrator != null)
                {
                    entity = _hydrator.HydrateEntity <TEntity>(command);
                }
            }

            return(entity);
        }