Skip to content

Other context type support #3

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ build/Output
test-results
.idea
output/
.vs
.vs
packages
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ or compile from sources available on Git: `git clone git://github.com/Suremaker/
* [Dependency check](https://github.com/Suremaker/Spring.FluentContext/wiki/Dependency-checking)
* [Type-safe, literal-less references](https://github.com/Suremaker/Spring.FluentContext/wiki/Object-definition-registration)

## Extenstion methods

Fluent configuration extracted to extension methods, so it can be used with any context implementing `IConfigurableApplicationContext`. Unfortunately, most of them have their core logic in constructors so simply registering objects is not enoguh.
Eg. for proper usage of `XmlApplicationContext` you need to instantiate `FluentXmlApplicationContext` with a configurator class implementing `IContextConfigurator` which is called during building base context. Not a nice solution, but only possible without changing core *Spring.Net* repo.

## Web support

Inside `Spring.FluentContext.Web` project there is a `FluentWebApplicationContext` which can be initialized using `FluentWebContextHandler`. Sample configuration in *web.config*:

```xml
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.FluentContext.Web.FluentWebContextHandler, Spring.FluentContext.Web" />
</sectionGroup>
</configSections>
<appSettings>
<add key="SpringConfigurator" value="WebClient.Mobile.Config.SpringFluentConfig, WebClient.Mobile" />
</appSettings>
</configuration>
```

## Not Implemented Features
* lazy and non-lazy initialization (now everything is initialized lazily)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net461;netcoreapp2.2</TargetFrameworks>
<Copyright>Copyright © 2012-2019, Wojciech Kotlarski</Copyright>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Spring.FluentContext\Spring.FluentContext.csproj" />
</ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net462;net6.0</TargetFrameworks>
<Copyright>Copyright © 2012-2019, Wojciech Kotlarski</Copyright>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Spring.FluentContext\Spring.FluentContext.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.2;net45</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Spring.FluentContext\Spring.FluentContext.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="NUnit" Version="3.7.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.2" />
</ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net462</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Spring.FluentContext\Spring.FluentContext.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
</ItemGroup>
</Project>
79 changes: 79 additions & 0 deletions Spring.FluentContext.Web/FluentWebApplicationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using Spring.Context;
using Spring.Context.Support;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;

namespace Spring.FluentContext.Web
{
public class FluentWebApplicationContext : WebApplicationContext
{
public const string SpringConfiguratorKey = "SpringConfigurator";
private IContextConfigurator _configurator;

#region Constructors

/// <summary>
/// Create a new WebApplicationContext, loading the definitions
/// from the given XML resource.
/// </summary>
/// <param name="configurationLocations">Names of configuration resources.</param>
public FluentWebApplicationContext(params string[] configurationLocations)
: base(new WebApplicationContextArgs(string.Empty, null, configurationLocations, null, false))
{
}

/// <summary>
/// Create a new WebApplicationContext, loading the definitions
/// from the given XML resource.
/// </summary>
/// <param name="name">The application context name.</param>
/// <param name="caseSensitive">Flag specifying whether to make this context case sensitive or not.</param>
/// <param name="configurationLocations">Names of configuration resources.</param>
public FluentWebApplicationContext(string name, bool caseSensitive, params string[] configurationLocations)
: base(new WebApplicationContextArgs(name, null, configurationLocations, null, caseSensitive))
{
}

/// <summary>
/// Create a new WebApplicationContext with the given parent,
/// loading the definitions from the given XML resources.
/// </summary>
/// <param name="name">The application context name.</param>
/// <param name="caseSensitive">Flag specifying whether to make this context case sensitive or not.</param>
/// <param name="parentContext">The parent context.</param>
/// <param name="configurationLocations">Names of configuration resources.</param>
public FluentWebApplicationContext(string name, bool caseSensitive, IApplicationContext parentContext,
params string[] configurationLocations)
: base(new WebApplicationContextArgs(name, parentContext, configurationLocations, null, caseSensitive))
{ }
#endregion

protected override void OnPreRefresh()
{
const string prefix = SpringConfiguratorKey + ProtocolSeparator;

for (int i = 0; i < ConfigurationLocations.Length; i++)
{
if (ConfigurationLocations[i].StartsWith(prefix))
{
string configuratorName = ConfigurationLocations[i].Substring(prefix.Length);
ConfigurationLocations[i] = "assembly://Spring.FluentContext.Web/Spring.FluentContext.Web/emptyContext.xml";
_configurator = (IContextConfigurator) Activator.CreateInstance(Type.GetType(configuratorName));
break; // only one configurator supported
}
}
base.OnPreRefresh();
}

/// <summary>
/// This method registers Fluent objects and calls base post processing
/// </summary>
/// <param name="objectFactory"></param>
protected override void PostProcessObjectFactory(IConfigurableListableObjectFactory objectFactory)
{
_configurator?.LoadObjectDefinitions(this);
base.PostProcessObjectFactory(objectFactory);
}
}
}
49 changes: 49 additions & 0 deletions Spring.FluentContext.Web/FluentWebContextHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Common.Logging;
using Spring.Context;
using Spring.Context.Support;
using Spring.Core.IO;

namespace Spring.FluentContext.Web
{
public class FluentWebContextHandler : WebContextHandler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(WebContextHandler));
private readonly string _configurator;

public FluentWebContextHandler()
{
_configurator = ConfigurationManager.AppSettings["SpringConfigurator"];
}

/// <summary>
/// Sets default context type to <see cref="FluentWebApplicationContext"/>
/// </summary>
protected override Type DefaultApplicationContextType
{
get { return typeof(FluentWebApplicationContext); }
}

/// <summary>
/// Handles web specific details of context instantiation.
/// </summary>
protected override IApplicationContext InstantiateContext(IApplicationContext parent, object configContext,
string contextName, Type contextType, bool caseSensitive, IList<string> resources)
{
string[] resourcesArray = resources.Union(
string.IsNullOrWhiteSpace(_configurator)
? new string[0]
: new[]
{
FluentWebApplicationContext.SpringConfiguratorKey +
ConfigurableResourceLoader.ProtocolSeparator + _configurator
}).ToArray();

return base.InstantiateContext(parent, configContext, contextName, contextType, caseSensitive,
resourcesArray);
}
}
}
33 changes: 33 additions & 0 deletions Spring.FluentContext.Web/Spring.FluentContext.Web.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<Description>Allows creating Spring.NET IoC container from code using fluent API</Description>
<Copyright>Copyright © 2012-2019, Wojciech Kotlarski</Copyright>
<Authors>Wojciech Kotlarski</Authors>
<PackageProjectUrl>https://github.com/Suremaker/Spring.FluentContext</PackageProjectUrl>
<Version>3.0.0</Version>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/Suremaker/Spring.FluentContext</RepositoryUrl>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<IncludeSymbols>True</IncludeSymbols>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageOutputPath>$(SolutionDir)\output</PackageOutputPath>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>true</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Spring.Web" Version="3.0.1" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Configuration" />
<Reference Include="System.Web" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Spring.FluentContext\Spring.FluentContext.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="emptyContext.xml" />
<EmbeddedResource Include="emptyContext.xml" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions Spring.FluentContext.Web/emptyContext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net" />
Loading