Exemplo n.º 1
0
        public override object GetMember(Func <object> proceed, object self, string name)
        {
            if (name == "Cells")
            {
                // provide a robot for zone manipulation on parent object
                return(ClayActivator.CreateInstance(new IClayBehavior[] {
                    new InterfaceProxyBehavior(),
                    new CellsBehavior(_cellFactory, self)
                }));
            }

            // Check for other result
            var result = proceed();

            // No result exists so create a zone
            if (((dynamic)result) == null)
            {
                // substitute nil results with a robot that turns adds a zone on
                // the parent when .Add is invoked
                return(ClayActivator.CreateInstance(new IClayBehavior[] {
                    new InterfaceProxyBehavior(),
                    new NilBehavior(),
                    new CellOnDemandBehavior(_cellFactory, self, name)
                }));
            }
            return(result);
        }
Exemplo n.º 2
0
        public void ClaySubclassIsActivatedWithoutDynamicProxy()
        {
            var alpha = ClayActivator.CreateInstance <ClayPlus>(Enumerable.Empty <IClayBehavior>());
            var type  = alpha.GetType();

            Assert.That(type, Is.EqualTo(typeof(ClayPlus)));
        }
        public override object GetMember(Func <object> proceed, object self, string name)
        {
            if (name == "Zones")
            {
                // provide a robot for zone manipulation on parent object
                return(ClayActivator.CreateInstance(new IClayBehavior[] {
                    new InterfaceProxyBehavior(),
                    new ZonesBehavior(_zoneFactory, self, _layoutShape)
                }));
            }

            var result = proceed();

            if (((dynamic)result) == null)
            {
                // substitute nil results with a robot that turns adds a zone on
                // the parent when .Add is invoked
                return(ClayActivator.CreateInstance(new IClayBehavior[] {
                    new InterfaceProxyBehavior(),
                    new NilBehavior(),
                    new ZoneOnDemandBehavior(_zoneFactory, self, name)
                }));
            }
            return(result);
        }
Exemplo n.º 4
0
        public void SimpleActivationUsesDefaultClass()
        {
            var alpha = ClayActivator.CreateInstance(Enumerable.Empty <IClayBehavior>());
            var type  = alpha.GetType();

            Assert.That(type, Is.EqualTo(typeof(Clay)));
        }
Exemplo n.º 5
0
 public dynamic CreateHelper(ViewContext viewContext, IViewDataContainer viewDataContainer)
 {
     return(ClayActivator.CreateInstance <DisplayHelper>(
                _behaviors,
                _displayManager,
                _shapeFactory,
                viewContext,
                viewDataContainer));
 }
Exemplo n.º 6
0
        public void ClaySubclassFromAnythingIsActivatedDynamixProxyAddingDlrInterfaces()
        {
            var alpha = ClayActivator.CreateInstance <Anything>(Enumerable.Empty <IClayBehavior>());

            var type = alpha.GetType();

            Assert.That(type, Is.Not.EqualTo(typeof(Anything)));
            Assert.That(typeof(Anything).IsAssignableFrom(type));
        }
Exemplo n.º 7
0
        public void TestGetPaths()
        {
            var  dynamically = ClayActivator.CreateInstance <Beta>(new[] { new BetaBehavior() });
            Beta statically  = dynamically;

            Assert.That(dynamically.Hello, Is.EqualTo("World-"));
            Assert.That(statically.Hello, Is.EqualTo("World-"));

            Assert.That(dynamically.Foo, Is.EqualTo("Bar-"));

            Assert.Throws <RuntimeBinderException>(() => { var x = dynamically.MissingPropNotHandled; });
        }
Exemplo n.º 8
0
        public override object GetMember(Func <object> proceed, object self, string name)
        {
            if (name == "Zones")
            {
                return(ClayActivator.CreateInstance(new IClayBehavior[] {
                    new InterfaceProxyBehavior(),
                    new ZonesProxyBehavior(() => proceed(), Proxies, self)
                }));
            }

            // Otherwise proceed to other behaviours, including the original ZoneHoldingBehavior
            return(proceed());
        }
Exemplo n.º 9
0
            public override object GetMember(Func <object> proceed, object self, string name)
            {
                var parentMember = ((dynamic)_parent)[name];

                if (parentMember == null)
                {
                    return(ClayActivator.CreateInstance(new IClayBehavior[] {
                        new InterfaceProxyBehavior(),
                        new NilBehavior(),
                        new CellOnDemandBehavior(_cellFactory, _parent, name)
                    }));
                }
                return(parentMember);
            }
