Inheritance: IObjectWrapper
 /// <summary> 
 /// Create the job instance, populating it with property values taken
 /// from the scheduler context, job data map and trigger data map.
 /// </summary>
 protected override object CreateJobInstance(TriggerFiredBundle bundle)
 {
     ObjectWrapper ow = new ObjectWrapper(bundle.JobDetail.JobType);
     if (IsEligibleForPropertyPopulation(ow.WrappedInstance))
     {
         MutablePropertyValues pvs = new MutablePropertyValues();
         if (schedulerContext != null)
         {
             pvs.AddAll(schedulerContext);
         }
         pvs.AddAll(bundle.JobDetail.JobDataMap);
         pvs.AddAll(bundle.Trigger.JobDataMap);
         if (ignoredUnknownProperties != null)
         {
             for (int i = 0; i < ignoredUnknownProperties.Length; i++)
             {
                 string propName = ignoredUnknownProperties[i];
                 if (pvs.Contains(propName))
                 {
                     pvs.Remove(propName);
                 }
             }
             ow.SetPropertyValues(pvs);
         }
         else
         {
             ow.SetPropertyValues(pvs, true);
         }
     }
     return ow.WrappedInstance;
 }
        public void GetNonExistantPropertyException()
		{
			ObjectWrapper wrapper = new ObjectWrapper(new TestObject("Rick", 23));
			PropertyAccessExceptionsException ex
				= new PropertyAccessExceptionsException(wrapper, new PropertyAccessException[]
				{
				});
			PropertyAccessException nullException = ex.GetPropertyAccessException("Age");
			Assert.IsNull(nullException);
        }
Example #3
0
		/// <summary> 
		/// This implementation applies the passed-in job data map as object property
		/// values, and delegates to <code>ExecuteInternal</code> afterwards.
		/// </summary>
		/// <seealso cref="ExecuteInternal" />
        public void Execute(IJobExecutionContext context)
		{
			try
			{
				ObjectWrapper bw = new ObjectWrapper(this);
				MutablePropertyValues pvs = new MutablePropertyValues();
				pvs.AddAll(context.Scheduler.Context);
				pvs.AddAll(context.MergedJobDataMap);
				bw.SetPropertyValues(pvs, true);
			}
			catch (SchedulerException ex)
			{
				throw new JobExecutionException(ex);
			}
			ExecuteInternal(context);
		}
        /// <summary>
        /// Post Domain轉DTO
        /// </summary>
        /// <param name="postList">PostDomain</param>
        /// <returns>ReportPostVO</returns>
        public static IList<ReportPostVO> ToDataTransferObjects(IList<PostVO> postList, int columnSize)
        {
            if (postList == null || columnSize < 1) return null;

            IList<ReportPostVO> result = new List<ReportPostVO>();

            ReportPostVO currentReportPostVO = new ReportPostVO();
            for (int i = 0; i < postList.Count; i++)
            {
                if (i == 0)
                {
                    currentReportPostVO = new ReportPostVO();
                    currentReportPostVO.Post1 = postList[i];
                }
                else
                {
                    //取餘數
                    int mod = i % columnSize;

                    if (mod == 0)
                    {
                        result.Add(currentReportPostVO);
                        currentReportPostVO = new ReportPostVO();
                        currentReportPostVO.Post1 = postList[i];
                    }
                    else
                    {
                        ObjectWrapper ow = new ObjectWrapper(currentReportPostVO);
                        ow.SetPropertyValue(string.Format("Post{0}", mod + 1), postList[i]);
                    }
                }
            }

            //將最後的加入
            if (currentReportPostVO != null)
            {
                result.Add(currentReportPostVO);
            }

            return result;
        }
		public void GetPropertyExceptions()
		{
			ObjectWrapper wrapper = new ObjectWrapper(new TestObject("Rick", 23));
			PropertyAccessExceptionsException ex
				= new PropertyAccessExceptionsException(wrapper, new PropertyAccessException[]
				{
					new TypeMismatchException(new PropertyChangeEventArgs("Doctor", new NestedTestObject("Foo"), "rubbish"), typeof (INestedTestObject)),
					new MethodInvocationException(new TargetInvocationException("Bad format", new FormatException("'12' is not a valid argument for 'foo'..")), new PropertyChangeEventArgs("Friends", new ArrayList(), "trash"))
				});
			PropertyAccessException doctorPropertyException = ex.GetPropertyAccessException("Doctor");
			Assert.IsNotNull(doctorPropertyException);
			Assert.AreEqual("typeMismatch", doctorPropertyException.ErrorCode);
			Assert.AreEqual("Foo", ((NestedTestObject) doctorPropertyException.PropertyChangeArgs.OldValue).Company);
			PropertyAccessException friendsPropertyException = ex.GetPropertyAccessException("Friends");
			Assert.IsNotNull(friendsPropertyException);
			Assert.AreEqual("methodInvocation", friendsPropertyException.ErrorCode);
			Assert.AreEqual(wrapper, ex.ObjectWrapper);
			Assert.AreEqual(wrapper.WrappedInstance, ex.BindObject);
			Assert.AreEqual(ex.Message, ex.ToString());

		}
