Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial checkin.

  • Loading branch information...
commit 59d020f5aa8f46cece2ff305f13da64d1e16bfba 0 parents
@lanwin authored
18 .gitignore
@@ -0,0 +1,18 @@
+*~
+*.*~
+*.mdb
+*.pdb
+*.pidb
+*.user
+*.ncb
+*.aps
+*.suo
+*.userprefs
+*.usertasks
+*.orig
+*.cache
+*.ReSharper
+
+bin/
+obj/
+*_ReSharper.*/**
20 Autofac.WindsorAdapter.sln
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Autofac.WindsorAdapter", "Source\Autofac.WindsorAdapter\Autofac.WindsorAdapter.csproj", "{4A3F9B1D-279D-4ED3-8CC4-FAF46BEDF76D}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {4A3F9B1D-279D-4ED3-8CC4-FAF46BEDF76D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {4A3F9B1D-279D-4ED3-8CC4-FAF46BEDF76D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {4A3F9B1D-279D-4ED3-8CC4-FAF46BEDF76D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {4A3F9B1D-279D-4ED3-8CC4-FAF46BEDF76D}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
BIN  Dependencies/Autofac.dll
Binary file not shown
5,273 Dependencies/Autofac.xml
5,273 additions, 0 deletions not shown
BIN  Dependencies/Castle.Core.dll
Binary file not shown
4,937 Dependencies/Castle.Core.xml
4,937 additions, 0 deletions not shown
BIN  Dependencies/Castle.Windsor.dll
Binary file not shown
8,635 Dependencies/Castle.Windsor.xml
8,635 additions, 0 deletions not shown
13 README.rst
@@ -0,0 +1,13 @@
+Autofac.WindsorAdapter
+==============
+This is an adapter which changes or creates a Castle Windsor instance so it redirects ALL registration and resolve requests to Autofac.
+
+I created this project primary to use Autofac in conjunection with Rhino.ServiceBus (Rhine.ESB).
+
+Home: http://github.com/lanwin/Autofac.WindsorAdapter
+
+Current state
+==============
+For now this is a toy project where i try to find out if this could work.
+
+Its able to send and receive messages via Rhino.ESB and inject Autofac dependencies.
65 Source/Autofac.WindsorAdapter/Autofac.WindsorAdapter.csproj
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{4A3F9B1D-279D-4ED3-8CC4-FAF46BEDF76D}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Autofac.WindsorAdapter</RootNamespace>
+ <AssemblyName>Autofac.WindsorAdapter</AssemblyName>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="Autofac">
+ <HintPath>..\Dependencies\Autofac.dll</HintPath>
+ </Reference>
+ <Reference Include="Castle.Core">
+ <HintPath>..\Dependencies\Castle.Core.dll</HintPath>
+ </Reference>
+ <Reference Include="Castle.Windsor">
+ <HintPath>..\Dependencies\Castle.Windsor.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="AutofacHandler.cs" />
+ <Compile Include="AutofacHandlerFactory.cs" />
+ <Compile Include="ComponentModelAdapter.cs" />
+ <Compile Include="WindsorAdapter.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
101 Source/Autofac.WindsorAdapter/AutofacHandler.cs
@@ -0,0 +1,101 @@
+using System;
+using Castle.Core;
+using Castle.MicroKernel;
+using Castle.MicroKernel.Context;
+
+namespace Autofac.WindsorAdapter
+{
+ internal class AutofacHandler : IHandler
+ {
+ private readonly ComponentModel _model;
+ private readonly IContainer _container;
+
+ public AutofacHandler(ComponentModel model, IContainer container)
+ {
+ if(model == null)
+ throw new ArgumentNullException("model");
+ if(container == null)
+ throw new ArgumentNullException("container");
+ _model = model;
+ _container = container;
+ Service = _model.Service;
+ }
+
+ public AutofacHandler(ComponentModel model, IContainer container, Type service)
+ {
+ if(model == null)
+ throw new ArgumentNullException("model");
+ if(container == null)
+ throw new ArgumentNullException("container");
+ _model = model;
+ _container = container;
+ Service = service;
+ }
+
+ public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
+ {
+ throw new NotImplementedException();
+ }
+
+ public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
+ {
+ throw new NotImplementedException();
+ }
+
+ public void Init(IKernel kernel)
+ {
+ throw new NotImplementedException();
+ }
+
+ public object Resolve(CreationContext context)
+ {
+ return _container.Resolve(_model.Service);
+ }
+
+ public object TryResolve(CreationContext context)
+ {
+ object service;
+ _container.TryResolve(_model.Service, out service);
+ return service;
+ }
+
+ public bool Release(object instance)
+ {
+ throw new NotImplementedException();
+ }
+
+ public void AddCustomDependencyValue(object key, object value)
+ {
+ throw new NotImplementedException();
+ }
+
+ public void RemoveCustomDependencyValue(object key)
+ {
+ throw new NotImplementedException();
+ }
+
+ public bool HasCustomParameter(object key)
+ {
+ throw new NotImplementedException();
+ }
+
+ public bool IsBeingResolvedInContext(CreationContext context)
+ {
+ throw new NotImplementedException();
+ }
+
+ public HandlerState CurrentState
+ {
+ get { throw new NotImplementedException(); }
+ }
+
+ public ComponentModel ComponentModel
+ {
+ get { return _model; }
+ }
+
+ public Type Service { get; private set; }
+
+ public event HandlerStateDelegate OnHandlerStateChanged;
+ }
+}
26 Source/Autofac.WindsorAdapter/AutofacHandlerFactory.cs
@@ -0,0 +1,26 @@
+using System;
+using Castle.Core;
+using Castle.MicroKernel;
+
+namespace Autofac.WindsorAdapter
+{
+ internal class AutofacHandlerFactory : IHandlerFactory
+ {
+ private readonly IContainer _container;
+
+ public AutofacHandlerFactory(IContainer container)
+ {
+ _container = container;
+ }
+
+ public IHandler Create(ComponentModel model)
+ {
+ return new AutofacHandler(model, _container);
+ }
+
+ public IHandler CreateForwarding(IHandler target, Type forwardedType)
+ {
+ return new AutofacHandler(target.ComponentModel, _container, forwardedType);
+ }
+ }
+}
59 Source/Autofac.WindsorAdapter/ComponentModelAdapter.cs
@@ -0,0 +1,59 @@
+using System;
+using System.Collections;
+using Castle.Core;
+
+namespace Autofac.WindsorAdapter
+{
+ public class ComponentModelAdapter
+ {
+ private readonly IContainer _container;
+
+ public ComponentModelAdapter(IContainer container)
+ {
+ if(container == null)
+ throw new ArgumentNullException("container");
+ _container = container;
+ }
+
+ public void Register(ComponentModel componentModel, string key)
+ {
+ if(componentModel == null)
+ throw new ArgumentNullException("componentModel");
+ if(key == null)
+ throw new ArgumentNullException("key");
+
+ var builder = new ContainerBuilder();
+
+ var registration = builder.RegisterType(componentModel.Implementation)
+ .As(componentModel.Service)
+ .Named(key, componentModel.Service);
+
+ foreach(DictionaryEntry property in componentModel.CustomDependencies)
+ {
+ var name = Convert.ToString(property.Key);
+ registration.WithParameter(new NamedParameter(name, property.Value));
+ }
+
+ foreach(DictionaryEntry property in componentModel.ExtendedProperties)
+ {
+ var name = Convert.ToString(property.Key);
+ registration.WithProperty(name, property.Value);
+ }
+
+ switch(componentModel.LifestyleType)
+ {
+ case LifestyleType.Transient:
+ registration.InstancePerDependency();
+ break;
+ case LifestyleType.Undefined:
+ case LifestyleType.Singleton:
+ registration.SingleInstance();
+ break;
+ default:
+ throw new ArgumentOutOfRangeException();
+ }
+
+ builder.Update(_container);
+ }
+ }
+}
36 Source/Autofac.WindsorAdapter/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Autofac.WindsorAdapter")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("Autofac.WindsorAdapter")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("32095bb8-350c-402d-ade6-f568fa524bae")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
86 Source/Autofac.WindsorAdapter/WindsorAdapter.cs
@@ -0,0 +1,86 @@
+using System;
+using System.Reflection;
+using Castle.MicroKernel;
+using Castle.Windsor;
+using Castle.Windsor.Configuration;
+using Castle.Windsor.Configuration.Interpreters;
+
+namespace Autofac.WindsorAdapter
+{
+ public class WindsorAdapter
+ {
+ private readonly ComponentModelAdapter _componentModelAdapter;
+ private readonly IContainer _container;
+ private readonly IKernel _kernel;
+
+ public WindsorAdapter(IKernel kernel, IContainer container)
+ {
+ if(kernel == null)
+ throw new ArgumentNullException("kernel");
+ if(container == null)
+ throw new ArgumentNullException("container");
+
+ _container = container;
+ _componentModelAdapter = new ComponentModelAdapter(_container);
+ _kernel = kernel;
+ _kernel.ComponentRegistered += (key, handler) => _componentModelAdapter.Register(handler.ComponentModel, key);
+
+ SetHandlerFactory();
+
+ RegisterKernel();
+ }
+
+ private void RegisterKernel()
+ {
+ var builder = new ContainerBuilder();
+ builder.RegisterInstance(_kernel);
+ builder.Update(_container);
+ }
+
+ private void SetHandlerFactory()
+ {
+ var handlerFactoryField = _kernel.GetType().GetField("handlerFactory", BindingFlags.Instance | BindingFlags.NonPublic);
+
+ if(handlerFactoryField == null)
+ throw new InvalidOperationException("Could not get handlerFactory field");
+
+ handlerFactoryField.SetValue(_kernel, new AutofacHandlerFactory(_container));
+ }
+
+ public static void Adapt(IWindsorContainer windsor, IContainer container)
+ {
+ if(windsor == null)
+ throw new ArgumentNullException("windsor");
+ if(container == null)
+ throw new ArgumentNullException("container");
+
+ new WindsorAdapter(windsor.Kernel, container);
+ }
+
+ public static IWindsorContainer Create(IContainer container)
+ {
+ if(container == null)
+ throw new ArgumentNullException("container");
+
+ var windsor = new WindsorContainer();
+
+ Adapt(windsor, container);
+
+ return windsor;
+ }
+
+ public static IWindsorContainer Create(IContainer container, IConfigurationInterpreter interpreter)
+ {
+ if(container == null)
+ throw new ArgumentNullException("container");
+ if(interpreter == null)
+ throw new ArgumentNullException("interpreter");
+
+ var windsor = new WindsorContainer(interpreter);
+
+ Adapt(windsor, container);
+
+ return windsor;
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.