Skip to content

Commit

Permalink
added: Rebus.DryIoc with DryIoc 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Nov 23, 2015
1 parent 2fd4b8d commit 7b06442
Show file tree
Hide file tree
Showing 20 changed files with 3,831 additions and 31 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -20,4 +20,7 @@ asb_connection_string.txt
sqs_connectionstring.txt
azure_storage_connection_string.txt
.vs
.vs/*
.vs/*
_NCrunch_*
*.user
*.backup
48 changes: 48 additions & 0 deletions Rebus.DryIoc.Tests/DryIocAssumptions.cs
@@ -0,0 +1,48 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Rebus.Handlers;
using Rebus.Transport;

namespace Rebus.DryIoc.Tests
{
[TestFixture]
public class DryIocAssumptions
{
[Test]
public void RegisterWorks()
{
var factory = new DryIocContainerAdapterFactory();

factory.RegisterHandlerType<SomeHandler>();
factory.RegisterHandlerType<AnotherHandler>();

using (var context = new DefaultTransactionContext())
{
const string stringMessage = "bimse";

var handlers = factory.GetActivator().GetHandlers(stringMessage, context).Result.ToList();

Assert.That(handlers.Count, Is.EqualTo(2));
}
}

class SomeHandler : IHandleMessages<string>
{
public Task Handle(string message)
{
throw new NotImplementedException();
}
}

class AnotherHandler : IHandleMessages<string>
{
public Task Handle(string message)
{
throw new NotImplementedException();
}
}

}
}
35 changes: 35 additions & 0 deletions Rebus.DryIoc.Tests/DryIocContainerAdapterFactory.cs
@@ -0,0 +1,35 @@
using DryIoc;
using Rebus.Activation;
using Rebus.Bus;
using Rebus.Handlers;
using Rebus.Tests.Contracts.Activation;

namespace Rebus.DryIoc.Tests
{
public class DryIocContainerAdapterFactory : IContainerAdapterFactory
{
readonly IContainer _container = new Container(rules => rules
.WithoutThrowOnRegisteringDisposableTransient()); // allows to register IDisposable transients

public IHandlerActivator GetActivator()
{
return new DryIocContainerAdapter(_container);
}

public void RegisterHandlerType<THandler>() where THandler : class, IHandleMessages
{
_container.RegisterMany<THandler>(serviceTypeCondition: type =>
type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IHandleMessages<>));
}

public void CleanUp()
{
_container.Dispose();
}

public IBus GetBus()
{
return _container.Resolve<IBus>();
}
}
}
10 changes: 10 additions & 0 deletions Rebus.DryIoc.Tests/DryIocContainerTests.cs
@@ -0,0 +1,10 @@
using NUnit.Framework;
using Rebus.Tests.Contracts.Activation;

namespace Rebus.DryIoc.Tests
{
[TestFixture]
public class DryIocContainerTests : ContainerTests<DryIocContainerAdapterFactory>
{
}
}
10 changes: 10 additions & 0 deletions Rebus.DryIoc.Tests/DryIocRealContainerTests.cs
@@ -0,0 +1,10 @@
using NUnit.Framework;
using Rebus.Tests.Contracts.Activation;

namespace Rebus.DryIoc.Tests
{
[TestFixture]
public class DryIocRealContainerTests : RealContainerTests<DryIocContainerAdapterFactory>
{
}
}
36 changes: 36 additions & 0 deletions Rebus.DryIoc.Tests/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("Rebus.DryIoc.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Rebus.DryIoc.Tests")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[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("ef482365-2098-4f7a-ad5e-9f55a8f7457e")]

// 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")]
82 changes: 82 additions & 0 deletions Rebus.DryIoc.Tests/Rebus.DryIoc.Tests.csproj
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{DA39D60A-DE08-4D48-A19A-2E4F45E9F9EB}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Rebus.DryIoc.Tests</RootNamespace>
<AssemblyName>Rebus.DryIoc.Tests</AssemblyName>
<TargetFrameworkVersion>v4.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="DryIoc, Version=2.0.0.356, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DryIoc.dll.2.0.0\lib\net45\DryIoc.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DryIocAssumptions.cs" />
<Compile Include="DryIocContainerTests.cs" />
<Compile Include="DryIocRealContainerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="DryIocContainerAdapterFactory.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Rebus.DryIoc\Rebus.DryIoc.csproj">
<Project>{887854dc-9ca5-436a-8c2d-d7fe61fb6767}</Project>
<Name>Rebus.DryIoc</Name>
</ProjectReference>
<ProjectReference Include="..\Rebus.Tests\Rebus.Tests.csproj">
<Project>{959c65ab-d21a-4582-bc4f-06d1425ff274}</Project>
<Name>Rebus.Tests</Name>
</ProjectReference>
<ProjectReference Include="..\Rebus\Rebus.csproj">
<Project>{7d7b7b36-6298-4e85-9a0e-1b415c5b9d12}</Project>
<Name>Rebus</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</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>
5 changes: 5 additions & 0 deletions Rebus.DryIoc.Tests/packages.config
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DryIoc.dll" version="2.0.0" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" />
</packages>
72 changes: 72 additions & 0 deletions Rebus.DryIoc/DryIocContainerAdapter.cs
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DryIoc;
using Rebus.Activation;
using Rebus.Bus;
using Rebus.Extensions;
using Rebus.Handlers;
using Rebus.Pipeline;
using Rebus.Transport;

#pragma warning disable 1998

namespace Rebus.DryIoc
{
public class DryIocContainerAdapter : IContainerAdapter
{
readonly IContainer _container;

/// <summary>
/// Constructs the adapter, using the specified container
/// </summary>
public DryIocContainerAdapter(IContainer container)
{
_container = container;
_container.Register(Made.Of(() => GetCurrentMessageContext()));
}

/// <summary>
/// Resolves all handlers for the given <typeparamref name="TMessage"/> message type
/// </summary>
public async Task<IEnumerable<IHandleMessages<TMessage>>> GetHandlers<TMessage>(TMessage message, ITransactionContext transactionContext)
{
var handlerInstances = _container.Resolve<IList<IHandleMessages<TMessage>>>();

transactionContext.OnDisposed(() =>
{
handlerInstances
.OfType<IDisposable>()
.ForEach(disposable =>
{
disposable.Dispose();
});
});

return handlerInstances;
}

/// <summary>
/// Stores the bus instance
/// </summary>
public void SetBus(IBus bus)
{
_container.RegisterInstance(bus);
}

/// <summary>
/// Returns the current message context and ensures it is not null
/// </summary>
/// <returns>IMessageContext</returns>
internal static IMessageContext GetCurrentMessageContext()
{
var currentMessageContext = MessageContext.Current;
if (currentMessageContext == null)
{
throw new InvalidOperationException("Attempted to inject the current message context from MessageContext.Current, but it was null! Did you attempt to resolve IMessageContext from outside of a Rebus message handler?");
}
return currentMessageContext;
}
}
}
36 changes: 36 additions & 0 deletions Rebus.DryIoc/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("Rebus.DryIoc")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Rebus.DryIoc")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[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("8547b969-4b81-481d-8c16-45ce3e4a072e")]

// 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")]

0 comments on commit 7b06442

Please sign in to comment.