Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for container Grace #328

Merged
merged 6 commits into from
Jul 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions build/runbuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ task NuGet -depends Compile -description "This tasks makes creates the NuGet pac

Create-MvcSiteMapProvider-Web-Package

Create-DIContainer-Packages ("Autofac", "Ninject", "SimpleInjector", "StructureMap", "Unity", "Windsor")
Create-DIContainer-Packages ("Autofac", "Grace", "Ninject", "SimpleInjector", "StructureMap", "Unity", "Windsor")
}

task Finalize -depends NuGet -description "This tasks finalizes the build" {
Expand Down Expand Up @@ -262,17 +262,25 @@ function Create-DIContainer-Packages ([string[]] $di_containers) {
Write-Host $di_container -ForegroundColor Yellow

#exception: SimpleInjector version 2 doesn't support .NET 3.5
if ($di_container -ne "SimpleInjector") {
if ($di_container -ne "SimpleInjector" -and $di_container -ne "Grace") {
Create-DIContainer-Package $di_container ("net35", "net40", "net45") -mvc_version "2"
Create-DIContainer-Modules-Package $di_container ("net35", "net40", "net45") -mvc_version "2"
}

Create-DIContainer-Package $di_container ("net40", "net45") -mvc_version "3"
Create-DIContainer-Modules-Package $di_container ("net40", "net45") -mvc_version "3"
if ($di_container -ne "Grace") {
Create-DIContainer-Package $di_container ("net40", "net45") -mvc_version "3"
Create-DIContainer-Modules-Package $di_container ("net40", "net45") -mvc_version "3"

Create-DIContainer-Package $di_container ("net40", "net45") -mvc_version "4"
Create-DIContainer-Modules-Package $di_container ("net40", "net45") -mvc_version "4"
} else {
Create-DIContainer-Package $di_container ("net45") -mvc_version "3"
Create-DIContainer-Modules-Package $di_container ("net45") -mvc_version "3"

Create-DIContainer-Package $di_container ("net45") -mvc_version "4"
Create-DIContainer-Modules-Package $di_container ("net45") -mvc_version "4"
}

Create-DIContainer-Package $di_container ("net40", "net45") -mvc_version "4"
Create-DIContainer-Modules-Package $di_container ("net40", "net45") -mvc_version "4"

Create-DIContainer-Package $di_container ("net45") -mvc_version "5"
Create-DIContainer-Modules-Package $di_container ("net45") -mvc_version "5"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<package inherits="MvcSiteMapProvider.Configuration.Shared.nuspec" xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<metadata>
<dependencies>
<group targetFramework="net45" xdt:Locator="Match(targetFramework)">
<dependency id="Grace" version="2.3.6" xdt:Transform="Insert" />
</group>
</dependencies>
</metadata>
</package>

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using DI;
using DI.Grace;
using DI.Grace.Modules;

internal class CompositionRoot
{
public static IDependencyInjectionContainer Compose()
{
// Create new container
var container = new Grace.DependencyInjection.DependencyInjectionContainer();

// Install MVC sitemap provider
container.Configure(new MvcSiteMapProviderModule());

// Install Controllers
container.Configure(new MvcModule());

// Add your DI configuration here

return new GraceDependencyInjectionContainer(container.RootScope);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using Grace.DependencyInjection;

namespace DI.Grace
{
internal class GraceDependencyInjectionContainer
: IDependencyInjectionContainer
{
public GraceDependencyInjectionContainer(IExportLocator container)
{
if (container == null)
throw new ArgumentNullException("container");

this.container = container;
}
private readonly IExportLocator container;

public object GetInstance(Type type)
{
return container.Locate(type);
}

public object TryGetInstance(Type type)
{
try
{
return container.Locate(type);
}
catch
{
return null;
}
}

public IEnumerable<object> GetAllInstances(Type type)
{
return container.LocateAll(type);
}

public void Release(object instance)
{
// Ddo nothing
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Grace.DependencyInjection;

namespace DI.Grace.Modules
{
public class MvcModule : IConfigurationModule
{
public void Configure(IExportRegistrationBlock registrationBlock)
{

registrationBlock.Export(AllTypes())
.BasedOn<IController>()
.ByType()
.ExternallyOwned();
}

private IEnumerable<Type> AllTypes()
{
return GetType().Assembly.ExportedTypes;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Hosting;
using System.Reflection;
using Grace.DependencyInjection;
using Microsoft.SqlServer.Server;
using MvcSiteMapProvider;
using MvcSiteMapProvider.Builder;
using MvcSiteMapProvider.Caching;
using MvcSiteMapProvider.Security;
using MvcSiteMapProvider.Visitor;
using MvcSiteMapProvider.Web.Compilation;
using MvcSiteMapProvider.Web.Mvc;
using MvcSiteMapProvider.Web.UrlResolver;
using MvcSiteMapProvider.Xml;

namespace DI.Grace.Modules
{
public class MvcSiteMapProviderModule : IConfigurationModule
{
public void Configure(IExportRegistrationBlock container)
{
bool enableLocalization = true;
string absoluteFileName = HostingEnvironment.MapPath("~/Mvc.sitemap");
TimeSpan absoluteCacheExpiration = TimeSpan.FromMinutes(5);
TimeSpan slidingCacheExpiration = TimeSpan.MinValue;
bool includeRootNode = true;
bool useNestedDynamicNodeRecursion = false;
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

var currentAssembly = this.GetType().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)
typeof(SiteMapNodeUrlResolver)
};

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.Export(implementationType).As(interfaceType).AndSingleton(),
new Assembly[] { siteMapProviderAssembly },
allAssemblies,
excludeTypes,
string.Empty);

// Multiple implementations of strategy based extension points (and not decorated with [ExcludeFromAutoRegistrationAttribute]).
CommonConventions.RegisterAllImplementationsOfInterface(
(interfaceType, implementationType) => container.Export(implementationType).As(interfaceType).AndSingleton(),
multipleImplementationTypes,
allAssemblies,
new Type[0],
string.Empty);

// Registration of internal controllers
CommonConventions.RegisterAllImplementationsOfInterface(
(interfaceType, implementationType) => container.Export(implementationType).As(interfaceType),
new Type[] { typeof(IController) },
new Assembly[] { siteMapProviderAssembly },
new Type[0],
string.Empty);

// Visibility Providers
container.Export<SiteMapNodeVisibilityProviderStrategy>()
.As<ISiteMapNodeVisibilityProviderStrategy>()
.WithCtorParam(() => string.Empty).Named("defaultProviderName");

// Pass in the global controllerBuilder reference
container.ExportInstance((s, c) => ControllerBuilder.Current);

container.Export<ControllerTypeResolverFactory>().As<IControllerTypeResolverFactory>();

// Configure attribute security
string attributeModuleKey = typeof(AuthorizeAttributeAclModule).Name;
container.Export<AuthorizeAttributeAclModule>()
.As<IAclModule>()
.WithKey(attributeModuleKey);

// Configure XML security
string xmlModuleKey = typeof(XmlRolesAclModule).Name;
container.Export<XmlRolesAclModule>()
.As<IAclModule>()
.WithKey(xmlModuleKey);

// Combine attribute and xml security
container.Export<CompositeAclModule>()
.As<IAclModule>()
.WithCtorParam<IAclModule[]>().Named("aclModules").LocateWithKey(new[] { attributeModuleKey, xmlModuleKey });

// Configure cache
container.ExportInstance<System.Runtime.Caching.ObjectCache>(
(scope, context) => System.Runtime.Caching.MemoryCache.Default);

container.Export(typeof(RuntimeCacheProvider<>)).As(typeof(ICacheProvider<>));

container.Export<RuntimeFileCacheDependency>()
.As<ICacheDependency>()
.WithKey("cacheDependency1")
.WithCtorParam(() => absoluteFileName);

container.Export<CacheDetails>()
.As<ICacheDetails>()
.WithKey("cacheDetails1")
.WithCtorParam<ICacheDependency>().LocateWithKey("cacheDependency1")
.WithNamedCtorValue(() => absoluteCacheExpiration)
.WithNamedCtorValue(() => slidingCacheExpiration);

// Configure the visitors
container.Export<UrlResolvingSiteMapNodeVisitor>().As<ISiteMapNodeVisitor>();

// Prepare for our node providers
container.Export<FileXmlSource>()
.As<IXmlSource>()
.WithKey("xmlSource1")
.WithCtorParam(() => absoluteFileName);

container.Export<ReservedAttributeNameProvider>()
.As<IReservedAttributeNameProvider>();

// Register the sitemap node providers
container.Export<XmlSiteMapNodeProvider>()
.As<ISiteMapNodeProvider>()
.WithKey("xmlSiteMapNodeProvider1")
.WithCtorParam<IXmlSource>().LocateWithKey("xmlSource1")
.WithNamedCtorValue(() => includeRootNode)
.WithNamedCtorValue(() => useNestedDynamicNodeRecursion);

container.Export<ReflectionSiteMapNodeProvider>()
.As<ISiteMapNodeProvider>()
.WithKey("reflectionSiteMapNodeProvider1")
.WithCtorParam(() => includeAssembliesForScan).Named("includeAssemblies")
.WithCtorParam(() => new string[0]).Named("excludeAssemblies");

container.Export<CompositeSiteMapNodeProvider>()
.As<ISiteMapNodeProvider>()
.WithKey("siteMapNodeProvider1")
.WithCtorParam<ISiteMapNodeProvider[]>().LocateWithKey(new[]
{
"xmlSiteMapNodeProvider1",
"reflectionSiteMapNodeProvider1"
});

// Register the sitemap builders
container.Export<SiteMapBuilder>()
.As<ISiteMapBuilder>()
.WithKey("siteMapBuilder1")
.WithCtorParam<ISiteMapNodeProvider>().Named("siteMapNodeProvider").LocateWithKey("siteMapNodeProvider1");

// Configure the builder setsbuilderSet1
container.Export<SiteMapBuilderSet>()
.As<ISiteMapBuilderSet>()
.WithKey("builderSet1")
.WithCtorParam(() => "default").Named("instanceName")
.WithCtorParam<ISiteMapBuilder>().Named("siteMapBuilder").LocateWithKey("siteMapBuilder1")
.WithCtorParam<ICacheDetails>().Named("cacheDetails").LocateWithKey("cacheDetails1")
.WithNamedCtorValue(() => securityTrimmingEnabled)
.WithNamedCtorValue(() => enableLocalization)
.WithNamedCtorValue(() => visibilityAffectsDescendants)
.WithNamedCtorValue(() => useTitleIfDescriptionNotProvided);

// Configure the builder sets
container.Export<SiteMapBuilderSetStrategy>()
.As<ISiteMapBuilderSetStrategy>()
.WithCtorParam<ISiteMapBuilderSet[]>().Named("siteMapBuilderSets").LocateWithKey(new[] { "builderSet1" });
}
}
}
Loading