Commits

Anonymous committed 6f15316

added metadata helper and initial multi-attribute test

Comments (0)

Files changed (5)

contrib/Source/AutofacContrib.Attributed/AutofacContrib.Attributed.csproj

       <Link>Properties\VersionAssemblyInfo.cs</Link>
     </Compile>
     <Compile Include="AutofacAttributeExtensions.cs" />
+    <Compile Include="MetadataHelper.cs" />
     <Compile Include="MetadataModule.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

contrib/Source/AutofacContrib.Attributed/MetadataHelper.cs

+using System;
+using System.Collections.Generic;
+using System.ComponentModel.Composition;
+using System.Linq;
+
+namespace AutofacContrib.Attributed
+{
+    public class MetadataHelper
+    {
+        private static IEnumerable<KeyValuePair<string, object>> GetProperties(object target)
+        {
+            return target.GetType()
+                         .GetProperties()
+                         .Where(propertyInfo => propertyInfo.CanRead &&
+                                 propertyInfo.DeclaringType.Name != typeof(Attribute).Name)
+                         .Select(propertyInfo =>
+                                  new KeyValuePair<string, object>(propertyInfo.Name, propertyInfo.GetValue(target, null)));
+        }
+
+
+        public static IEnumerable<KeyValuePair<string, object>> GetMetadata(Type targetType)
+        {
+            var propertyList = new List<KeyValuePair<string, object>>();
+
+            foreach (var attribute in targetType.GetCustomAttributes(true)
+                                                .Where(p => p.GetType().GetCustomAttributes(typeof(MetadataAttributeAttribute), false).Count() > 0))
+                propertyList.AddRange(GetProperties(attribute));
+
+            return propertyList;
+        }
+    }
+}

contrib/Source/AutofacContrib.Attributed/MetadataModule.cs

     {
         IRegistrationBuilder<TInstance, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterType
             <TInstance>(TMetadata metadata) where TInstance : TInterface;
+
+        IRegistrationBuilder<TInstance, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterAttributedType
+            <TInstance>(TMetadata metadata) where TInstance : TInterface;
     }
 
+   
+
 
     /// <summary>
     /// provides a mechanism to separate metadata registrations from compile-time attributes
             Register(this);
         }
 
-        #region IMetadataRegistrar<TInterface,TMetadata> Members
-
-
-
-        public IRegistrationBuilder<TInstance, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterType<TInstance>(TMetadata metadata) where TInstance : TInterface
-        {
-            return _containerBuilder.RegisterType<TInstance>().As<TInterface>().WithMetadata(TranslateMetadata(metadata));
-        }
-
         private static IEnumerable<KeyValuePair<string, object>> TranslateMetadata(object instance)
         {
             return instance.GetType().GetProperties().Where(propertyInfo => propertyInfo.CanRead)
                                             (propertyInfo.Name, propertyInfo.GetValue(instance, null))).ToDictionary(pair => pair.Key, pair => pair.Value);
         }
 
+
+        #region IMetadataRegistrar<TInterface,TMetadata> Members
+
+
+
+        public IRegistrationBuilder<TInstance, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterType<TInstance>(TMetadata metadata) where TInstance : TInterface
+        {
+            return _containerBuilder.RegisterType<TInstance>().As<TInterface>().WithMetadata(TranslateMetadata(metadata));
+        }
+
+
+
+        public IRegistrationBuilder<TInstance, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterAttributedType<TInstance>(TMetadata metadata) where TInstance : TInterface
+        {
+            throw new System.NotImplementedException();
+        }
+
         #endregion
     }
 }

contrib/Test/AutofacContrib.Tests.Attributed/AutofacContrib.Tests.Attributed.csproj

   </ItemGroup>
   <ItemGroup>
     <Compile Include="CombinationalWeakTypedAttributeScenarioTestFixture.cs" />
+    <Compile Include="MetadataHelperTestFixture.cs" />
     <Compile Include="MetadataModuleTestFixture.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="ScenarioTypes\CombinationalWeakTypedAttributeScenario.cs" />

contrib/Test/AutofacContrib.Tests.Attributed/MetadataHelperTestFixture.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using AutofacContrib.Attributed;
+using AutofacContrib.Tests.Attributed.ScenarioTypes;
+using NUnit.Framework;
+
+namespace AutofacContrib.Tests.Attributed
+{
+    [TestFixture]
+    public class MetadataHelperTestFixture
+    {
+        [Test]
+        public void scan_multiple_properties_into_one_enumerable_set()
+        {
+            var metadata = MetadataHelper.GetMetadata(typeof (CombinationalWeakTypedScenario));
+
+            Assert.That(metadata.Where(p => p.Key == "Name").FirstOrDefault().Value, Is.EqualTo("Hello"));
+            Assert.That(metadata.Where(p => p.Key == "Age").FirstOrDefault().Value, Is.EqualTo(42));
+        }
+    }
+}