Conceptually speaking, a PluginFamily object represents a point of abstraction or variability in the system. A PluginFamily defines a CLR Type that StructureMap can build, and all of the possible Plugin’s implementing the CLR Type.
Inheritance: HasLifecycle, IDisposable
示例#1
0
        public PluginFamily CreateTemplatedClone(Type[] templateTypes)
        {
            Type templatedType   = _pluginType.MakeGenericType(templateTypes);
            var  templatedFamily = new PluginFamily(templatedType, Parent);

            templatedFamily.DefaultInstanceKey = DefaultInstanceKey;
            templatedFamily._lifecycle         = _lifecycle;

            _instances.GetAll().Select(x =>
            {
                Instance clone = x.CloseType(templateTypes);
                if (clone == null)
                {
                    return(null);
                }

                clone.Name = x.Name;
                return(clone);
            }).Where(x => x != null).Each(templatedFamily.AddInstance);

            //Are there instances that close the templatedtype straight away?
            _instances.GetAll()
            .Where(x => x.ConcreteType.CanBeCastTo(templatedType))
            .Each(templatedFamily.AddInstance);

            // Need to attach the new PluginFamily to the old PluginGraph
            Parent.PluginFamilies.Add(templatedFamily);


            return(templatedFamily);
        }
        public void set_the_scope_to_session_hybrid()
        {
            var family = new PluginFamily(typeof(IServiceProvider));
            family.SetLifecycleTo(WebLifecycles.HybridSession);

            family.Lifecycle.ShouldBeOfType<HybridSessionLifecycle>();
        }
示例#3
0
        public PluginFamily Build(Type type)
        {
            if (!type.GetTypeInfo().IsGenericType)
            {
                return(null);
            }

            var basicType = type.GetGenericTypeDefinition();

            if (basicType == typeof(Func <,>) && type.GetGenericArguments().First() == typeof(string))
            {
                var family        = new PluginFamily(type);
                var typeToBeBuilt = type.GetGenericArguments().Last();
                var templateType  = typeof(FuncByNameInstance <>);

                var @default =
                    Activator.CreateInstance(templateType.MakeGenericType(typeToBeBuilt)) as Instance;

                family.SetDefault(@default);

                return(family);
            }

            return(null);
        }
        public void SetUp()
        {
            family = new PluginFamily(typeof (IService<>));
            PluginGraph.CreateRoot("something").AddFamily(family);

            configuration = new GenericFamilyConfiguration(family, PipelineGraph.BuildEmpty());
        }
        public void Can_NOT_be_plugged_in_if_plugged_type_cannot_be_cast_to_the_plugin_type()
        {
            var instance = new ConfiguredInstance(typeof (ColorRule));
            var family = new PluginFamily(typeof (IWidget));

            instance.As<IDiagnosticInstance>().CanBePartOfPluginFamily(family).ShouldBeFalse();
        }
 private void validateInstance(IDiagnosticInstance instance, PluginFamily family, PluginGraph graph)
 {
     if (!instance.CanBePartOfPluginFamily(family))
     {
         graph.Log.RegisterError(104, instance.CreateToken(), family.PluginType);
     }
 }
示例#7
0
    /// <summary>
    /// Constructor to use when troubleshooting possible configuration issues.
    /// </summary>
    /// <param name="family"></param>
    public InstanceFactory(PluginFamily family)
    {
        if (family == null)
            {
                throw new ArgumentNullException("family");
            }

            try
            {
                _lifecycle = family.Lifecycle;

                _pluginType = family.PluginType;
                MissingInstance = family.MissingInstance;

                family.Instances.Each(AddInstance);
            }
            catch (StructureMapException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new StructureMapException(115, e, family.PluginType.AssemblyQualifiedName);
            }
    }
