public static void SetUp(Container container)
        {
            bool     enableLocalization               = true;
            string   absoluteFileName                 = HostingEnvironment.MapPath("~/Mvc.sitemap");
            TimeSpan absoluteCacheExpiration          = TimeSpan.FromMinutes(5);
            bool     visibilityAffectsDescendants     = true;
            bool     useTitleIfDescriptionNotProvided = true;

#if Demo
            // Settings for MvcMusicStore demo: don't copy into your project
            bool     securityTrimmingEnabled  = true;
            string[] includeAssembliesForScan = new string[] { "Mvc Music Store" };
#else
            bool     securityTrimmingEnabled  = false;
            string[] includeAssembliesForScan = new string[] { "$AssemblyName$" };
#endif

            // Extension to allow resolution of arrays by GetAllInstances (natively based on IEnumerable).
            // source from: https://simpleinjector.codeplex.com/wikipage?title=CollectionRegistrationExtensions
            AllowToResolveArraysAndLists(container);

            var currentAssembly         = typeof(MvcSiteMapProviderContainerInitializer).Assembly;
            var siteMapProviderAssembly = typeof(SiteMaps).Assembly;
            var allAssemblies           = new Assembly[] { currentAssembly, siteMapProviderAssembly };
            var excludeTypes            = new Type[]
            {
                // Use this array to add types you wish to explicitly exclude from convention-based
                // auto-registration. By default all types that either match I[TypeName] = [TypeName] or
                // I[TypeName] = [TypeName]Adapter will be automatically wired up as long as they don't
                // have the [ExcludeFromAutoRegistrationAttribute].
                //
                // If you want to override a type that follows the convention, you should add the name
                // of either the implementation name or the interface that it inherits to this list and
                // add your manual registration code below. This will prevent duplicate registrations
                // of the types from occurring.

                // Example:
                // typeof(SiteMap),
                // typeof(SiteMapNodeVisibilityProviderStrategy)
            };
            var multipleImplementationTypes = new Type[]
            {
                typeof(ISiteMapNodeUrlResolver),
                typeof(ISiteMapNodeVisibilityProvider),
                typeof(IDynamicNodeProvider)
            };

            // Matching type name (I[TypeName] = [TypeName]) or matching type name + suffix Adapter (I[TypeName] = [TypeName]Adapter)
            // and not decorated with the [ExcludeFromAutoRegistrationAttribute].
            CommonConventions.RegisterDefaultConventions(
                (interfaceType, implementationType) => container.RegisterSingle(interfaceType, implementationType),
                new Assembly[] { siteMapProviderAssembly },
                allAssemblies,
                excludeTypes,
                string.Empty);

            // Multiple implementations of strategy based extension points (and not decorated with [ExcludeFromAutoRegistrationAttribute]).
            CommonConventions.RegisterAllImplementationsOfInterfaceSingle(
                (interfaceType, implementationTypes) => container.RegisterAll(interfaceType, implementationTypes),
                multipleImplementationTypes,
                allAssemblies,
                excludeTypes,
                string.Empty);

            container.Register <XmlSiteMapController>();

            // Visibility Providers
            container.RegisterSingle <ISiteMapNodeVisibilityProviderStrategy>(() =>
                                                                              new SiteMapNodeVisibilityProviderStrategy(
                                                                                  container.GetAllInstances <ISiteMapNodeVisibilityProvider>().ToArray(), string.Empty));

            // Pass in the global controllerBuilder reference
            container.RegisterSingle <ControllerBuilder>(() => ControllerBuilder.Current);

            container.RegisterSingle <IControllerTypeResolverFactory>(() =>
                                                                      new ControllerTypeResolverFactory(
                                                                          new string[0],
                                                                          container.GetInstance <IControllerBuilder>(),
                                                                          container.GetInstance <IBuildManager>()));

            // Configure Security
            container.RegisterAll <IAclModule>(typeof(AuthorizeAttributeAclModule), typeof(XmlRolesAclModule));
            container.RegisterSingle <IAclModule>(() => new CompositeAclModule(container.GetAllInstances <IAclModule>().ToArray()));

            // Setup cache
#if NET35
            container.RegisterSingleOpenGeneric(typeof(ICacheProvider <>), typeof(AspNetCacheProvider <>));
            container.RegisterSingle <ICacheDependency>(() => new AspNetFileCacheDependency(absoluteFileName));
#else
            container.RegisterSingle <System.Runtime.Caching.ObjectCache>(() => System.Runtime.Caching.MemoryCache.Default);
            container.RegisterSingleOpenGeneric(typeof(ICacheProvider <>), typeof(RuntimeCacheProvider <>));
            container.RegisterSingle <ICacheDependency>(() => new RuntimeFileCacheDependency(absoluteFileName));
#endif
            container.RegisterSingle <ICacheDetails>(() => new CacheDetails(absoluteCacheExpiration, TimeSpan.MinValue, container.GetInstance <ICacheDependency>()));

            // Configure the visitors
            container.RegisterSingle <ISiteMapNodeVisitor, UrlResolvingSiteMapNodeVisitor>();

            // Prepare for the sitemap node providers
            container.RegisterSingle <IReservedAttributeNameProvider>(() => new ReservedAttributeNameProvider(new string[0]));
            container.RegisterSingle <IXmlSource>(() => new FileXmlSource(absoluteFileName));

            // Register the sitemap node providers
            container.RegisterSingle <XmlSiteMapNodeProvider>(() => container.GetInstance <XmlSiteMapNodeProviderFactory>()
                                                              .Create(container.GetInstance <IXmlSource>()));
            container.RegisterSingle <ReflectionSiteMapNodeProvider>(() => container.GetInstance <ReflectionSiteMapNodeProviderFactory>()
                                                                     .Create(includeAssembliesForScan));

            // Register the sitemap builders
            container.RegisterSingle <ISiteMapBuilder>(() => container.GetInstance <SiteMapBuilderFactory>()
                                                       .Create(new CompositeSiteMapNodeProvider(container.GetInstance <XmlSiteMapNodeProvider>(), container.GetInstance <ReflectionSiteMapNodeProvider>())));

            container.RegisterAll <ISiteMapBuilderSet>(
                ResolveISiteMapBuilderSets(container, securityTrimmingEnabled, enableLocalization, visibilityAffectsDescendants, useTitleIfDescriptionNotProvided));
            container.RegisterSingle <ISiteMapBuilderSetStrategy>(() => new SiteMapBuilderSetStrategy(container.GetAllInstances <ISiteMapBuilderSet>().ToArray()));
        }
        public static void SetUp(Container container)
        {
            bool     securityTrimmingEnabled = false;
            bool     enableLocalization      = true;
            string   absoluteFileName        = HostingEnvironment.MapPath("~/Mvc.sitemap");
            TimeSpan absoluteCacheExpiration = TimeSpan.FromMinutes(5);

            string[] includeAssembliesForScan = new string[] { "$AssemblyName$" };

            // Extension to allow resolution of arrays by GetAllInstances (natively based on IEnumerable).
            // source from: https://simpleinjector.codeplex.com/wikipage?title=CollectionRegistrationExtensions
            AllowToResolveArraysAndLists(container);

            var currentAssembly         = typeof(MvcSiteMapProviderContainerInitializer).Assembly;
            var siteMapProviderAssembly = typeof(SiteMaps).Assembly;
            var allAssemblies           = new Assembly[] { currentAssembly, siteMapProviderAssembly };
            var excludeTypes            = new Type[]
            {
                typeof(SiteMapNodeVisibilityProviderStrategy),
                typeof(SiteMapXmlReservedAttributeNameProvider),
                typeof(SiteMapBuilderSetStrategy),
                typeof(ControllerTypeResolverFactory),

                // Added 2013-06-28 by eric-b to avoid default singleton registration:
                typeof(XmlSiteMapController),

                // Added 2013-06-28 by eric-b for SimpleInjector.Verify method:
                typeof(PreservedRouteParameterCollection),
                typeof(MvcResolver),
                typeof(MvcSiteMapProvider.SiteMap),
                typeof(MetaRobotsValueCollection),
                typeof(RoleCollection),
                typeof(SiteMapPluginProvider),
                typeof(ControllerTypeResolver),
                typeof(RouteValueDictionary),
                typeof(AttributeDictionary)
            };
            var multipleImplementationTypes = new Type[]
            {
                typeof(ISiteMapNodeUrlResolver),
                typeof(ISiteMapNodeVisibilityProvider),
                typeof(IDynamicNodeProvider)
            };

            // Single implementations of interface with matching name (minus the "I").
            CommonConventions.RegisterDefaultConventions(
                (interfaceType, implementationType) => container.RegisterSingle(interfaceType, implementationType),
                new Assembly[] { siteMapProviderAssembly },
                allAssemblies,
                excludeTypes,
                string.Empty);

            // Multiple implementations of strategy based extension points
            CommonConventions.RegisterAllImplementationsOfInterfaceSingle(
                (interfaceType, implementationTypes) => container.RegisterAll(interfaceType, implementationTypes),
                multipleImplementationTypes,
                allAssemblies,
                new Type[0],
                "^Composite");

            container.Register <XmlSiteMapController>();

            // Visibility Providers
            container.RegisterSingle <ISiteMapNodeVisibilityProviderStrategy>(() =>
                                                                              new SiteMapNodeVisibilityProviderStrategy(
                                                                                  container.GetAllInstances
                                                                                  <ISiteMapNodeVisibilityProvider>().
                                                                                  ToArray(), string.Empty));

            // Pass in the global controllerBuilder reference
            container.RegisterSingle <ControllerBuilder>(() => ControllerBuilder.Current);

            container.RegisterSingle <IControllerBuilder, ControllerBuilderAdaptor>();

            container.RegisterSingle <IBuildManager, BuildManagerAdaptor>();

            container.RegisterSingle <IControllerTypeResolverFactory>(() =>
                                                                      new ControllerTypeResolverFactory(new string[0],
                                                                                                        container.GetInstance
                                                                                                        <IControllerBuilder
                                                                                                        >(),
                                                                                                        container.GetInstance
                                                                                                        <IBuildManager>()));

            // Configure Security
            container.RegisterSingle <AuthorizeAttributeAclModule>(() => new AuthorizeAttributeAclModule(
                                                                       container.GetInstance <IMvcContextFactory>(),
                                                                       container.GetInstance <IObjectCopier>(),
                                                                       container.GetInstance <IControllerDescriptorFactory>(),
                                                                       container.GetInstance <IControllerBuilder>(),
                                                                       container.GetInstance <IAuthorizeAttributeBuilder>(),
                                                                       container.GetInstance <IGlobalFilterProvider>()));

            container.RegisterAll <IAclModule>(typeof(AuthorizeAttributeAclModule), typeof(XmlRolesAclModule));
            container.RegisterSingle <IAclModule>(() => new CompositeAclModule(container.GetAllInstances <IAclModule>().ToArray()));

            // Setup cache
#if NET35
            container.RegisterSingle <ICacheProvider <ISiteMap>, AspNetCacheProvider <ISiteMap> >();
            container.RegisterSingle <ICacheDependency>(() => new AspNetFileCacheDependency(absoluteFileName));
#else
            container.RegisterSingle <System.Runtime.Caching.ObjectCache>(() => System.Runtime.Caching.MemoryCache.Default);
            container.RegisterSingle <ICacheProvider <ISiteMap>, RuntimeCacheProvider <ISiteMap> >();
            container.RegisterSingle <ICacheDependency>(() => new RuntimeFileCacheDependency(absoluteFileName));
#endif
            container.RegisterSingle <ICacheDetails>(() => new CacheDetails(absoluteCacheExpiration, TimeSpan.MinValue, container.GetInstance <ICacheDependency>()));

            // Configure the visitors
            container.RegisterSingle <ISiteMapNodeVisitor, UrlResolvingSiteMapNodeVisitor>();


            // Register the sitemap builder
            container.RegisterSingle <ISiteMapXmlReservedAttributeNameProvider>(
                () => new SiteMapXmlReservedAttributeNameProvider(new string[0]));

            container.RegisterSingle <IXmlSource>(() => new FileXmlSource(absoluteFileName));


            container.RegisterSingle <XmlSiteMapBuilder>(() =>
                                                         new XmlSiteMapBuilder(
                                                             container.GetInstance <IXmlSource>(),
                                                             container.GetInstance <ISiteMapXmlReservedAttributeNameProvider>(),
                                                             container.GetInstance <INodeKeyGenerator>(),
                                                             container.GetInstance <IDynamicNodeBuilder>(),
                                                             container.GetInstance <ISiteMapNodeFactory>(),
                                                             container.GetInstance <ISiteMapXmlNameProvider>()
                                                             ));

            container.RegisterSingle <ReflectionSiteMapBuilder>(() =>
                                                                new ReflectionSiteMapBuilder(
                                                                    includeAssembliesForScan,
                                                                    new string[0],
                                                                    container.GetInstance
                                                                    <ISiteMapXmlReservedAttributeNameProvider>(),
                                                                    container.GetInstance <INodeKeyGenerator>(),
                                                                    container.GetInstance <IDynamicNodeBuilder>(),
                                                                    container.GetInstance <ISiteMapNodeFactory>(),
                                                                    container.GetInstance <ISiteMapCacheKeyGenerator>()
                                                                    ));

            container.RegisterAll <ISiteMapBuilderSet>(ResolveISiteMapBuilderSets(container, securityTrimmingEnabled, enableLocalization));
            container.RegisterSingle <ISiteMapBuilderSetStrategy>(() => new SiteMapBuilderSetStrategy(container.GetAllInstances <ISiteMapBuilderSet>().ToArray()));

            container.RegisterSingle <VisitingSiteMapBuilder>();
        }
