Ejemplo n.º 1
0
        public DbCommand GetUpdateCommand(object entity, IEnumerable <string> fieldsToUpdate, EntityMetadata entityMetadata)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (fieldsToUpdate == null)
            {
                throw new ArgumentNullException(nameof(fieldsToUpdate));
            }
            if (entityMetadata == null)
            {
                throw new ArgumentNullException(nameof(entityMetadata));
            }
            Type      entityType = entity.GetType();
            string    fieldsKey  = fieldsToUpdate.Join();
            DbCommand command;
            var       key = new TypeStringKey(entityType, fieldsKey);

            if (updateCommandCache.TryGetValue(key, out command))
            {
                SetParameterValuesFromObject(command, entity, entityMetadata);
            }
            else
            {
                command = GenerateUpdateCommand(entity, fieldsToUpdate);
                updateCommandCache.Add(key, command);
            }
            return(command);
        }
Ejemplo n.º 2
0
        public static Func <IDataReader, object> GetFactory(this IDataReader reader, Type entityType)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            if (entityType == null)
            {
                throw new ArgumentNullException(nameof(entityType));
            }
            Func <IDataReader, object> factory1 = null;
            var key = new TypeStringKey(entityType, reader.GetMetadataSummary());

            if (factoryCache.TryGetValue(key, out factory1))
            {
                return(factory1);
            }

            return(keyLocker.ExecuteSynchronized(key, k =>
            {
                Func <IDataReader, object> factory2;
                if (!factoryCache.TryGetValue(k, out factory2))
                {
                    var builder = new FromDataReaderFactoryBuilder(entityType, reader);
                    factory2 = builder.CreateFactory();
                    factoryCache[key] = factory2;
                }
                return factory2;
            }));
        }