示例#8
0
        public PluginFamily CreateTemplatedClone(Type[] templateTypes)
        {
            Type templatedType   = _pluginType.MakeGenericType(templateTypes);
            var  templatedFamily = new PluginFamily(templatedType);

            templatedFamily.copyLifecycle(this);

            _instances.GetAll().Select(x => {
                Instance clone = x.CloseType(templateTypes);
                if (clone == null)
                {
                    return(null);
                }

                clone.Name = x.Name;
                return(clone);
            }).Where(x => x != null).Each(templatedFamily.AddInstance);

            if (GetDefaultInstance() != null)
            {
                string   defaultKey = GetDefaultInstance().Name;
                Instance @default   = templatedFamily.Instances.FirstOrDefault(x => x.Name == defaultKey);
                if (@default != null)
                {
                    templatedFamily.SetDefault(@default);
                }
            }

            //Are there instances that close the templatedtype straight away?
            _instances.GetAll()
            .Where(x => x.ConcreteType.CanBeCastTo(templatedType))
            .Each(templatedFamily.AddInstance);

            return(templatedFamily);
        }
        public PluginFamily Build(Type type)
        {
            if (!type.GetTypeInfo().IsGenericType)
            {
                return(null);
            }

            var basicType = type.GetGenericTypeDefinition();

            if (!_graph.Families.Has(basicType))
            {
                // RIGHT HERE: do the connections thing HERE!
                var connectingTypes = _graph.ConnectedConcretions.Where(x => x.CanBeCastTo(type)).ToArray();
                if (connectingTypes.Any())
                {
                    var family = new PluginFamily(type);
                    connectingTypes.Each(family.AddType);

                    return(family);
                }

                return(_graph.Families.ToArray().FirstOrDefault(x => type.GetTypeInfo().IsAssignableFrom(x.PluginType.GetTypeInfo())));
            }

            var basicFamily             = _graph.Families[basicType];
            var templatedParameterTypes = type.GetGenericArguments();

            return(basicFamily.CreateTemplatedClone(templatedParameterTypes.ToArray()));
        }
        public void SetScopeToHybrid()
        {
            var family = new PluginFamily(typeof(IServiceProvider));

            family.SetLifecycleTo(WebLifecycles.Hybrid);
            family.Lifecycle.ShouldBeOfType<HybridLifecycle>();
        }
        public PluginFamily Build(Type type)
        {
            if (!type.GetTypeInfo().IsGenericType) return null;

            var basicType = type.GetGenericTypeDefinition();

            if (!_graph.Families.Has(basicType))
            {
                // RIGHT HERE: do the connections thing HERE!
                var connectingTypes = _graph.ConnectedConcretions.Where(x => x.CanBeCastTo(type)).ToArray();
                if (connectingTypes.Any())
                {
                    var family = new PluginFamily(type);
                    connectingTypes.Each(family.AddType);

                    return family;
                }

                return _graph.Families.ToArray().FirstOrDefault(x => type.GetTypeInfo().IsAssignableFrom(x.PluginType.GetTypeInfo()));
            }

            var basicFamily = _graph.Families[basicType];
            var templatedParameterTypes = type.GetGenericArguments();

            return basicFamily.CreateTemplatedClone(templatedParameterTypes.ToArray());
        }
        public void SetScopeToHttpContext()
        {
            var family = new PluginFamily(typeof(IServiceProvider));
            family.Lifecycle.ShouldBeNull();

            family.SetLifecycleTo(WebLifecycles.HttpContext);
            family.Lifecycle.ShouldBeOfType<HttpContextLifecycle>();
        }
示例#13
0
 private void attachInstances(PluginFamily family, XmlElement familyElement, IGraphBuilder builder)
 {
     familyElement.ForEachChild(INSTANCE_NODE).Do(element =>
     {
         InstanceMemento memento = _mementoCreator.CreateMemento(element);
         family.AddInstance(memento);
     });
 }
