Skip to content

Commit

Permalink
fixing support for IExportStrategyProviderAttribute issue #283
Browse files Browse the repository at this point in the history
  • Loading branch information
ipjohnson committed Jul 27, 2021
1 parent 93465b1 commit bcbb46b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Grace.DependencyInjection.Impl;

namespace Grace.DependencyInjection.Attributes.Interfaces
{
Expand All @@ -7,11 +8,13 @@ namespace Grace.DependencyInjection.Attributes.Interfaces
/// </summary>
public interface IExportStrategyProviderAttribute
{
/// <summary>
/// Provide an export strategy for the attributed type
/// </summary>
/// <param name="attributedType"></param>
/// <returns></returns>
ICompiledExportStrategy ProvideStrategy(Type attributedType);
/// <summary>
/// Provide an export strategy for the attributed type
/// </summary>
/// <param name="attributedType"></param>
/// <param name="activationStrategyCreator"></param>
/// <returns></returns>
ICompiledExportStrategy ProvideStrategy(Type attributedType,
IActivationStrategyCreator activationStrategyCreator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ private IEnumerable<ICompiledExportStrategy> CreateExportStrategiesForTypes(Type
{
keyedExports = keyedExports.Add(value);
}

if (attribute is IExportStrategyProviderAttribute providerAttribute)
{
yield return providerAttribute.ProvideStrategy(type, _strategyCreator);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Text;
using Grace.DependencyInjection;
using Grace.DependencyInjection.Attributes.Interfaces;
using Grace.DependencyInjection.Impl;
using Grace.DependencyInjection.Impl.CompiledStrategies;
using Grace.Tests.Classes.Simple;
using Xunit;

namespace Grace.Tests.DependencyInjection.AttributeTests
{
public class ExportStrategyProviderAttributeTest
{
[Fact]
public void ExportMagicAttributeTest()
{
var container = new DependencyInjectionContainer();

container.Configure(
block => block.ExportAssemblyContaining<ExportStrategyProviderAttributeTest>()
.Where(TypesThat.AreInTheSameNamespaceAs<ExportStrategyProviderAttributeTest>())
.ExportAttributedTypes()
);

var depService = container.Locate<IDependentService<SomeWrappedTyped>>();

Assert.NotNull(depService);
}
}

public class MagicExportAttribute : Attribute, IExportStrategyProviderAttribute
{
public ICompiledExportStrategy ProvideStrategy(Type attributedType, IActivationStrategyCreator creator)
{
var newType = typeof(DependentService<>).MakeGenericType(attributedType);
var exportType = typeof(IDependentService<>).MakeGenericType(attributedType);

var strategy = creator.GetCompiledExportStrategy(newType);

strategy.AddExportAs(exportType);

return strategy;
}
}

[MagicExport]
public class SomeWrappedTyped
{

}
}

0 comments on commit bcbb46b

Please sign in to comment.