Пример #1
0
        /// <summary>
        ///     Creates a new Instance based on possible Ctor's and the given
        ///     <paramref name="reader" />
        /// </summary>
        /// <returns></returns>
        public static object SetPropertysViaReflection(
            this DbClassInfoCache type,
            IDataRecord reader,
            DbAccessType?accessType = null,
            DbConfig config         = null)
        {
            if (reader == null)
            {
                return(null);
            }

            bool created;
            var  source = DbAccessLayer.CreateInstance(type, reader, out created);

            if (created)
            {
                return(source);
            }

            if (config == null)
            {
                config = new DbConfig(true);
            }

#pragma warning disable 618
            return(DbAccessLayer.ReflectionPropertySet(config, source, type, reader, null, accessType));

#pragma warning restore 618
        }
 /// <summary>
 ///     Returns an enumerator that iterates through the collection.
 /// </summary>
 /// <returns>
 ///     An enumerator that can be used to iterate through the collection.
 /// </returns>
 IEnumerator <TEntity> IEnumerable <TEntity> .GetEnumerator()
 {
     CheckCreatedElseThrow();
     return(Base.Values.ToArray().Select(s =>
     {
         if (_keepOriginalObject)
         {
             return s;
         }
         bool fullyLoaded;
         return (TEntity)DbAccessLayer.CreateInstance(
             _typeInfo,
             new ObjectDataRecord(s, _config, 0),
             out fullyLoaded,
             DbAccessType.Unknown);
     }).GetEnumerator());
 }
 /// <summary>
 ///     Thread save
 /// </summary>
 /// <returns></returns>
 public TEntity[] ToArray()
 {
     lock (SyncRoot)
     {
         return(Base.Values.Select(s =>
         {
             if (_keepOriginalObject)
             {
                 return s;
             }
             bool fullyLoaded;
             return (TEntity)DbAccessLayer.CreateInstance(
                 _typeInfo,
                 new ObjectDataRecord(s, _config, 0),
                 out fullyLoaded,
                 DbAccessType.Unknown);
         }).ToArray());
     }
 }
Пример #4
0
        /// <summary>
        ///     Creates a new Instance based on possible Ctor's and the given
        ///     <paramref name="reader" />
        /// </summary>
        /// <returns></returns>
        public static object SetPropertiesViaReflection(
            this DbClassInfoCache type,
            XmlDataRecord reader,
            DbAccessType?accessType = null,
            DbConfig config         = null)
        {
            if (reader == null)
            {
                return(null);
            }

            var eagerReader = new EagarDataRecord();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                eagerReader.Add(eagerReader.GetName(i), eagerReader[i]);
            }

            bool created;
            var  source = DbAccessLayer.CreateInstance(type, eagerReader, out created);

            if (created)
            {
                return(source);
            }

            if (config == null)
            {
                config = new DbConfig(true);
            }

#pragma warning disable 618
            return(DbAccessLayer.ReflectionPropertySet(config, source, type, eagerReader, new DbAccessLayer.ReflectionSetCacheModel(), accessType));

#pragma warning restore 618
        }
        /// <summary>
        ///     Adds a new Item to the Table
        /// </summary>
        /// <param name="item"></param>
        public void Add(TEntity item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            var elementToAdd = item;

            CheckCreatedElseThrow();
            if (!Contains(elementToAdd))
            {
                var hasTransaction = AttachTransactionIfSet(elementToAdd,
                                                            CollectionStates.Added);
                Constraints.Check.Enforce(elementToAdd);
                Constraints.Unique.Enforce(elementToAdd);
                var id = SetNextId(elementToAdd);

                //Check Data integrity
                if (!hasTransaction)
                {
                    var ex = EnforceCheckConstraints(elementToAdd);

                    if (ex != null)
                    {
                        throw ex;
                    }
                }
                TriggersUsage.For.OnInsert(elementToAdd);
                Constraints.Default.Enforce(elementToAdd);

                if (!_keepOriginalObject)
                {
                    bool fullyLoaded;
                    elementToAdd = (TEntity)DbAccessLayer.CreateInstance(
                        _typeInfo,
                        new ObjectDataRecord(item, _config, 0),
                        out fullyLoaded,
                        DbAccessType.Unknown);
                    if (!fullyLoaded || elementToAdd == null)
                    {
                        throw new InvalidOperationException(string.Format("The given type did not provide a Full ado.net constructor " +
                                                                          "and the setting of the propertys did not succeed. " +
                                                                          "Type: '{0}'", typeof(TEntity)));
                    }
                }

                if (!TriggersUsage.InsteadOf.OnInsert(elementToAdd))
                {
                    Base.Add(id, elementToAdd);
                    IndexesUsage.Add(item);
                }
                try
                {
                    TriggersUsage.After.OnInsert(elementToAdd);
                }
                catch (Exception)
                {
                    Base.Remove(id);
                    throw;
                }
                Constraints.Unique.ItemAdded(elementToAdd);
            }
        }