示例#14
0
        public void If_a_Memento_does_not_know_its_PluggedType_or_concreteKey_select_the_DEFAULT_Plugin()
        {
            var family = new PluginFamily(typeof (IGateway));
            Plugin plugin = family.AddPlugin(typeof (TheGateway), Plugin.DEFAULT);

            var memento = new MemoryInstanceMemento();
            Assert.AreSame(plugin, memento.FindPlugin(family));
        }
        public void Can_be_plugged_in_if_there_is_a_plugged_type_and_the_plugged_type_can_be_cast_to_the_plugintype()
        {
            var instance = new ConfiguredInstance(typeof (ColorWidget));
            var family = new PluginFamily(typeof (IWidget));

            IDiagnosticInstance diagnosticInstance = instance;
            Assert.IsTrue(diagnosticInstance.CanBePartOfPluginFamily(family));
        }
        public void has_family_but_family_does_not_have_owner()
        {
            var family = new PluginFamily(GetType());
            var instance = new SimpleInstance();
            family.AddInstance(instance);

            instance.Owner().ShouldBeNull();
        }
示例#17
0
        public PluginFamily Build(Type type)
        {
            if (type != typeof (IFancy)) return null;

            var family = new PluginFamily(type);
            family.SetDefault(new SmartInstance<Very>());

            return family;
        }
示例#18
0
        public void still_chooses_PerRequest_if_nothing_is_selected_on_either_family_or_instance()
        {
            var family = new PluginFamily(GetType());

            var i1 = new ConfiguredInstance(GetType()).Named("foo");

            family.AddInstance(i1);

            i1.Lifecycle.ShouldBeOfType<TransientLifecycle>();
        }
        public void FindMaster_Instance_happy_path()
        {
            var family = new PluginFamily(typeof (ISomething));
            ObjectInstance redInstance = new ObjectInstance(new SomethingOne()).WithName("Red");
            family.AddInstance(redInstance);
            family.AddInstance(new ObjectInstance(new SomethingOne()).WithName("Blue"));

            var instance = new ReferencedInstance("Red");
            Assert.AreSame(redInstance, ((IDiagnosticInstance) instance).FindInstanceForProfile(family, null, null));
        }
        private void assertScopeLeadsToInterceptor(string scope, Type interceptorType)
        {
            var att = new PluginFamilyAttribute();
            att.Scope = scope;

            var family = new PluginFamily(typeof (TypeThatDoesNotHaveCustomMementoSource));
            att.Configure(family);

            family.Lifecycle.ShouldBeOfType(interceptorType);
        }
        public void Add_instance_that_does_not_exist_in_destination()
        {
            var source = new PluginFamily(typeof (IWidget));
            var sourceInstance = new ObjectInstance(new AWidget());
            source.AddInstance(sourceInstance);

            var destination = new PluginFamily(typeof (IWidget));
            destination.ImportFrom(source);

            Assert.AreSame(sourceInstance, destination.GetInstance(sourceInstance.Name));
        }
        public static void ConfigureFamily(PluginFamily family)
        {
            var att =
                GetCustomAttribute(family.PluginType, typeof (PluginFamilyAttribute), false)
                as PluginFamilyAttribute;

            if (att != null)
            {
                att.Configure(family);
            }
        }
        public void import_from_another_family_will_override_the_build_policy_if_the_initial_policy_is_the_default()
        {
            var factory = new InstanceFactory(typeof (IWidget));

            var family = new PluginFamily(typeof (IWidget));
            family.SetScopeTo(InstanceScope.Singleton);

            factory.ImportFrom(family);

            factory.Lifecycle.ShouldBeOfType<SingletonLifecycle>();
        }