コード例 #3
0
        public static void SetUp(Container container)
        {
            bool enableLocalization = true;
            // xml not supported yet
            //string absoluteFileName = HostingEnvironment.MapPath("~/Mvc.sitemap");
            TimeSpan absoluteCacheExpiration          = TimeSpan.FromMinutes(5);
            bool     visibilityAffectsDescendants     = true;
            bool     useTitleIfDescriptionNotProvided = true;
            bool     securityTrimmingEnabled          = false;

            string[] includeAssembliesForScan = new string[] { "DemoApp" };


// Extension to allow resolution of arrays by GetAllInstances (natively based on IEnumerable).
// source from: https://simpleinjector.codeplex.com/wikipage?title=CollectionRegistrationExtensions
            container.AllowToResolveArraysAndLists();

            var currentAssembly         = typeof(MvcSiteMapProviderContainerInitializer).Assembly;
            var siteMapProviderAssembly = typeof(SiteMaps).Assembly;
            var allAssemblies           = new Assembly[] { currentAssembly, siteMapProviderAssembly };
            var excludeTypes            = new Type[]
            {
                typeof(IMvcContextFactory),
                // xml not supported yet
                typeof(MvcSiteMapProvider.Xml.ISiteMapXmlValidator),
                typeof(MvcSiteMapProvider.Xml.ISiteMapXmlNameProvider),
                typeof(IXmlSiteMapResultFactory)
            };
            var multipleImplementationTypes = new Type[]
            {
                typeof(ISiteMapNodeUrlResolver),
                typeof(ISiteMapNodeVisibilityProvider),
                typeof(IDynamicNodeProvider)
            };

// Matching type name (I[TypeName] = [TypeName]) or matching type name + suffix Adapter (I[TypeName] = [TypeName]Adapter)
// and not decorated with the [ExcludeFromAutoRegistrationAttribute].
            CommonConventions.RegisterDefaultConventions(
                (interfaceType, implementationType) => container.Register(interfaceType, implementationType),
                new Assembly[] { siteMapProviderAssembly },
                allAssemblies,
                excludeTypes,
                string.Empty);

// Multiple implementations of strategy based extension points (and not decorated with [ExcludeFromAutoRegistrationAttribute]).
            CommonConventions.RegisterAllImplementationsOfInterfaceSingle(
                (interfaceType, implementationTypes) => container.RegisterCollection(interfaceType, implementationTypes),
                multipleImplementationTypes,
                allAssemblies,
                excludeTypes,
                string.Empty);

            // xml not supported yet
            //container.RegisterMvcController<XmlSiteMapController>();

            container.Register <IMvcContextFactory, MvcContextFactory>(Lifestyle.Singleton);

// Visibility Providers
            container.Register <ISiteMapNodeVisibilityProviderStrategy>(() =>
                                                                        new SiteMapNodeVisibilityProviderStrategy(
                                                                            container.GetAllInstances <ISiteMapNodeVisibilityProvider>().ToArray(), string.Empty),
                                                                        Lifestyle.Singleton);

// Pass in the global controllerBuilder reference
            container.Register <ControllerBuilder>(() => ControllerBuilder.Current, Lifestyle.Singleton);

            container.Register <IControllerTypeResolverFactory>(() =>
                                                                new ControllerTypeResolverFactory(
                                                                    new string[0],
                                                                    container.GetInstance <IControllerBuilder>(),
                                                                    container.GetInstance <IBuildManager>()),
                                                                Lifestyle.Singleton);

// Configure Security
            // xml not supported yet (disable XmlRolesAclModule)
            container.RegisterCollection(typeof(IAclModule), new Type[] { typeof(AuthorizeAttributeAclModule) /*, typeof(XmlRolesAclModule)*/ });
            container.Register <IAclModule>(() => new CompositeAclModule(container.GetAllInstances <IAclModule>().ToArray()), Lifestyle.Singleton);

// Setup cache
            container.Register <System.Runtime.Caching.ObjectCache>(() => System.Runtime.Caching.MemoryCache.Default, Lifestyle.Singleton);
            container.RegisterOpenGeneric(typeof(ICacheProvider <>), typeof(RuntimeCacheProvider <>), Lifestyle.Singleton);
            // xml not supported yet
            //container.Register<ICacheDependency>(() => new RuntimeFileCacheDependency(absoluteFileName), Lifestyle.Singleton);
            container.Register <ICacheDependency>(() => new NullCacheDependency(), Lifestyle.Singleton);

            container.Register <ICacheDetails>(() => new CacheDetails(absoluteCacheExpiration, TimeSpan.MinValue, container.GetInstance <ICacheDependency>()), Lifestyle.Singleton);

// Configure the visitors
            container.Register <ISiteMapNodeVisitor, UrlResolvingSiteMapNodeVisitor>(Lifestyle.Singleton);

// Prepare for the sitemap node providers
            container.Register <IReservedAttributeNameProvider>(() => new ReservedAttributeNameProvider(new string[0]), Lifestyle.Singleton);
            // xml not supported yet
            //container.Register<IXmlSource>(() => new FileXmlSource(absoluteFileName), Lifestyle.Singleton);

// Register the sitemap node providers
            // xml not supported yet
            //container.Register<XmlSiteMapNodeProvider>(() => container.GetInstance<XmlSiteMapNodeProviderFactory>()
            //	.Create(container.GetInstance<IXmlSource>()),
            //	Lifestyle.Singleton);
            container.Register <ReflectionSiteMapNodeProvider>(() => container.GetInstance <ReflectionSiteMapNodeProviderFactory>()
                                                               .Create(includeAssembliesForScan),
                                                               Lifestyle.Singleton);

// Register the sitemap builders
            // xml not supported yet
            //container.Register<ISiteMapBuilder>(() => container.GetInstance<SiteMapBuilderFactory>()
            //	.Create(new CompositeSiteMapNodeProvider(container.GetInstance<XmlSiteMapNodeProvider>(), container.GetInstance<ReflectionSiteMapNodeProvider>())),
            //	Lifestyle.Singleton);
            container.Register <ISiteMapBuilder>(() => container.GetInstance <SiteMapBuilderFactory>()
                                                 .Create(container.GetInstance <ReflectionSiteMapNodeProvider>()), Lifestyle.Singleton);

            container.RegisterCollection <ISiteMapBuilderSet>(
                ResolveISiteMapBuilderSets(container, securityTrimmingEnabled, enableLocalization, visibilityAffectsDescendants, useTitleIfDescriptionNotProvided));
            container.Register <ISiteMapBuilderSetStrategy>(() => new SiteMapBuilderSetStrategy(container.GetAllInstances <ISiteMapBuilderSet>().ToArray()),
                                                            Lifestyle.Singleton);
        }