Exemplo n.º 1
0
        /**
         * Entity Property Setter.
         *
         * All _"mapped"_ properties need to implement this as their Setter.
         *
         * ```cs
         *  class Foo : Model<Foo>
         *  {
         *      public string Bar { get... set { Set(value); } }
         *  }
         * ```
         *
         * > TODO: Investigate IL Weaving... or possibly just a super simple
         * > pre compilation script (grunt/gulp task) to automatically add
         * > the needed method calls.
         */
        public virtual void Set <T>(T value, [CallerMemberName] string propName = "", bool triggerChangeEvent = true)
        {
            // Grab the property
            var prop = MappedProps.Single(p => p.Name == propName);

            // Create the property bag dict if it doesn't exist yet.
            if (this.PropertyBag == null)
            {
                this.PropertyBag = new Dictionary <string, object>();
            }

            // If the value is an entity or list of entities
            // we will save it to our discovered list.
            if (value != null && !TypeMapper.IsClrType(value))
            {
                //   this.SaveDiscoveredEntities(prop, value);
            }

            // If the property does not already have
            // a value, set it's original value.
            if (this.Hydrated && this.Get <object>(propName, loadFromDiscovered: false, loadFromDb: false) == null)
            {
                if (value != null && TypeMapper.IsListOfEntities(value))
                {
                    var clone = (value as IEnumerable <object>)
                                .Cast <IModel <TModel> >().ToList();

                    this.OriginalPropertyBag[propName] = clone;
                }
                else
                {
                    this.OriginalPropertyBag[propName] = value;
                }
            }

            // Wrap any normal Lists in a BindingList so that we can track when
            // new entities are added so that we may save those entities to our
            // discovered list.
            dynamic propertyBagValue;

            if (value != null && TypeMapper.IsList(value))
            {
                dynamic bindingList = Activator.CreateInstance
                                      (
                    typeof(BindingList <>).MakeGenericType
                    (
                        value.GetType().GenericTypeArguments[0]
                    ),
                    new object[] { value }
                                      );

                bindingList.ListChanged += new ListChangedEventHandler
                                           (
                    (sender, e) =>
                {
                    //if (!triggerChangeEvent) return;

                    switch (e.ListChangedType)
                    {
                    case ListChangedType.ItemAdded:
                    case ListChangedType.ItemDeleted:
                        {
                            this.FirePropertyChanged(prop);
                        }
                        break;
                    }
                }
                                           );

                propertyBagValue = bindingList;
            }
            else
            {
                propertyBagValue = value;
            }

            // Save the new value
            this.PropertyBag[propName] = propertyBagValue;

            // Trigger the change event
            if (triggerChangeEvent)
            {
                this.FirePropertyChanged(prop);
            }
        }
Exemplo n.º 2
0
 public String GetMapped(String key)
 {
     return(MappedProps.Get(key));
 }