示例#24
0
 /// <summary>
 /// Removes a PluginFamily from this PluginGraph
 /// and disposes that family and all of its Instance's
 /// </summary>
 /// <param name="pluginType"></param>
 public void EjectFamily(Type pluginType)
 {
     if (_families.ContainsKey(pluginType))
     {
         PluginFamily family = null;
         if (_families.TryRemove(pluginType, out family))
         {
             family.SafeDispose();
         }
     }
 }
        public PluginFamily Build(Type type)
        {
            if (EnumerableInstance.IsEnumerable(type))
            {
                var family = new PluginFamily(type);
                family.SetDefault(new AllPossibleInstance(type));

                return family;
            }

            return null;
        }
        public PluginFamily Build(Type type)
        {
            if (EnumerableInstance.IsEnumerable(type))
            {
                var family = new PluginFamily(type);
                family.SetDefault(new AllPossibleInstance(type));

                return(family);
            }

            return(null);
        }
        public void Can_be_part_of_PluginFamily()
        {
            var target = new ATarget();
            var instance = new ObjectInstance(target);
            IDiagnosticInstance diagnosticInstance = instance;

            var family1 = new PluginFamily(typeof (ATarget));
            Assert.IsTrue(diagnosticInstance.CanBePartOfPluginFamily(family1));

            var family2 = new PluginFamily(GetType());
            Assert.IsFalse(diagnosticInstance.CanBePartOfPluginFamily(family2));
        }
示例#28
0
        public void does_override_the_scope_of_the_parent()
        {
            var family = new PluginFamily(GetType());
            family.SetLifecycleTo(Lifecycles.Singleton);

            var i1 = new ConfiguredInstance(GetType()).Named("foo");
            i1.SetLifecycleTo(Lifecycles.ThreadLocal);

            family.AddInstance(i1);

            i1.Lifecycle.ShouldBeOfType<ThreadLocalStorageLifecycle>();
        }
示例#29
0
        public PluginFamily FindExistingOrCreateFamily(Type pluginType)
        {
            if (_families.Has(pluginType))
            {
                return(_families[pluginType]);
            }

            var family = new PluginFamily(pluginType);

            _families[pluginType] = family;

            return(family);
        }
示例#30
0
        internal PluginFamily FindExistingOrCreateFamily(Type pluginType)
        {
            if (_families.ContainsKey(pluginType))
            {
                return(_families[pluginType]);
            }

            var family = new PluginFamily(pluginType);

            _families[pluginType] = family;

            return(family);
        }
示例#31
0
        public PluginFamily Build(Type type)
        {
            if (type.Name.EndsWith("Settings") && type.IsConcreteWithDefaultCtor())
            {
                var family = new PluginFamily(type);
                var instance = buildInstanceForType(type);
                family.SetDefault(instance);

                return family;
            }

            return null;
        }
示例#32
0
    public static InstanceFactory CreateFactoryForFamily(PluginFamily family, ProfileManager profileManager)
    {
        family.Seal();

            var factory = new InstanceFactory(family);

            Instance instance = family.GetDefaultInstance();
            if(instance != null) {
                profileManager.SetDefault(family.PluginType, instance);
            }

            return factory;
    }
        public void Do_not_override_named_instance()
        {
            var source = new PluginFamily(typeof (IWidget));
            ObjectInstance sourceInstance = new ObjectInstance(new AWidget()).WithName("New");
            source.AddInstance(sourceInstance);

            var destination = new PluginFamily(typeof (IWidget));
            ObjectInstance destinationInstance = new ObjectInstance(new AWidget()).WithName("New");
            destination.AddInstance(destinationInstance);

            destination.ImportFrom(source);

            Assert.AreSame(destinationInstance, destination.GetInstance(sourceInstance.Name));
        }
