Пример #1
0
 public void RegisterFactory_FactoryInvokesFunc()
 {
     var wasCalled = false;
     Func<IDependencyResolver, object> f = (resolver) => { wasCalled = true; return new object(); };
     var reg = new Registration<object>(f);
     var result = reg.Factory(null);
     wasCalled.Should().BeTrue();
 }
 private static void Register(this ContainerBuilder builder, Registration registration, string name = null)
 {
     if (registration.Instance != null)
     {
         var reg = builder.Register(ctx => registration.Instance).SingleInstance();
         if (name != null)
         {
             reg.Named(name, registration.DependencyType);
         }
         else
         {
             reg.As(registration.DependencyType);
         }
     }
     else if (registration.Type != null)
     {
         var reg = builder.RegisterType(registration.Type);
         if (name != null)
         {
             reg.Named(name, registration.DependencyType);
         }
         else
         {
             reg.As(registration.DependencyType);
         }
     }
     else if (registration.Factory != null)
     {
         var reg = builder.Register(ctx => registration.Factory(new AutofacDependencyResolver(ctx)));
         if (name != null)
         {
             reg.Named(name, registration.DependencyType);
         }
         else
         {
             reg.As(registration.DependencyType);
         }
     }
     else
     {
         var message = "No type or factory found on registration " + registration.GetType().FullName;
         Logger.Error(message);
         throw new InvalidOperationException(message);
     }
 }