private bool ConstructorCanBeCalled(ConstructorInfo cons, ParameterInfo[] methodArgs, IDivineInjector injector)
 {
     var constructorParams = cons.GetParameters()
         .Where(param => !injector.IsBound(param.ParameterType))
         .ToArray();
     return ConstructorHasAllMethodArgs(methodArgs, constructorParams);
 }
 public FactoryClass Create(Type factoryInterface, IDivineInjector injector, Type domainObjectType, ConstructorArgList constructorArgList)
 {
     var methods = factoryInterface.GetMethods()
         .Select(m => m_methodFactory.Create(m, injector, domainObjectType))
         .ToList();
     return new FactoryClass(constructorArgList, methods);
 }
 public FactoryClassEmitter(IDivineInjector injector, Type interfaceType, Type domainObjectType)
 {
     m_interfaceType = interfaceType;
     m_injector = injector;
     m_domainObjectType = domainObjectType;
     m_tb = GetTypeBuilder(interfaceType);
     m_factoryClassFactory = new FactoryClassFactory(new FactoryMethodFactory());
 }
        public IFactoryMethod Create(MethodInfo method, IDivineInjector injector, Type domainObjectType)
        {
            var methodArgs = method.GetParameters();
            var constructors = domainObjectType.GetConstructors()
                .Where(cons => ConstructorCanBeCalled(cons, methodArgs, injector))
                .ToList();
            if (constructors.Count() > 1)
                throw new Exception("Multiple callable constructors found in target type " + domainObjectType);
            var constructor = constructors.SingleOrDefault();
            if (constructor == null)
                throw new Exception(
                    string.Format(
                        "Could not find constructor on {0} for factory method {1}.{2}",
                        domainObjectType.Name,
                        method.DeclaringType.Name,
                        method.Name));

            var consArgs = constructor.GetParameters()
                .Select(param => ToConstructorArg(method, param, injector))
                .ToList();

            return new FactoryMethod(constructor, method.Name, method.ReturnType, domainObjectType, methodArgs.Select(a => a.ParameterType).ToArray(), consArgs);
        }
 private IConstructorArgDefinition ToConstructorArg(MethodInfo method, ParameterInfo param, IDivineInjector injector)
 {
     if (injector.IsBound(param.ParameterType))
         return new InjectableConstructorArgDefinition(param.ParameterType, GetPropertyName(param.Name));
     return new PassedConstructorArgDefinition(param.ParameterType, MethodArgIndex(param, method));
 }