示例#34
0
        public void CopyDefault(Type sourceType, Type destinationType, PluginFamily family)
        {
            if (!_instances.ContainsKey(sourceType)) return;

            Instance sourceInstance = _instances[sourceType];
            if (sourceInstance.IsReference)
            {
                _instances.Add(destinationType, sourceInstance);
            }
            else
            {
                family.ForInstance(sourceInstance.Name, x => { _instances.Add(destinationType, x); });
            }
        }
        public void Import_from_family_picks_up_new_instances()
        {
            var factory = new InstanceFactory(typeof (IWidget));

            var family = new PluginFamily(typeof (IWidget));
            family.AddInstance(new ObjectInstance(new AWidget()).WithName("New"));
            family.AddInstance(new ObjectInstance(new AWidget()).WithName("New2"));
            family.AddInstance(new ObjectInstance(new AWidget()).WithName("New3"));

            factory.ImportFrom(family);

            Assert.IsNotNull(factory.FindInstance("New"));
            Assert.IsNotNull(factory.FindInstance("New2"));
            Assert.IsNotNull(factory.FindInstance("New3"));
        }
        public void do_not_replace_the_build_Lifecycle_if_it_is_the_same_type_as_the_imported_family()
        {
            var originalFamily = new PluginFamily(typeof (IWidget));
            originalFamily.SetScopeTo(InstanceScope.Singleton);
            var factory = new InstanceFactory(originalFamily);

            ILifecycle originalLifecycle = factory.Lifecycle;

            var family = new PluginFamily(typeof (IWidget));
            family.SetScopeTo(InstanceScope.Singleton);

            factory.ImportFrom(family);

            factory.Lifecycle.ShouldBeTheSameAs(originalLifecycle);
        }
        public PluginFamily Build(Type pluginType)
        {
            if (!pluginType.IsAbstract && pluginType.IsClass)
            {
                return null;
            }

            var family = new PluginFamily(pluginType);
            family.SetDefault(() => {
                var service = _locator.Service(pluginType);

                return new ObjectInstance(service);
            });

            return family;
        }
示例#38
0
        public void CopyDefaults(Type basicType, Type templatedType, PluginFamily family)
        {
            _default.CopyDefault(basicType, templatedType, family);
            foreach (var pair in _profiles)
            {
                pair.Value.CopyDefault(basicType, templatedType, family);
            }

            if (!string.IsNullOrEmpty(CurrentProfile))
            {
                Profile theSourceProfile = getProfile(CurrentProfile);
                theSourceProfile.FillAllTypesInto(_currentProfile);
            }

            _default.FillAllTypesInto(_currentProfile);
        }
示例#39
0
        public void ImportFrom(PluginFamily source)
        {
            if (source.Lifecycle != null)
            {
                SetScopeTo(source.Lifecycle);
            }

            source.Instances.Each(instance => _instances.Fill(instance.Name, instance));

            source._pluggedTypes.Each((key, plugin) => _pluggedTypes.Fill(key, plugin));

            if (source.MissingInstance != null)
            {
                MissingInstance = source.MissingInstance;
            }
        }
        private PluginFamily tryToConnect(Type type)
        {
            // RIGHT HERE: do the connections thing HERE!
            var connectingTypes = _graph.ConnectedConcretions.ToArray().Where(x => x.CanBeCastTo(type)).ToArray();

            if (connectingTypes.Any())
            {
                var family = new PluginFamily(type);
                connectingTypes.Each(family.AddType);

                return(family);
            }

            // This is a problem right here. Need this to be exposed
            return(_graph.Families.ToArray()
                   .FirstOrDefault(x => type.GetTypeInfo().IsAssignableFrom(x.PluginType.GetTypeInfo())));
        }
        public PluginFamily CreateTemplatedFamily(Type templatedType, ProfileManager profileManager)
        {
            Type basicType = templatedType.GetGenericTypeDefinition();

            if (_families.Has(basicType))
            {
                PluginFamily basicFamily             = _families[basicType];
                Type[]       templatedParameterTypes = templatedType.GetGenericArguments();


                PluginFamily templatedFamily = basicFamily.CreateTemplatedClone(templatedParameterTypes);
                PluginFamily family          = templatedFamily;
                profileManager.CopyDefaults(basicType, templatedType, family);

                return(family);
            }
            else
            {
                return(null);
            }
        }
示例#42
0
 public void AddFamily(PluginFamily family)
 {
     family.Owner = this;
     _families[family.PluginType] = family;
 }
 public void AddFamily(PluginFamily family)
 {
     _families[family.PluginType] = family;
 }
示例#44
0
 public abstract void Alter(PluginFamily family);
        public void ImportFrom(PluginFamily sourceFamily)
        {
            PluginFamily destinationFamily = FindFamily(sourceFamily.PluginType);

            destinationFamily.ImportFrom(sourceFamily);
        }