Example #1
0
        public static DynamicParameters Generate(T obj, bool removeUnused = false)
        {
            var parameters = new DynamicParameters();

            foreach (var name in PropertyGetterCache.Keys)
            {
                var value = PropertyGetterCache[name](obj);
                var attr  = AttributeCache[name];

                if (attr.IsDataTable || attr.IsEnumerableDataRecord)
                {
                    if (value is DataTable dataTablePropertyValue)
                    {
                        var tableName = !string.IsNullOrEmpty(attr.TableName) ? attr.TableName : dataTablePropertyValue.TableName;
                        parameters.Add(name, dataTablePropertyValue.AsTableValuedParameter(tableName), attr.DbType, attr.Direction, attr.Size, attr.Precision, attr.Scale);
                    }
                    if (value is IEnumerable <IDataRecord> enumerableDataRecordPropertyValue)
                    {
                        parameters.Add(name, enumerableDataRecordPropertyValue.AsTableValuedParameter(attr.TableName), attr.DbType, attr.Direction, attr.Size, attr.Precision, attr.Scale);
                    }
                }
                else
                {
                    parameters.Add(name, value, attr.DbType, attr.Direction, attr.Size, attr.Precision, attr.Scale);
                }
            }

            parameters.RemoveUnused = removeUnused;
            return(parameters);
        }
Example #2
0
        static Parakeet()
        {
            var type = typeof(T);

            var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
                             .Where(p => (p.PropertyType.IsParakeetEligible() && !p.GetCustomAttributes <ParakeetIgnoreAttribute>().Any()) || p.GetCustomAttributes <ParakeetAttribute>().Any())
                             .ToList();

            foreach (var property in properties)
            {
                var attr = property.GetCustomAttributes <ParakeetAttribute>().FirstOrDefault();
                if (attr == null)
                {
                    attr = new ParakeetAttribute {
                        Name = property.Name
                    }
                }
                ;
                if (string.IsNullOrEmpty(attr.Name))
                {
                    attr.Name = property.Name;
                }

                attr.IsDataTable            = property.PropertyType == typeof(DataTable);
                attr.IsEnumerableDataRecord = typeof(IEnumerable <IDataRecord>).IsAssignableFrom(property.PropertyType);

                var func = Parakeet.GenerateDelegate <T>(property.GetGetMethod());

                AttributeCache.AddOrUpdate(attr.Name, attr, (t, p) => attr);
                PropertyGetterCache.AddOrUpdate(attr.Name, func, (t, f) => func);
            }
        }
        public void TestIndexerTypeNotRegistered()
        {
            var cache = new PropertyGetterCache();

            var typeCache = cache[typeof(PropertyGetterCacheTests)];
            Assert.IsNotNull(typeCache);
            Assert.AreEqual(typeof(PropertyGetterCacheTests), typeCache.Type);
        }
        public void TestRegisterTNull()
        {
            var cache = new PropertyGetterCache();

            Assert.ThrowsException<ArgumentNullException>(
                () => cache.Register<PropertyGetterCacheTests>(
                    nameof(TestContext),
                    null
                )
            );
        }
        public void TestGetNotRegistered()
        {
            var cache = new PropertyGetterCache();

            Func<object, object> getter = cache.Get(
                typeof(PropertyGetterCacheTests),
                nameof(TestContext)
            );

            Assert.AreSame(TestContext, getter(this));
        }
        public void TestIndexer()
        {
            var cache = new PropertyGetterCache();
            cache.Register(
                typeof(PropertyGetterCacheTests),
                nameof(TestContext),
                (obj) => 5
            );

            var typeCache = cache[typeof(PropertyGetterCacheTests)];

            Assert.IsNotNull(typeCache);
            Assert.AreEqual(typeof(PropertyGetterCacheTests), typeCache.Type);
        }
        public void TestGetRegisteredGeneric()
        {
            var cache = new PropertyGetterCache();
            cache.Register<PropertyGetterCacheTests>(
                nameof(TestContext),
                (obj) => 5
            );

            Func<object, object> getter = cache.Get(
                typeof(PropertyGetterCacheTests),
                nameof(TestContext)
            );

            Assert.AreEqual(5, getter(this));
        }
Example #8
0
        /// <summary>
        /// Initializes a new view model using the given service locator.
        /// </summary>
        /// <param name="services">The service locator. If null, the default will be used.</param>
        protected ValidatingViewModel(ISimpleIoc services)
            : base(services)
        {
            PropertyGetterCache cache = PropertyGetterCache.Default;

            if (Services.IsRegistered <PropertyGetterCache>())
            {
                cache = services.GetInstance <PropertyGetterCache>();
            }

            Type thisType = GetType();

            GetterCache = cache[thisType];

            PropertyChanged += PropertyChangedToErrorsChanged;
        }
        public void TestRegisterAlreadyRegistered()
        {
            var cache = new PropertyGetterCache();

            cache.Register(
                typeof(PropertyGetterCacheTests),
                nameof(TestContext),
                (obj) => 5
            );
            cache.Register(
                typeof(PropertyGetterCacheTests),
                nameof(TestContext),
                (obj) => 7
            );

            var getter = cache.Get(
                typeof(PropertyGetterCacheTests),
                nameof(TestContext)
            );

            Assert.AreEqual(7, getter(this));
        }