static internal InspectableAdapter ToAdapter(object o) { if (o == null) { return(null); } InspectableAdapter inspectableAdapter = o as InspectableAdapter; if (inspectableAdapter != null) { return(inspectableAdapter); } IFactoryObject factoryObject = o as IFactoryObject; // If we find an 'IFactoryObject' we will need to return the underlying Interface if (factoryObject != null) { return(factoryObject.Adapter); } if (o.GetType() == typeof(object[])) { object[] objs = (object[])o; IntPtr[] objectAdapters = new IntPtr[objs.Length]; for (int index = 0; index < objs.Length; ++index) { inspectableAdapter = ToAdapter(objs[index]); objectAdapters[index] = inspectableAdapter != null ? inspectableAdapter.Interface : IntPtr.Zero; } return(new PropertyValueAdapter(objectAdapters)); } // we assume can be constructed by a PropertyValue return(PropertyValueAdapter.CreateValue(o)); }
private void DoGetObjectsOfType(Type type, bool includeFactoryObjects, bool includePrototypes, IDictionary collector) { bool isFactoryType = (type != null && typeof(IFactoryObject).IsAssignableFrom(type)); foreach (string name in objects.Keys) { object instance = objects[name]; if (instance is IFactoryObject && includeFactoryObjects) { IFactoryObject factory = (IFactoryObject)instance; Type objectType = factory.ObjectType; if ((objectType == null && factory.IsSingleton) || ((factory.IsSingleton || includePrototypes) && objectType != null && type.IsAssignableFrom(objectType))) { object createdObject = GetObject(name); if (type.IsInstanceOfType(createdObject)) { collector[name] = createdObject; } } } else if (type.IsAssignableFrom(instance.GetType())) { if (isFactoryType) { collector[ObjectFactoryUtils.BuildFactoryObjectName(name)] = instance; } else { collector[name] = instance; } } } }
public void CanExportFromFactoryObjectIfObjectTypeIsInterface() { using (DefaultListableObjectFactory of = new DefaultListableObjectFactory()) { MockRepository mocks = new MockRepository(); IFactoryObject simpleCounterFactory = (IFactoryObject)mocks.DynamicMock(typeof(IFactoryObject)); Expect.Call(simpleCounterFactory.ObjectType).Return(typeof(ISimpleCounter)); Expect.Call(simpleCounterFactory.IsSingleton).Return(true); Expect.Call(simpleCounterFactory.GetObject()).Return(new SimpleCounter()); mocks.ReplayAll(); of.RegisterSingleton("simpleCounter", simpleCounterFactory); SaoExporter saoExporter = new SaoExporter(); saoExporter.ObjectFactory = of; saoExporter.TargetName = "simpleCounter"; saoExporter.ServiceName = "RemotedSaoCallCounter"; saoExporter.AfterPropertiesSet(); of.RegisterSingleton("simpleCounterExporter", saoExporter); // also tests SaoExporter.Dispose()! AssertExportedService(saoExporter.ServiceName, 2); mocks.VerifyAll(); } }
internal PropertyChangedEventArgs( PropertyChangedEventArgsAdapter eventArgsAdapter, IFactoryObject factoryObject) : base(eventArgsAdapter.PropertyName) { this.EventArgsAdapter = eventArgsAdapter; this.FactoryObject = factoryObject; }
public void GetObjectTypeWithNoTargetOrTargetSource() { IObjectFactory bf = new XmlObjectFactory(new ReadOnlyXmlTestResource("proxyFactoryTargetSourceTests.xml", GetType())); bf.GetObject("noTarget"); IFactoryObject pfb = (ProxyFactoryObject)bf.GetObject("&noTarget"); Assert.IsTrue(typeof(ITestObject).IsAssignableFrom(pfb.ObjectType), "Has correct object type"); }
public void CanExportFromFactoryObjectIfObjectTypeIsInterface() { using (DefaultListableObjectFactory of = new DefaultListableObjectFactory()) { IFactoryObject simpleCounterFactory = A.Fake <IFactoryObject>(); A.CallTo(() => simpleCounterFactory.ObjectType).Returns(typeof(ISimpleCounter)); A.CallTo(() => simpleCounterFactory.IsSingleton).Returns(true); A.CallTo(() => simpleCounterFactory.GetObject()).Returns(new SimpleCounter()); of.RegisterSingleton("simpleCounter", simpleCounterFactory); SaoExporter saoExporter = new SaoExporter(); saoExporter.ObjectFactory = of; saoExporter.TargetName = "simpleCounter"; saoExporter.ServiceName = "RemotedSaoCallCounter"; saoExporter.AfterPropertiesSet(); of.RegisterSingleton("simpleCounterExporter", saoExporter); // also tests SaoExporter.Dispose()! AssertExportedService(saoExporter.ServiceName, 2); } }
public void Add(string key, IFactoryObject v) { if (v == null || c_ == null) { return; } var o = v.ToJSON().AsObject(); if (o == null) { Synergy.LogError( "Factory object ToJson() did not return an object"); return; } o.Add("factoryTypeName", v.GetFactoryTypeName()); c_.Add(key, o.Impl); }
static void Main(string[] args) { SystemConsole.WriteLine("Hello World!"); foreach (IFactory factory in Factories) { IFactoryObject factoryObject = factory.MakeObject(); factoryObject.PrintSelf(); } IBuiltObject builtObject1 = new Builder(_logger) .Start() .Annoying() .Loud() .WithColor(ConsoleColor.Green) .Build(); IBuiltObject builtObject2 = new Builder(_logger) .Start() .Quiet() .WithColor(ConsoleColor.Yellow) .Build(); builtObject1.PrintSelf(); builtObject2.PrintSelf(); foreach (IFactoryMethod factoryMethod in FactoryMethods) { IFactoryObject factoryObject = factoryMethod.CreateObject(); factoryObject.PrintSelf(); } IPrototype prototype = Prototype.InitialPrototype.Clone(); for (int i = 0; i < 10; ++i) { SystemConsole.WriteLine($"prototype #{prototype.Id} copied"); prototype = prototype.Clone(); } Singleton singleton = Singleton.Instance; SystemConsole.WriteLine($"Singleton created at {singleton.CreationDateTime}"); DivisionResult <int> divisionResult = _multiplyDivideAdapter.Divide(10, 3); SystemConsole.WriteLine($"{nameof(MultiplyDivideAdapter)}: 10 / 3 = {divisionResult.WholePart} with remainder {divisionResult.Remainder}"); int multiplicationResult = _multiplyDivideAdapter.Multiply(10, 3); SystemConsole.WriteLine($"{nameof(MultiplyDivideAdapter)}: 10 x 3 = {multiplicationResult}"); _logger.Log("Turn on hot"); _hotWaterPipeBridge.TurnOn(); _logger.Log("Turn off hot"); _hotWaterPipeBridge.TurnOff(); _logger.Log("Sprinkling hot"); _hotWaterPipeBridge.Sprinkle(); _logger.Log("Turn on cold"); _coldWaterPipeBridge.TurnOn(); _logger.Log("Turn off cold"); _coldWaterPipeBridge.TurnOff(); _logger.Log("Sprinkling cold"); _coldWaterPipeBridge.Sprinkle(); _TraverseCompositeTree(_composite); _logger.Log($"It seems we have a gift! First let's unwrap the {_wrapper.GetType().Name}"); GiftBox giftBox = (_wrapper as WrappingPaper).Unwrap(); _logger.Log($"We have the {giftBox.GetType().Name} open, now let's take our gift out."); Gift gift = giftBox.Open(); _logger.Log($"The gift is a {gift.Name}!"); _logger.Log($"Random divided numbers from facade: {_facade.GetRandomNumbersAndDivideThem()}"); _logger.Log($"Random multiplied numbers from facade: {_facade.GetRandomNumbersAndMultiplyThem()}"); SystemConsole.ReadLine(); }
/// <summary> /// Obtain an object to expose from the given IFactoryObject. /// </summary> /// <param name="factory">The IFactoryObject instance.</param> /// <param name="objectName">Name of the object.</param> /// <param name="rod">The merged object definition.</param> /// <returns>The object obtained from the IFactoryObject</returns> /// <exception cref="ObjectCreationException">If IFactoryObject object creation failed.</exception> private object GetObjectFromFactoryObject(IFactoryObject factory, string objectName, RootObjectDefinition rod) { object instance; try { instance = factory.GetObject(); } catch (FactoryObjectNotInitializedException ex) { throw new ObjectCurrentlyInCreationException( rod.ResourceDescription, objectName, ex); } catch (Exception ex) { throw new ObjectCreationException(rod.ResourceDescription, objectName, "FactoryObject threw exception on object creation.", ex); } // Do not accept a null value for a FactoryBean that's not fully // initialized yet: Many FactoryBeans just return null then. if (instance == null && IsSingletonCurrentlyInCreation(objectName)) { throw new ObjectCurrentlyInCreationException(rod.ResourceDescription, objectName, "FactoryObject which is currently in creation returned null from GetObject."); } if (factory is IConfigurableFactoryObject) { IConfigurableFactoryObject configurableFactory = (IConfigurableFactoryObject)factory; #region Instrumentation if (log.IsDebugEnabled) { log.Debug(string.Format("Factory object with name '{0}' is configurable.", TransformedObjectName(objectName))); } #endregion if (configurableFactory.ProductTemplate != null) { ApplyObjectPropertyValues(instance, objectName, configurableFactory.ProductTemplate); } } if (instance != null) { try { instance = PostProcessObjectFromFactoryObject(instance, objectName); } catch (Exception ex) { throw new ObjectCreationException(rod.ResourceDescription, objectName, "Post-processing of the FactoryObject's object failed.", ex); } } return instance; }
/// <summary> /// Gets the type for the given FactoryObject. /// </summary> /// <param name="factoryObject">The factory object instance to check.</param> /// <returns>the FactoryObject's object type</returns> protected virtual Type GetTypeForFactoryObject(IFactoryObject factoryObject) { try { return factoryObject.ObjectType; } catch (Exception ex) { log.Warn("FactoryObject threw exception from ObjectType, despite the contract saying " + "that it should return null if the type of its object cannot be determined yet", ex); return null; } }
/// <summary> /// Ensure that all non-lazy-init singletons are instantiated, also /// considering <see cref="Spring.Objects.Factory.IFactoryObject"/>s. /// </summary> /// <exception cref="Spring.Objects.ObjectsException"> /// If one of the singleton objects could not be created. /// </exception> /// <seealso cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory.PreInstantiateSingletons"/> public void PreInstantiateSingletons() { #region Instrumentation if (log.IsDebugEnabled) { log.Debug("Pre-instantiating singletons in factory [" + this + "]"); } #endregion try { int definitionCount = objectDefinitionNames.Count; for (int i = 0; i < definitionCount; i++) { string name = (string)objectDefinitionNames[i]; if (!ContainsSingleton(name) && ContainsObjectDefinition(name)) { RootObjectDefinition definition = GetMergedObjectDefinition(name, false); if (!definition.IsAbstract && definition.IsSingleton && !definition.IsLazyInit) { Type objectType = ResolveObjectType(definition, name); if (objectType != null && typeof(IFactoryObject).IsAssignableFrom(definition.ObjectType)) { IFactoryObject factoryObject = (IFactoryObject)GetObject( ObjectFactoryUtils. BuildFactoryObjectName(name)); if (factoryObject.IsSingleton) { GetObject(name); } } else { GetObject(name); } } } } } catch (ObjectsException) { // destroy already created singletons to avoid dangling resources... try { Dispose(); } catch (Exception ex) { log.Error( "PreInstantiateSingletons failed but couldn't destroy any already-created singletons.", ex); } throw; } }