protected UnityConfigurationSection SerializeAndLoadConfig(string filename, Action<ContainerElement> containerInitializer)
        {
            var serializer = new ConfigSerializer(filename);
            var section = new UnityConfigurationSection();
            section.SectionExtensions.Add(new SectionExtensionElement()
            {
                TypeName = typeof(InterceptionConfigurationExtension).AssemblyQualifiedName
            });

            var container = new ContainerElement();
            section.Containers.Add(container);

            containerInitializer(container);

            serializer.Save("unity", section);

            return (UnityConfigurationSection)serializer.Load().GetSection("unity");
        }
示例#2
0
 private static ContainerElement GuardContainerExists(string configuredContainerName, ContainerElement containerElement)
 {
     if (containerElement == null)
     {
         throw new ArgumentException(
                   string.Format(CultureInfo.CurrentCulture,
                                 Constants.NoSuchContainer, configuredContainerName),
                   "configuredContainerName");
     }
     return(containerElement);
 }
 private static ContainerElement GuardContainerExists(string configuredContainerName, ContainerElement containerElement)
 {
     if (containerElement == null)
     {
         throw new ArgumentException(
             string.Format(CultureInfo.CurrentCulture,
                 Resources.NoSuchContainer, configuredContainerName),
             "configuredContainerName");
     }
     return containerElement;
 }
        private static void CreateConfigWithCallHandlers(ContainerElement c)
        {
            var interceptionElement = new InterceptionElement();
            var policy = new PolicyElement() { Name = "PolicyOne" };
            policy.MatchingRules.Add(new MatchingRuleElement() { Name =  "All", TypeName = "AlwaysMatchingRule" });

            policy.CallHandlers.Add(new CallHandlerElement()
            {
                Name = "NamedRule"
            });

            policy.CallHandlers.Add(new CallHandlerElement()
            {
                Name = "NameAndType",
                TypeName = "DoMoreRule"
            });

            policy.CallHandlers.Add(new CallHandlerElement()
            {
                Name = "HandlerWithLifetime",
                TypeName = "DoSomethingRule",
                Lifetime = new LifetimeElement()
                {
                    TypeName = "singleton"
                }
            });

            var handlerWithMembers = new CallHandlerElement()
            {
                Name = "HandlerWithElements",
                TypeName = "CallCountHandler"
            };
            handlerWithMembers.Injection.Add(new ConstructorElement());
            handlerWithMembers.Injection.Add(new PropertyElement() { Name = "MyProp" });

            policy.CallHandlers.Add(handlerWithMembers);

            interceptionElement.Policies.Add(policy);

            c.ConfiguringElements.Add(interceptionElement);
        }
        private static void CreateConfigWithMatchingRules(ContainerElement c)
        {
            var interceptionElement = new InterceptionElement();
            var policy = new PolicyElement() {Name = "PolicyOne"};
            policy.MatchingRules.Add(new MatchingRuleElement()
            {
                Name = "NameOnly"
            });

            policy.MatchingRules.Add(new MatchingRuleElement()
            {
                Name = "NameAndType",
                TypeName = "AlwaysMatchingRule"
            });

            policy.MatchingRules.Add(new MatchingRuleElement
            {
                Name = "RuleWithLifetime",
                TypeName = "AlwaysMatchingRule",
                Lifetime = new LifetimeElement()
                {
                    TypeName = "singleton"
                }
            });

            var ruleWithMembers = new MatchingRuleElement
            {
                Name = "RuleWithElements",
                TypeName = "AlwaysMatchingRule"
            };
            ruleWithMembers.Injection.Add(new ConstructorElement());
            ruleWithMembers.Injection.Add(new PropertyElement() { Name = "MyProp" } );

            policy.MatchingRules.Add(ruleWithMembers);

            interceptionElement.Policies.Add(policy);

            c.ConfiguringElements.Add(interceptionElement);
        }