Exemplo n.º 10
0
        public override object GetMember(Func <object> proceed, object self, string name)
        {
            // Already have a member?
            var existing = proceed();

            // Otherwise return a temp clay that will create a stuff when a property is set (or return nil if it's a nested get)
            if ((dynamic)existing == null)
            {
                return(ClayActivator.CreateInstance(new IClayBehavior[] {
                    new NilBehavior(),   // Causes to object to be effectively null until a member is activated
                    new InterfaceProxyBehavior(),
                    new StuffBehavior(), // Means that child members will also return StuffOnDemand
                    new StuffOnDemandBehavior(self, name)
                }));
            }
            return(existing);
        }
Exemplo n.º 11
0
        public void TestInvokePaths()
        {
            var dynamically = ClayActivator.CreateInstance <Alpha>(new IClayBehavior[] {
                new InterfaceProxyBehavior(),
                new AlphaBehavior()
            });
            Alpha  statically    = dynamically;
            IAlpha interfacially = dynamically;

            Assert.That(dynamically.Hello(), Is.EqualTo("World-"));
            Assert.That(statically.Hello(), Is.EqualTo("World-"));
            Assert.That(interfacially.Hello(), Is.EqualTo("World-"));

            Assert.That(dynamically.Foo(), Is.EqualTo("Bar-"));
            Assert.That(interfacially.Foo(), Is.EqualTo("Bar-"));

            Assert.Throws <RuntimeBinderException>(() => dynamically.MissingNotHandled());
        }
Exemplo n.º 12
0
        public void SubclassMembersRemainAvailableStaticallyAndDynamicallyAndViaInterface()
        {
            var alpha = ClayActivator.CreateInstance <ClayPlus>(new[] { new InterfaceProxyBehavior() });

            dynamic   dynamically   = alpha;
            ClayPlus  statically    = alpha;
            IClayPlus interfacially = alpha;

            Assert.That(dynamically.Hello, Is.EqualTo("World"));
            Assert.That(statically.Hello, Is.EqualTo("World"));
            Assert.That(interfacially.Hello, Is.EqualTo("World"));

            Assert.That(dynamically.Add(3, 4), Is.EqualTo(7));
            Assert.That(statically.Add(3, 4), Is.EqualTo(7));
            Assert.That(interfacially.Add(3, 4), Is.EqualTo(7));
            Assert.That(interfacially.Add(3, 5), Is.EqualTo(8));
            Assert.That(interfacially.Add(3, 6), Is.EqualTo(9));
        }
Exemplo n.º 13
0
        public void BehaviorsCanFilterVirtualMethods()
        {
            var alpha = ClayActivator.CreateInstance <Anything>(new IClayBehavior[] {
                new InterfaceProxyBehavior(),
                new AnythingModifier()
            });

            dynamic   dynamically   = alpha;
            Anything  statically    = alpha;
            IClayPlus interfacially = alpha;

            Assert.That(dynamically.Hello, Is.EqualTo("[World]"));
            Assert.That(statically.Hello, Is.EqualTo("[World]"));
            Assert.That(interfacially.Hello, Is.EqualTo("[World]"));

            Assert.That(dynamically.Add(3, 4), Is.EqualTo(9));
            Assert.That(statically.Add(3, 4), Is.EqualTo(9));
            Assert.That(interfacially.Add(3, 4), Is.EqualTo(9));
            Assert.That(interfacially.Add(3, 5), Is.EqualTo(10));
            Assert.That(interfacially.Add(3, 6), Is.EqualTo(11));
        }
Exemplo n.º 14
0
        public void SubclassFromAnythingMembersRemainAvailableStaticallyAndDynamicallyAndViaInterface()
        {
            var alpha = ClayActivator.CreateInstance <Anything>(new[] { new InterfaceProxyBehavior() });
            var type  = alpha.GetType();

            Assert.That(type, Is.Not.EqualTo(typeof(Anything)));

            dynamic   dynamically   = alpha;
            Anything  statically    = alpha;
            IClayPlus interfacially = alpha;

            Assert.That(dynamically.Hello, Is.EqualTo("World"));
            Assert.That(statically.Hello, Is.EqualTo("World"));
            Assert.That(interfacially.Hello, Is.EqualTo("World"));

            Assert.That(dynamically.Add(3, 4), Is.EqualTo(7));
            Assert.That(statically.Add(3, 4), Is.EqualTo(7));
            Assert.That(interfacially.Add(3, 4), Is.EqualTo(7));
            Assert.That(interfacially.Add(3, 5), Is.EqualTo(8));
            Assert.That(interfacially.Add(3, 6), Is.EqualTo(9));
        }
