Beispiel #1
0
 public DelegatePropertyDescriptor(string name, IMicroModel model, Type propertyType) : base(name, null)
 {
     _name         = name;
     _model        = model;
     _ownerType    = model.GetType();
     _propertyType = propertyType;
 }
Beispiel #2
0
        private static IPropertyDefinition BuildProperty(IMicroModel model, string propertyName, Type propertyType)
        {
            var property = new DelegatePropertyDescriptor(propertyName, model, propertyType);

            model.AddProperty(property);
            return(property);
        }
Beispiel #3
0
        public static IPropertyDefinition Command <TCommandParameter>(this IMicroModel model, string commandPropertyName, Action <TCommandParameter> executedCallback, Func <TCommandParameter, bool> canExecuteCallback)
        {
            var command  = new DelegateCommand(x => executedCallback((TCommandParameter)x), x => canExecuteCallback((TCommandParameter)x));
            var property = BuildProperty(model, commandPropertyName, typeof(ICommand));

            property.Getter = x => command;
            return(property);
        }
Beispiel #4
0
        public static IPropertyDefinition Command(this IMicroModel model, string commandPropertyName, Action executedCallback)
        {
            var command  = new DelegateCommand(x => executedCallback());
            var property = BuildProperty(model, commandPropertyName, typeof(ICommand));

            property.Getter = x => command;
            return(property);
        }
Beispiel #5
0
        public static IPropertyDefinition Property <TProperty>(this IMicroModel model, string propertyName, Expression <Func <TProperty> > getPropertyCallback)
        {
            getPropertyCallback = Evaluator.EvaluateClosures(getPropertyCallback);
            var compiledGetter = getPropertyCallback.Compile();
            var property       = BuildProperty(model, propertyName, typeof(TProperty));

            property.Getter = (x) => compiledGetter();
            AddDependencies(property, getPropertyCallback);
            return(property);
        }
Beispiel #6
0
        public static IPropertyDefinition Property(this IMicroModel model, string propertyName, Type propertyType, Expression <Func <object> > getPropertyCallback, Func <object, object> setPropertyCallback)
        {
            getPropertyCallback = Evaluator.EvaluateClosures(getPropertyCallback);
            var compiledGetter = getPropertyCallback.Compile();
            var property       = BuildProperty(model, propertyName, propertyType);

            property.Getter = (x) => compiledGetter();
            property.Setter = (x, y) => setPropertyCallback(y);
            AddDependencies(property, getPropertyCallback);
            return(property);
        }
Beispiel #7
0
        public static ICollectionDefinition <TElement> Collection <TElement>(this IMicroModel model, string propertyName, Expression <Func <IEnumerable <TElement> > > items)
        {
            items = Evaluator.EvaluateClosures(items);
            var compiledGetter       = items.Compile();
            var property             = BuildProperty(model, propertyName, typeof(ObservableCollection <object>));
            var collectionDefinition = new CollectionDefinition <TElement>(property, compiledGetter);

            property.Getter = x => collectionDefinition.Collection;
            AddDependencies(property, items);
            return(collectionDefinition);
        }
        /// <summary>
        /// Applies this extension to the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        public void Apply(IMicroModel model)
        {
            var properties = model.GetProperties().ToList();

            if (properties.Count != properties.Select(x => x.Name).Distinct().Count())
            {
                Fail("One or more properties on a dynamic object have duplicate names. The properties are: {0}{1}",
                     Environment.NewLine,
                     string.Join(Environment.NewLine, properties.Select(x => " - " + x.Name).ToArray())
                     );
            }
        }
Beispiel #9
0
        public static IEnumerable <IPropertyDefinition> AllProperties(this IMicroModel model, object source)
        {
            var properties      = TypeDescriptor.GetProperties(source).OfType <PropertyDescriptor>();
            var propertiesToAdd = new List <IPropertyDefinition>();

            foreach (var sourceProperty in properties)
            {
                var property      = sourceProperty;
                var propertyToAdd = new DelegatePropertyDescriptor(property.Name, model, property.PropertyType);
                propertyToAdd.Getter = x => property.GetValue(source);
                if (!property.IsReadOnly)
                {
                    propertyToAdd.Setter = (x, v) => property.SetValue(source, v);
                }

                model.AddProperty(propertyToAdd);
                propertiesToAdd.Add(propertyToAdd);
            }
            return(propertiesToAdd);
        }
Beispiel #10
0
        public static IPropertyDefinition Property <TProperty>(this IMicroModel model, Expression <Func <TProperty> > propertyGetter)
        {
            propertyGetter = Evaluator.EvaluateClosures(propertyGetter);
            var member        = propertyGetter.GetOutermostMember();
            var propertyInfo  = (PropertyInfo)member.Member;
            var property      = BuildProperty(model, propertyInfo.Name, typeof(TProperty));
            var readEvaluator = new SafeExpressionEvaluator(propertyGetter.Body, new Dictionary <string, object>());

            property.Getter = (x) => readEvaluator.Evaluate();
            AddDependencies(property, propertyGetter);
            if (propertyInfo.CanWrite)
            {
                var writeEvaluator = new SafeExpressionEvaluator(member.Expression, new Dictionary <string, object>());
                property.Setter = (x, y) =>
                {
                    var target = writeEvaluator.Evaluate();
                    propertyInfo.SetValue(target, y, null);
                };
            }
            return(property);
        }
Beispiel #11
0
 public void SetUp()
 {
     Model = new SampleModel();
 }
Beispiel #12
0
 public void Apply(IMicroModel model)
 {
     model.AddProperty(new Mock <IPropertyDefinition>().Object);
 }
Beispiel #13
0
 public void SetUp()
 {
     Model = new MicroModel();
 }
 public void SetUp()
 {
     Model = new SampleModel();
     Model.AddExtension(new TypeValidationExtensionAttribute());
 }