Пример #1
0
 /// <summary>
 /// The value of attribute Table
 /// </summary>
 /// <typeparam name="type"></typeparam>
 /// <returns></returns>
 public static String TableName(this Type type)
 {
     if (CachedTableNames.ContainsKey(type))
     {
         return(CachedTableNames[type]);
     }
     return(CachedTableNames.GetOrAdd(type, (type.GetCustomAttribute <Table>()?.Name ?? type.Name).CleanName()));
 }
Пример #2
0
 /// <summary>
 /// The value of attribute Table
 /// </summary>
 /// <typeparam name="type"></typeparam>
 /// <returns></returns>
 public static Table TableName(this Type type)
 {
     if (CachedTableNames.ContainsKey(type))
     {
         return(CachedTableNames[type]);
     }
     return(CachedTableNames.GetOrAdd(type, (type.GetCustomAttribute <Table>() ?? new Table(type.Name))));
 }
        public LightDataLinqToNoSql(Type type, Transaction.Transaction transaction)
        {
            _transaction  = transaction;
            _obType       = type.GetActualType();
            DataBaseTypes = transaction.DataBaseTypes;
            var key = _obType.FullName + DataBaseTypes;

            if (!CachedColumns.ContainsKey(key))
            {
                _columns = CachedColumns.GetOrAdd(key, _transaction.GetColumnSchema(_obType).Select(x => $"{_obType.TableName().GetName(DataBaseTypes)}.[{x.Key}]").ToList());
            }
            else
            {
                _columns = CachedColumns[key];
            }
            _primaryId = _obType.GetPrimaryKey()?.GetPropertyName();
        }
        /// <summary>
        /// Constructs an <see cref="NpgsqlSimpleTypeHandler{TDefault}"/>.
        /// </summary>
        protected NpgsqlSimpleTypeHandler()
        {
            // Get code-generated delegates for non-generic ValidateAndGetLength/WriteWithLengthInternal
            var item = NonGenericDelegateCache.GetOrAdd(GetType(), new Tuple <NonGenericValidateAndGetLength, NonGenericWriteWithLength>(GenerateNonGenericValidationMethod(GetType()), GenerateNonGenericWriteMethod(GetType(), typeof(INpgsqlSimpleTypeHandler <>))));

            _nonGenericValidateAndGetLength = item.Item1;
            _nonGenericWriteWithLength      = item.Item2;
        }
Пример #5
0
 /// <summary>
 /// Get the Primary key from type
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static IFastDeepClonerProperty GetPrimaryKey(this Type type)
 {
     if (CachedPrimaryKeys.ContainsKey(type))
     {
         return(CachedPrimaryKeys[type]);
     }
     return(CachedPrimaryKeys.GetOrAdd(type, DeepCloner.GetFastDeepClonerProperties(type).FirstOrDefault(x => x.ContainAttribute <PrimaryKey>())));
 }
Пример #6
0
        /// <summary>
        /// Get PropertyName from the cashed Properties
        /// </summary>
        /// <param name="prop"></param>
        /// <returns></returns>
        public static string GetPropertyName(this IFastDeepClonerProperty prop)
        {
            if (CachedPropertyNames.ContainsKey(prop))
            {
                return(CachedPropertyNames[prop]);
            }

            return(CachedPropertyNames.GetOrAdd(prop, (prop.GetCustomAttribute <PropertyName>()?.Name ?? prop.Name).CleanName()));
        }
Пример #7
0
        /// <summary>
        /// Attach object to WorkEntity to track changes
        /// </summary>
        /// <param name="objcDbEntity"></param>
        /// <param name="overwrite"></param>
        public void Attach(object objcDbEntity, bool overwrite = false)
        {
            if (objcDbEntity == null)
            {
                throw new NullReferenceException("DbEntity cant be null");
            }
            if (Extension.ObjectIsNew(objcDbEntity.GetPrimaryKeyValue()))
            {
                throw new NullReferenceException("Id is IsNullOrEmpty, it cant be attached");
            }
            var key = objcDbEntity.EntityKey();

            if (_attachedObjects.ContainsKey(key))
            {
                if (overwrite)
                {
                    _attachedObjects.GetOrAdd(key, this.Clone(objcDbEntity, CloneLevel.FirstLevelOnly), true);
                }
            }
            else
            {
                _attachedObjects.GetOrAdd(key, this.Clone(objcDbEntity, CloneLevel.FirstLevelOnly));
            }
        }
Пример #8
0
        /// <summary>
        /// Convert to unknown type
        /// </summary>
        /// <param name="command"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public IList DataReaderConverter(DbCommandExtended command, Type type)
        {
            IList result;

            ValidateConnection();
            try
            {
                var o = command.Command.ExecuteReader();
                OpenedDataReaders.GetOrAdd(o, true);
                result = Extension.DataReaderConverter(this, o, command, type);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                CloseifPassible();
            }

            return(result);
        }
Пример #9
0
        /// <summary>
        /// Convert to unknown type
        /// </summary>
        /// <param name="command"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public IList DataReaderConverter(ISqlCommand command, Type type)
        {
            IList result;

            ValidateConnection();
            GlobalConfiguration.Log?.Info("Execute", command);
            try
            {
                var o = command.Command.ExecuteReader();
                OpenedDataReaders.GetOrAdd(o, true);
                result = Extension.DataReaderConverter(this, o, command, type);
            }
            catch (Exception e)
            {
                throw new EntityException(e.Message);
            }
            finally
            {
                CloseifPassible();
            }

            return(result);
        }
        internal static object Creator(this Type type)
        {
            if (!DefaultConstructor.ContainsKey(type))
            {
                DefaultConstructor.Add(type, type.GetConstructor(Type.EmptyTypes));
            }


            if (DefaultConstructor[type] != null)
            {
#if NETSTANDARD2_0 || NETSTANDARD1_3 || NETSTANDARD1_5
                if (CachedConstructor.ContainsKey(type))
                {
                    return(CachedConstructor[type].Invoke());
                }
                return(CachedConstructor.GetOrAdd(type, Expression.Lambda <Func <object> >(Expression.New(type)).Compile()).Invoke());
#else
                if (CachedDynamicMethod.ContainsKey(type))
                {
                    return(CachedDynamicMethod[type]());
                }

                var emptyConstructor = DefaultConstructor[type];
                var dynamicMethod    = new System.Reflection.Emit.DynamicMethod("CreateInstance", type, Type.EmptyTypes, true);
                System.Reflection.Emit.ILGenerator ilGenerator = dynamicMethod.GetILGenerator();
                ilGenerator.Emit(System.Reflection.Emit.OpCodes.Nop);
                ilGenerator.Emit(System.Reflection.Emit.OpCodes.Newobj, emptyConstructor);
                ilGenerator.Emit(System.Reflection.Emit.OpCodes.Ret);
                return(CachedDynamicMethod.GetOrAdd(type, (ObjectActivator)dynamicMethod.CreateDelegate(typeof(ObjectActivator)))());
#endif
            }
            else
            {
                return(FormatterServices.GetUninitializedObject(type));
            }
        }