Example #6
0
            /// <summary>
            /// Applies attributes to the proxy class.
            /// </summary>
            /// <param name="typeBuilder">The type builder to use.</param>
            /// <param name="targetType">The proxied class.</param>
            /// <see cref="IProxyTypeBuilder.ProxyTargetAttributes"/>
            /// <see cref="IProxyTypeBuilder.TypeAttributes"/>
            protected override void ApplyTypeAttributes(TypeBuilder typeBuilder, Type targetType)
            {
                foreach (object attr in GetTypeAttributes(targetType))
                {
                    if (attr is CustomAttributeBuilder)
                    {
                        typeBuilder.SetCustomAttribute((CustomAttributeBuilder)attr);
                    }
#if NET_2_0
                    else if (attr is CustomAttributeData)
                    {
                        typeBuilder.SetCustomAttribute(
                            ReflectionUtils.CreateCustomAttribute((CustomAttributeData)attr));
                    }
#endif
                    else if (attr is Attribute)
                    {
                        typeBuilder.SetCustomAttribute(
                            ReflectionUtils.CreateCustomAttribute((Attribute)attr));
                    }
                    else if (attr is IObjectDefinition)
                    {
                        RootObjectDefinition objectDefinition = (RootObjectDefinition) attr;

                        //TODO check that object definition is for an Attribute type.

                        //Change object definition so it can be instantiated and make prototype scope.
                        objectDefinition.IsAbstract = false;
                        objectDefinition.IsSingleton = false;
                        string objectName = ObjectDefinitionReaderUtils.GenerateObjectName(objectDefinition, objectFactory);
                        objectFactory.RegisterObjectDefinition(objectName, objectDefinition);
                        

                        //find constructor and constructor arg values to create this attribute.                       
                        ConstructorResolver constructorResolver = new ConstructorResolver(objectFactory, objectFactory,
                                                                               new SimpleInstantiationStrategy(),
                                                                               new ObjectDefinitionValueResolver(objectFactory));

                        
                        ConstructorInstantiationInfo ci = constructorResolver.GetConstructorInstantiationInfo(objectName,
                                                                                                              objectDefinition,
                                                                                                              null, null);

                        if (objectDefinition.PropertyValues.PropertyValues.Length == 0)
                        {
                            CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo,
                                                                                    ci.ArgInstances);
                            typeBuilder.SetCustomAttribute(cab);
                        }
                        else
                        {
                            object attributeInstance = objectFactory.GetObject(objectName);
                            IObjectWrapper wrappedAttributeInstance = new ObjectWrapper(attributeInstance);
                            PropertyInfo[] namedProperties = wrappedAttributeInstance.GetPropertyInfos();
                            object[] propertyValues = new object[namedProperties.Length];
                            for (int i = 0; i < namedProperties.Length; i++)
                            {
                                propertyValues[i] =
                                    wrappedAttributeInstance.GetPropertyValue(namedProperties[i].Name);
                            }
                            CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo, ci.ArgInstances, 
                                                                                    namedProperties, propertyValues);
                            typeBuilder.SetCustomAttribute(cab);
                        }


                    }
                    
                }
            }
        /// <summary>
        /// Convert QueryResults by generate decorate serializable class with transformed values.
        /// </summary>
        /// <param name="dynamicPageService"></param>
        /// <param name="results"></param>
        /// <returns></returns>
        private static IEnumerable ConvertQueryResults(IDynamicPage dynamicPageService, IEnumerable results)
        {
            IEnumerable<object> originalObjects = results.Cast<object>();
            if (originalObjects.Count() == 0) return results;

            IEnumerable<PropertyDefinition> propertyDecorateConfigs = CreatePropertyDecorateConfigs(dynamicPageService);
            IEnumerable<object> convertResults = null;

            try
            {
                convertResults = ClassDecorator.CreateDecorateObjects(originalObjects, propertyDecorateConfigs.ToArray()).Cast<object>();
            }
            catch (CompileException exp)
            {
                string loggingMessage = string.Format(CultureInfo.InvariantCulture, "There has errors to compile a dynamic class from dynamic page configuration to render UI grid of the dynamic page with object id equals to \"{0}\".", QueryStringUtility.ObjectId);
                Logger.Instance(typeof(DynamicPageDataServiceHandler)).Error(loggingMessage, exp);
                throw;
            }

            ObjectWrapper convertResultObjectWrapper = new ObjectWrapper(convertResults.First().GetType());
            for (int i = 0; i < originalObjects.Count(); i++)
            {
                object originalObject = originalObjects.ElementAt(i);
                GridRowControlBindEventArgs arg = new GridRowControlBindEventArgs(originalObject);
                dynamicPageService.OnGridRowControlsBind(arg);
                convertResultObjectWrapper.WrappedInstance = convertResults.ElementAt(i);

                // set visibility of checkbox
                bool showCheckBoxColumn = (bool)convertResultObjectWrapper.GetPropertyValue(DynamicPageDataServiceHandler.ShowCheckBoxColumnPropertyName);
                convertResultObjectWrapper.SetPropertyValue(DynamicPageDataServiceHandler.ShowCheckBoxColumnPropertyName, showCheckBoxColumn && arg.ShowCheckBoxColumn);

                // set visibility of edit button
                string permissionValue = string.Format(CultureInfo.InvariantCulture, "{0}.Update", dynamicPageService.Configuration.PermissionValue);
                bool hasUpdatePermission = permissionBridge.HasPermission(permissionValue);
                bool showEditButtonColumn = (bool)convertResultObjectWrapper.GetPropertyValue(DynamicPageDataServiceHandler.ShowEditButtonColumnPropertyName);
                convertResultObjectWrapper.SetPropertyValue(DynamicPageDataServiceHandler.ShowEditButtonColumnPropertyName, showEditButtonColumn && arg.ShowEditButton && hasUpdatePermission);

                // set visibility of view button
                bool showViewButtonColumn = (bool)convertResultObjectWrapper.GetPropertyValue(DynamicPageDataServiceHandler.ShowViewButtonColumnPropertyName);
                convertResultObjectWrapper.SetPropertyValue(DynamicPageDataServiceHandler.ShowViewButtonColumnPropertyName, showViewButtonColumn && arg.ShowViewButton);

                // set visibility of delete button
                permissionValue = string.Format(CultureInfo.InvariantCulture, "{0}.Delete", dynamicPageService.Configuration.PermissionValue);
                bool hasDeletePermission = permissionBridge.HasPermission(permissionValue);
                bool showDeleteButtonColumn = (bool)convertResultObjectWrapper.GetPropertyValue(DynamicPageDataServiceHandler.ShowDeleteButtonColumnPropertyName);
                convertResultObjectWrapper.SetPropertyValue(DynamicPageDataServiceHandler.ShowDeleteButtonColumnPropertyName, showDeleteButtonColumn && arg.ShowDeleteButton && hasDeletePermission);
            }

            return convertResults;
        }
        public void CreateDecorateObjectsWithoutConvertPropertyTypesTest()
        {
            var anonymousObjects = new[]
            {
                new { Name = "Eunge Liu", Career = "Software Development", Birthday = new DateTime(1982, 2, 7) },
                new { Name = "Lucy Liu", Career = "Financial", Birthday = new DateTime(1982, 5, 1) }
            };

            IEnumerable<PropertyDefinition> propertyDecorateConfigs = new PropertyDefinition[]
            {
                new PropertyDefinition(typeof(string)) { PropertyName = "Name", NewPropertyName = "Name" },
                new PropertyDefinition(typeof(string)) { PropertyName = "Career", NewPropertyName = "Career" },
                new PropertyDefinition(typeof(DateTime)) { PropertyName = "Birthday", NewPropertyName = "Birthday" },
            };

            IEnumerable<object> returnObjects = ClassDecorator.CreateDecorateObjects(anonymousObjects, propertyDecorateConfigs).Cast<object>();
            Assert.IsNotNull(returnObjects);
            Assert.AreEqual(2, returnObjects.Count());

            IObjectWrapper objectWrapper = new ObjectWrapper(returnObjects.First());
            Assert.AreEqual("Eunge Liu", objectWrapper.GetPropertyValue("Name"));
            Assert.AreEqual("Software Development", objectWrapper.GetPropertyValue("Career"));
            Assert.AreEqual(new DateTime(1982, 2, 7), objectWrapper.GetPropertyValue("Birthday"));

            objectWrapper = new ObjectWrapper(returnObjects.Last());
            Assert.AreEqual("Lucy Liu", objectWrapper.GetPropertyValue("Name"));
            Assert.AreEqual("Financial", objectWrapper.GetPropertyValue("Career"));
            Assert.AreEqual(new DateTime(1982, 5, 1), objectWrapper.GetPropertyValue("Birthday"));
        }
        public void CreateDecorateObjectsWithConvertPropertyTypesTest()
        {
            var anonymousObjects = new[]
            {
                new { Name = "Eunge Liu", Career = "Software Development", Birthday = new DateTime(1982, 2, 7), Salary = 13000 },
                new { Name = "Lucy Liu", Career = "Financial", Birthday = new DateTime(1982, 5, 1), Salary = 3000 }
            };

            IEnumerable results = ClassDecorator.CreateDecorateObjects(anonymousObjects, new[]
                {
                    new PropertyDefinition(typeof(string)) { PropertyName = "Name" },
                    new PropertyDefinition(typeof(string)) { PropertyName = "Career" },
                    new PropertyDefinition(typeof(string)) { PropertyName = "Birthday", PropertyValueConvertCallback = sender => { return ((DateTime)sender).ToString("yyyy-MM-dd") ;}},
                    new PropertyDefinition(typeof(string)) { PropertyName = "Salary" },
                });
            IEnumerable<object> returnObjects = results.Cast<object>();
            Assert.IsNotNull(returnObjects);
            Assert.AreEqual(2, returnObjects.Count());

            IObjectWrapper objectWrapper = new ObjectWrapper(returnObjects.First());
            Assert.AreEqual("Eunge Liu", objectWrapper.GetPropertyValue("Name"));
            Assert.AreEqual("Software Development", objectWrapper.GetPropertyValue("Career"));
            Assert.AreEqual("1982-02-07", objectWrapper.GetPropertyValue("Birthday"));
            Assert.AreEqual("13000", objectWrapper.GetPropertyValue("Salary"));

            objectWrapper = new ObjectWrapper(returnObjects.Last());
            Assert.AreEqual("Lucy Liu", objectWrapper.GetPropertyValue("Name"));
            Assert.AreEqual("Financial", objectWrapper.GetPropertyValue("Career"));
            Assert.AreEqual("1982-05-01", objectWrapper.GetPropertyValue("Birthday"));
            Assert.AreEqual("3000", objectWrapper.GetPropertyValue("Salary"));
        }
        public void CreateDecorateObjectsForTypeWithDictionaryPropertyTest()
        {
            CodeDomTestObject obj1 = new CodeDomTestObject { Name = "Eunge" };
            obj1.Properties["AnnualPackage"] = 210000;
            obj1.Properties["Birthday"] = new DateTime(1982, 2, 7);
            obj1.Properties["Position"] = "Engineer";

            IEnumerable results = ClassDecorator.CreateDecorateObjects(new[] { obj1 }, new[]
                {
                    new PropertyDefinition(typeof(string)) { PropertyName = "Name" },
                    new PropertyDefinition(typeof(int))
                    {
                        PropertyName = @"Properties[""AnnualPackage""]",
                        NewPropertyName = FieldNameTransformUtility.DataBoundFieldName(@"Properties[""AnnualPackage""]", 1),
                    },
                    new PropertyDefinition(typeof(DateTime))
                    {
                        PropertyName = @"Properties[""Birthday""]",
                        NewPropertyName = FieldNameTransformUtility.DataBoundFieldName(@"Properties[""Birthday""]", 2),
                    },
                    new PropertyDefinition(typeof(string))
                    {
                        PropertyName = @"Properties[""Position""]",
                        NewPropertyName = FieldNameTransformUtility.DataBoundFieldName(@"Properties[""Position""]", 3),
                    }
                });

            IEnumerable<object> returnObjects = results.Cast<object>();
            Assert.IsNotNull(returnObjects);
            Assert.AreEqual(1, returnObjects.Count());

            IObjectWrapper objectWrapper = new ObjectWrapper(returnObjects.First());
            Assert.AreEqual("Eunge", objectWrapper.GetPropertyValue("Name"));

            string positionPropertyName = FieldNameTransformUtility.DataBoundFieldName(@"Properties[""Position""]", 3);
            Assert.AreEqual("Engineer", objectWrapper.GetPropertyValue(positionPropertyName));
        }