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

Attributed Metadata allowing overriding of metadata creation via an IMetadataProvider interface on metadata attributes #519

Merged
merged 2 commits into from
May 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
</CodeAnalysisDictionary>
<Compile Include="AttributedMetadataModule.cs" />
<Compile Include="AutofacAttributeExtensions.cs" />
<Compile Include="IMetadataProvider.cs" />
<Compile Include="MetadataHelper.cs" />
<Compile Include="MetadataModule.cs" />
<Compile Include="ParameterFilterAttribute.cs" />
Expand All @@ -86,4 +87,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
20 changes: 20 additions & 0 deletions Extras/Source/Autofac.Extras.Attributed/IMetadataProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Autofac.Extras.Attributed
{
/// <summary>
/// Allows for custom generation of metadata
/// </summary>
public interface IMetadataProvider
{
/// <summary>
/// Gets metadata pairs for the passed target type
/// </summary>
/// <param name="targetType">Target type to get metadata for</param>
/// <returns>Metadata dictionary to merge with existing metadata</returns>
IDictionary<string, object> GetMetadata(Type targetType);
}
}
23 changes: 19 additions & 4 deletions Extras/Source/Autofac.Extras.Attributed/MetadataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,31 @@ public static class MetadataHelper
/// Given a target object, returns a set of properties and associated values.
/// </summary>
/// <param name="target">Target instance to be scanned.</param>
/// <param name="instanceType">Type that the target is being scanned for (not the type of <paramref name="target"/>)</param>
/// <returns>Enumerable set of properties and associated values.</returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="target" /> is <see langword="null" />.
/// Thrown if <paramref name="target" /> or <paramref name="instanceType"/> is <see langword="null" />.
/// </exception>
public static IEnumerable<KeyValuePair<string, object>> GetProperties(object target)
public static IEnumerable<KeyValuePair<string, object>> GetProperties(object target, Type instanceType)
{
if (target == null)
{
throw new ArgumentNullException("target");
}

if (instanceType == null)
{
throw new ArgumentNullException("instanceType");
}

var asProvider = target as IMetadataProvider;

if (asProvider != null)
{
//this instance decides its own properties
return asProvider.GetMetadata(instanceType);
}

return target.GetType()
.GetProperties()
.Where(propertyInfo => propertyInfo.CanRead &&
Expand Down Expand Up @@ -52,7 +67,7 @@ public static IEnumerable<KeyValuePair<string, object>> GetMetadata(Type targetT

foreach (var attribute in targetType.GetCustomAttributes(true)
.Where(p => p.GetType().GetCustomAttributes(typeof(MetadataAttributeAttribute), true).Any()))
propertyList.AddRange(GetProperties(attribute));
propertyList.AddRange(GetProperties(attribute, targetType));

return propertyList;
}
Expand All @@ -75,7 +90,7 @@ public static IEnumerable<KeyValuePair<string, object>> GetMetadata<TMetadataTyp
var attribute =
(from p in targetType.GetCustomAttributes(typeof(TMetadataType), true) select p).FirstOrDefault();

return attribute != null ? GetProperties(attribute) : new List<KeyValuePair<string, object>>();
return attribute != null ? GetProperties(attribute, targetType) : new List<KeyValuePair<string, object>>();
}
}
}
4 changes: 2 additions & 2 deletions Extras/Source/Autofac.Extras.Attributed/MetadataModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public IRegistrationBuilder<TInstance, ConcreteReflectionActivatorData, SingleRe
{
return
ContainerBuilder.RegisterType<TInstance>().As<TInterface>().WithMetadata(
MetadataHelper.GetProperties(metadata));
MetadataHelper.GetProperties(metadata, typeof(TInstance)));
}

/// <summary>
Expand All @@ -138,7 +138,7 @@ public IRegistrationBuilder<object, ConcreteReflectionActivatorData, SingleRegis
{
return
ContainerBuilder.RegisterType(instanceType).As<TInterface>().WithMetadata(
MetadataHelper.GetProperties(metadata));
MetadataHelper.GetProperties(metadata, instanceType));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@
<Compile Include="CombinationalWeakTypedAttributeScenarioTestFixture.cs" />
<Compile Include="MetadataHelperTestFixture.cs" />
<Compile Include="MetadataModuleTestFixture.cs" />
<Compile Include="MetadataProviderTestFixture.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScenarioTypes\CombinationalWeakTypedAttributeScenario.cs" />
<Compile Include="ScenarioTypes\MetadataModuleScenarioDiscoveryTargets.cs" />
<Compile Include="ScenarioTypes\MetadataModuleScenarioInterfaces.cs" />
<Compile Include="ScenarioTypes\MetadataProviderScenario.cs" />
<Compile Include="ScenarioTypes\StrongTypedScenarioMetadataModule.cs" />
<Compile Include="ScenarioTypes\StrongTypedMetadataAttributeScenario.cs" />
<Compile Include="ScenarioTypes\TypeOfScenarioMetadataModule.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Autofac.Extras.Attributed;
using Autofac.Extras.Tests.Attributed.ScenarioTypes;
using Autofac.Features.Metadata;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Autofac.Extras.Tests.Attributed
{
[TestFixture]
public class MetadataProviderTestFixture
{
[Test]
public void load_provided_into_weak_metadata()
{
var builder = new ContainerBuilder();
builder.RegisterModule<AttributedMetadataModule>();
builder.RegisterType<MetadataProviderScenario>()
.As<IMetadataProviderScenario>();

var container = builder.Build();
var withMetadata = container.Resolve<Meta<IMetadataProviderScenario>>();

Assert.That(withMetadata, Is.Not.Null);
var value1 = withMetadata.Metadata.Where(kv => kv.Key == "Key1").FirstOrDefault();
var value2 = withMetadata.Metadata.Where(kv => kv.Key == "Key2").FirstOrDefault();

Assert.That(value1, Is.Not.Null);
Assert.That(value2, Is.Not.Null);
Assert.That(value1.Value, Is.EqualTo("Value1"));
Assert.That(value2.Value, Is.EqualTo("Value2"));
}

public void load_provided_into_strong_metadata()
{
var builder = new ContainerBuilder();
builder.RegisterModule<AttributedMetadataModule>();
builder.RegisterType<MetadataProviderScenario>()
.As<IMetadataProviderScenario>();

var container = builder.Build();
var withMetadata = container.Resolve<Meta<IMetadataProviderScenario, ProvidedMetadata>>();

Assert.That(withMetadata, Is.Not.Null);
Assert.That(withMetadata.Metadata, Is.Not.Null);
Assert.That(withMetadata.Metadata.Key1, Is.EqualTo("Value1"));
Assert.That(withMetadata.Metadata.Key2, Is.EqualTo("Value2"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Autofac.Extras.Attributed;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;

namespace Autofac.Extras.Tests.Attributed.ScenarioTypes
{
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class)]
public class ProvidedMetadataAttribute : Attribute, IMetadataProvider
{
public IDictionary<string, object> GetMetadata(Type targetType)
{
return new Dictionary<string, object>()
{
{ "Key1", "Value1" },
{ "Key2", "Value2" }
};
}
}

public class ProvidedMetadata
{
public string Key1 { get; set; }
public string Key2 { get; set; }
}

public interface IMetadataProviderScenario
{
}

[ProvidedMetadata]
public class MetadataProviderScenario : IMetadataProviderScenario { }
}