Exemplo n.º 1
0
        private IAssembler <TObject, TConfiguration> GetAssembler(TConfiguration objectConfiguration)
        {
            bool exists = false;
            Type type   = objectConfiguration.GetType();
            IAssembler <TObject, TConfiguration> assembler;

            lock (assemblersMappingLock)
            {
                exists = assemblersMapping.TryGetValue(type, out assembler);
            }
            if (exists)
            {
                return(assembler);
            }
            AssemblerAttribute assemblerAttribute = GetAssemblerAttribute(type);

            if (!typeof(IAssembler <TObject, TConfiguration>).IsAssignableFrom(assemblerAttribute.AssemblerType))
            {
                throw new InvalidOperationException(
                          string.Format(
                              Resources.Culture,
                              Resources.ExceptionAssemblerTypeNotCompatible,
                              objectConfiguration.GetType().FullName,
                              typeof(IAssembler <TObject, TConfiguration>),
                              assemblerAttribute.AssemblerType.FullName));
            }
            assembler = (IAssembler <TObject, TConfiguration>)Activator.CreateInstance(assemblerAttribute.AssemblerType);
            lock (assemblersMappingLock)
            {
                assemblersMapping[type] = assembler;
            }
            return(assembler);
        }
Exemplo n.º 2
0
        private AssemblerAttribute GetAssemblerAttribute(Type type)
        {
            AssemblerAttribute assemblerAttribute
                = Attribute.GetCustomAttribute(type, typeof(AssemblerAttribute)) as AssemblerAttribute;

            if (assemblerAttribute == null)
            {
                throw new InvalidOperationException(
                          string.Format(
                              Resources.Culture,
                              Resources.ExceptionAssemblerAttributeNotSet,
                              type.FullName));
            }
            return(assemblerAttribute);
        }
		public void AssemblerAttributeWithCompatibleTypeWorks()
		{
			AssemblerAttribute attribute = new AssemblerAttribute(typeof(MockAssembler));
		}
		public void AssemblerAttributeWithNullTypeThrows()
		{
			AssemblerAttribute attribute = new AssemblerAttribute(null);
		}