Commits

Anonymous committed 879abcf

added a sample for dynamic behavior

  • Participants
  • Parent commits 86fd17f

Comments (0)

Files changed (3)

File Dynamic/CSharpDynamic/DynamicFeaturesSamples.Tests/Dynamic_customer_tests.cs

-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using Should;
 using SubSpec;
 
         dynamic _customer;
         Dictionary<string, object> _additionalData = new Dictionary<string, object>();
 
+        Dictionary<string, Func<dynamic, dynamic>> _additionalBehavior =
+            new Dictionary<string, Func<dynamic, dynamic>>();
+
         [Specification]
         public void dynamic_properties()
         {
                 .Assert(setters_are_exposed);
         }
 
+        [Specification]
+        public void dynamic_behavior()
+        {
+            "Given additional dynamic behavior for a customer".Context(() => make_behavior_for_a_customer());
+            "When constructing a customer object".Do(() => _customer = GetCustomer());
+            "Then the object should expose the additional behavior".Assert(behaviors_are_exposed);
+            "And the behavior should be accessible".Assert(behaviors_are_accessible);
+        }
+
         void make_data_for_a_customer()
         {
             _additionalData.Clear();
-            _additionalData.Add("Comments", "Some comments...");    
+            _additionalData.Add("Comments", "Some comments...");
             _additionalData.Add("NumberOfDependents", 3);
         }
 
+        void make_behavior_for_a_customer()
+        {
+            _additionalBehavior.Clear();
+            _additionalBehavior.Add("FullName", c => string.Format("{0} {1}", c.FirstName, c.LastName));
+        }
+
         Customer GetCustomer()
         {
             //return new Customer(_additionalData);
 
-            // alternatively...
-            return new Customer(new Dictionary<string, object>
-                                    {
-                                        { "Comments", "Some comments..." }, 
-                                        { "NumberOfDependents", 3 }
-                                    });
+            ////alternatively...
+            //return new Customer(new Dictionary<string, object>
+            //                        {
+            //                            { "Comments", "Some comments..." }, 
+            //                            { "NumberOfDependents", 3 }
+            //                        });
+
+            // adding behavior...
+            var customer = new Customer(_additionalBehavior)
+                               {
+                                   FirstName = "Jon", LastName = "Jones"
+                               };
+            return customer;
         }
 
         void properties_are_exposed()
             (_customer as DynamicEntity).RespondTo("Comments").ShouldBeTrue();
             (_customer as DynamicEntity).RespondTo("NumberOfDependents").ShouldBeTrue();
         }
-
+        
         void getters_are_exposed()
         {
             ((string) _customer.Comments).ShouldEqual("Some comments...");
             _customer.Comments = "New comments";
             _customer.NumberOfDependents = 9;
 
-            ((string)_customer.Comments).ShouldEqual("New comments");
-            ((int)_customer.NumberOfDependents).ShouldEqual(9);
+            ((string) _customer.Comments).ShouldEqual("New comments");
+            ((int) _customer.NumberOfDependents).ShouldEqual(9);
+        }
+
+        void behaviors_are_exposed()
+        {
+            (_customer as DynamicEntity).RespondTo("FullName").ShouldBeTrue();
+        }
+
+        void behaviors_are_accessible()
+        {
+            ((string)_customer.FullName).ShouldEqual("Jon Jones");
         }
     }
 }

File Dynamic/CSharpDynamic/DynamicFeaturesSamples/Customer.cs

 using System.Collections.Generic;
+using System;
 
 namespace DynamicFeaturesSamples
 {
         {
         }
 
+        public Customer(Dictionary<string, Func<dynamic, dynamic>> additionalBehavior) : base(additionalBehavior)
+        {
+            
+        }
+
         public string FirstName { get; set; }
         public string LastName { get; set; }
     }

File Dynamic/CSharpDynamic/DynamicFeaturesSamples/DynamicEntity.cs

 using System.Collections.Generic;
 using System.Dynamic;
+using System;
 
 namespace DynamicFeaturesSamples
 {
     public class DynamicEntity : DynamicObject
     {
+        readonly Dictionary<string, Func<dynamic, dynamic>> _additionalBehavior;
         Dictionary<string, object> _additionalData = new Dictionary<string, object>();
 
         public DynamicEntity(IEnumerable<KeyValuePair<string, object>> additionalData)
             }
         }
 
+        public DynamicEntity(Dictionary<string, Func<dynamic, dynamic>> additionalBehavior)
+        {
+            _additionalBehavior = additionalBehavior;
+        }
+
         public override bool TryGetMember(GetMemberBinder binder, out object result)
         {
             if (_additionalData.ContainsKey(binder.Name))
                 result = _additionalData[binder.Name];
                 return true;
             }
+            if (_additionalBehavior.ContainsKey(binder.Name))
+            {
+                result = _additionalBehavior[binder.Name].Invoke(this);
+                return true;
+            }
             return base.TryGetMember(binder, out result);
         }
 
 
         public bool RespondTo(string message)
         {
-            return _additionalData.ContainsKey(message);
+            return _additionalData.ContainsKey(message) || _additionalBehavior.ContainsKey(message);
         }
     }
 }