Exemplo n.º 15
0
        public IShape Create(string shapeType, INamedEnumerable <object> parameters, IEnumerable <IClayBehavior> behaviors)
        {
            var             defaultShapeTable = _shapeTableLocator.Value.Lookup(null);
            ShapeDescriptor shapeDescriptor;

            defaultShapeTable.Descriptors.TryGetValue(shapeType, out shapeDescriptor);

            var creatingContext = new ShapeCreatingContext {
                New          = this,
                ShapeFactory = this,
                ShapeType    = shapeType,
                OnCreated    = new List <Action <ShapeCreatedContext> >()
            };
            var positional = parameters.Positional;

            creatingContext.BaseType = positional.Take(1).OfType <Type>().SingleOrDefault();
            if (creatingContext.BaseType == null)
            {
                // default to common base class
                creatingContext.BaseType = typeof(Shape);
            }
            else
            {
                // consume the first argument
                positional = positional.Skip(1);
            }

            if (creatingContext.BaseType == typeof(Array))
            {
                // array is a hint - not an intended base class
                creatingContext.BaseType  = typeof(Shape);
                creatingContext.Behaviors = new List <IClayBehavior> {
                    new ClaySharp.Behaviors.InterfaceProxyBehavior(),
                    new ClaySharp.Behaviors.PropBehavior(),
                    new ClaySharp.Behaviors.ArrayBehavior(),
                    new ClaySharp.Behaviors.NilResultBehavior(),
                };
            }
            else
            {
                creatingContext.Behaviors = new List <IClayBehavior> {
                    new ClaySharp.Behaviors.InterfaceProxyBehavior(),
                    new ClaySharp.Behaviors.PropBehavior(),
                    new ClaySharp.Behaviors.NilResultBehavior(),
                    new Shape.ShapeBehavior(),
                };
            }

            if (behaviors != null && behaviors.Any())
            {
                // include behaviors passed in by caller, if any
                creatingContext.Behaviors = creatingContext.Behaviors.Concat(behaviors).ToList();
            }

            // "creating" events may add behaviors and alter base type)
            foreach (var ev in _events)
            {
                ev.Value.Creating(creatingContext);
            }
            if (shapeDescriptor != null)
            {
                foreach (var ev in shapeDescriptor.Creating)
                {
                    ev(creatingContext);
                }
            }

            // create the new instance
            var createdContext = new ShapeCreatedContext {
                New       = creatingContext.New,
                ShapeType = creatingContext.ShapeType,
                Shape     = ClayActivator.CreateInstance(creatingContext.BaseType, creatingContext.Behaviors)
            };
            var shapeMetadata = new ShapeMetadata {
                Type = shapeType
            };

            createdContext.Shape.Metadata = shapeMetadata;

            if (shapeDescriptor != null)
            {
                shapeMetadata.Wrappers = shapeMetadata.Wrappers.Concat(shapeDescriptor.Wrappers).ToList();
            }

            // "created" events provides default values and new object initialization
            foreach (var ev in _events)
            {
                ev.Value.Created(createdContext);
            }
            if (shapeDescriptor != null)
            {
                foreach (var ev in shapeDescriptor.Created)
                {
                    ev(createdContext);
                }
            }
            foreach (var ev in creatingContext.OnCreated)
            {
                ev(createdContext);
            }


            // other properties passed with call overlay any defaults, so are after the created events

            // only one non-Type, non-named argument is allowed
            var initializer = positional.SingleOrDefault();

            if (initializer != null)
            {
                foreach (var prop in initializer.GetType().GetProperties())
                {
                    createdContext.Shape[prop.Name] = prop.GetValue(initializer, null);
                }
            }

            foreach (var kv in parameters.Named)
            {
                createdContext.Shape[kv.Key] = kv.Value;
            }

            return(createdContext.Shape);
        }