示例#1
0
        protected override object CreateContainer(Type t)
        {
            var setup = new RegistrationSetup();

            setup.Register(t);
            return(setup.Construct(GetType().Assembly));
        }
 public WhenCompilationFails(ITestOutputHelper output)
 {
     _output = output;
     _setup  =
         new RegistrationSetup()
         .Register <CompilationErrorTestClass1>();
 }
示例#3
0
 public WhenAFactoryAndASingleAreRegisteredForTheSameImplementation()
 {
     _setup =
         new RegistrationSetup()
         .RegisterSingleton <IInterface1, ConcreteClassImplementing2Interfaces>()
         .RegisterFactory <IInterface2, ConcreteClassImplementing2Interfaces>(
             () => new ConcreteClassImplementing2Interfaces());
 }
示例#4
0
 public WhenASingletonAndAnInjectedSingleAreRegisteredForTheSameImplementation()
 {
     _setup =
         new RegistrationSetup()
         .RegisterSingleton <IInterface2, ConcreteClassImplementing2Interfaces>()
         .RegisterFixed <IInterface1, ConcreteClassImplementing2Interfaces>(
             new ConcreteClassImplementing2Interfaces());
 }
示例#5
0
        public WhenExtendingUsingInvalidRegistrationVisitorsWithoutAContext()
        {
            var setup = new RegistrationSetup()
                        .RegisterFixed(Guid.NewGuid().ToString());

            TestAction = () => setup.Compose(
                typeof(VisitorWithoutAPublicConstructor),
                typeof(VisitorWithMultiplePublicConstructors),
                typeof(VisitorWithInvalidConstructorArguments));
        }
示例#6
0
        public override void PrepareBasic()
        {
            var setup = new RegistrationSetup();

            RegisterDummies(setup);
            RegisterStandard(setup);
            RegisterComplex(setup);

            _compilationContext = setup.Construct(GetType().GetTypeInfo().Assembly);
        }
示例#7
0
 // This method gets called by the runtime. Use this method to add services to the container.
 // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
 public void ConfigureServices(IServiceCollection services)
 {
     RegistrationSetup.RegisterDependencies(services);
     services.AddCors();
     services.AddMvc(options =>
     {
         options.ReturnHttpNotAcceptable = true;
         options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
     });
 }
        public WhenFactoringAWeaklyTypedClassWithAContext(ITestOutputHelper output)
        {
            Container =
                new RegistrationSetup <Guid>()
                .RegisterFactory(typeof(ClassCreatedWithAContext), ClassCreatedWithAContext.WeakCreate)
                .Register <IClassCreatedWithAContext, ClassCreatedWithAContext>()
                .Construct(GetType().GetTypeInfo().Assembly, out string code);

            output.WriteLine(code);
        }
示例#9
0
        protected RequireConstructionContextTestsBase(
            RegistrationSetup <string> registrationContext,
            ITestOutputHelper output)
        {
            Context =
                registrationContext
                .Register(typeof(DependentService))
                .Construct(GetType().GetTypeInfo().Assembly, out string code);

            output.WriteLine(code);
        }
示例#10
0
 private static void RegisterMultiple(RegistrationSetup setup)
 {
     setup
     .Register <ISimpleAdapter, SimpleAdapterOne>()
     .Register <ISimpleAdapter, SimpleAdapterTwo>()
     .Register <ISimpleAdapter, SimpleAdapterThree>()
     .Register <ISimpleAdapter, SimpleAdapterFour>()
     .Register <ISimpleAdapter, SimpleAdapterFive>()
     .Register <ImportMultiple1>()
     .Register <ImportMultiple2>()
     .Register <ImportMultiple3>();
 }
        public WhenFactoringAStronglyTypedClassWithAContextAsAPropertyDependency(ITestOutputHelper output)
        {
            Container =
                new RegistrationSetup <Guid>()
                .RegisterFactory <IClassCreatedWithAContext, ClassCreatedWithAContext>(
                    ClassCreatedWithAContext.StrongCreate,
                    compose => compose.Internal())
                .Register <PropertyDependentClass>(composer => composer.InjectAllProperties())
                .Construct(GetType().GetTypeInfo().Assembly, out string code);

            output.WriteLine(code);
        }
示例#12
0
 private static void RegisterComplex(RegistrationSetup setup)
 {
     setup
     .RegisterFixed <IFirstService>(new FirstService())
     .RegisterFixed <ISecondService>(new SecondService())
     .RegisterFixed <IThirdService>(new ThirdService())
     .RegisterInternal <ISubObjectOne, SubObjectOne>()
     .RegisterInternal <ISubObjectTwo, SubObjectTwo>()
     .RegisterInternal <ISubObjectThree, SubObjectThree>()
     .Register <IComplex1, Complex1>()
     .Register <IComplex2, Complex2>()
     .Register <IComplex3, Complex3>();
 }
示例#13
0
 private static void RegisterStandard(RegistrationSetup setup)
 {
     setup
     .RegisterFixed <ISingleton1>(new Singleton1())
     .RegisterFixed <ISingleton2>(new Singleton2())
     .RegisterFixed <ISingleton3>(new Singleton3())
     .Register <ITransient1, Transient1>()
     .Register <ITransient2, Transient2>()
     .Register <ITransient3, Transient3>()
     .Register <ICombined1, Combined1>()
     .Register <ICombined2, Combined2>()
     .Register <ICombined3, Combined3>();
 }
        public WhenFactoringAWeaklyTypedClassWithAContextAsAConstructorDependency(ITestOutputHelper output)
        {
            Container =
                new RegistrationSetup <Guid>()
                .RegisterFactory(
                    typeof(IClassCreatedWithAContext),
                    typeof(ClassCreatedWithAContext),
                    ClassCreatedWithAContext.WeakCreate, compose => compose.Internal())
                .Register <ConstructorDependentClass>()
                .Construct(GetType().GetTypeInfo().Assembly, out string code);

            output.WriteLine(code);
        }
示例#15
0
 private static void RegisterDummies(RegistrationSetup setup)
 {
     setup
     .Register <IDummyOne, DummyOne>()
     .Register <IDummyTwo, DummyTwo>()
     .Register <IDummyThree, DummyThree>()
     .Register <IDummyFour, DummyFour>()
     .Register <IDummyFive, DummyFive>()
     .Register <IDummySix, DummySix>()
     .Register <IDummySeven, DummySeven>()
     .Register <IDummyEight, DummyEight>()
     .Register <IDummyNine, DummyNine>()
     .Register <IDummyTen, DummyTen>();
 }
示例#16
0
        /// <summary>
        /// Compiles the <paramref name="code"/> for the registration <paramref name="setup"/>.
        /// </summary>
        /// <param name="setup">The registration <paramref name="setup"/>.</param>
        /// <param name="code">The source code to compile.</param>
        /// <param name="fieldValues">The values of the fields to be initialized.</param>
        /// <param name="srcAssemblies">The source assemblies for the types top create.</param>
        /// <returns>The <see cref="CompositionContainer"/>.</returns>
        public static AbiocContainer Compile(
            RegistrationSetup setup,
            string code,
            object[] fieldValues,
            params Assembly[] srcAssemblies)
        {
            if (setup == null)
            {
                throw new ArgumentNullException(nameof(setup));
            }
            if (code == null)
            {
                throw new ArgumentNullException(nameof(code));
            }
            if (fieldValues == null)
            {
                throw new ArgumentNullException(nameof(fieldValues));
            }
            if (srcAssemblies == null)
            {
                throw new ArgumentNullException(nameof(srcAssemblies));
            }

            Compilation compilation = Compilations.GetOrAdd(code, GetOrAdd);

            Compilation GetOrAdd(string c)
            {
                Assembly assembly      = CompileCode(c, srcAssemblies);
                Type     containerType = assembly.GetType("Abioc.Generated.Container");
                Func <object[], IContainer> containerFactory = CreateContainerFactory(containerType);

                return(new Compilation(assembly, containerType, containerFactory));
            }

            var        factory   = (Func <object[], IContainer>)compilation.ContainerFactory;
            IContainer container = factory(fieldValues);
            IContainerInitialization initialization = (IContainerInitialization)container;

            Dictionary <Type, Func <object> > createMap = initialization.GetCreateMap();

            // Get all mappings, where there are distinct non internal registrations.
            IEnumerable <(Type type, Func <object>[] compositions)> iocMappings =
                from kvp in setup.Registrations
                let regTypes = kvp.Value.DistinctPublicRegistrationTypes().ToList()
                               where regTypes.Count > 0
                               select(kvp.Key, regTypes.Select(type => createMap[type]).ToArray());

            return(iocMappings.ToDictionary(m => m.type, kvp => kvp.compositions).ToContainer(container));
        }
        /// <summary>
        /// Composes the registration <paramref name="setup"/> into a <see cref="CompositionContainer"/> for code
        /// generation.
        /// </summary>
        /// <param name="setup">The registration <paramref name="setup"/>.</param>
        /// <param name="externalVisitorTypes">
        /// The types of concrete classes that implement <see cref="IRegistrationVisitor"/> from an external assembly.
        /// </param>
        /// <returns>The <see cref="CompositionContainer"/>.</returns>
        public static CompositionContainer Compose(
            this RegistrationSetup setup,
            params Type[] externalVisitorTypes)
        {
            if (setup == null)
            {
                throw new ArgumentNullException(nameof(setup));
            }
            if (externalVisitorTypes == null)
            {
                throw new ArgumentNullException(nameof(externalVisitorTypes));
            }

            return(Compose(setup, (IEnumerable <Type>)externalVisitorTypes));
        }
        /// <summary>
        /// Composes the registration <paramref name="setup"/> into a <see cref="CompositionContainer"/> for code
        /// generation.
        /// </summary>
        /// <typeparam name="TExtra">
        /// The type of the <see cref="ConstructionContext{TExtra}.Extra"/> construction context information.
        /// </typeparam>
        /// <param name="setup">The registration <paramref name="setup"/>.</param>
        /// <param name="externalVisitorTypes">
        /// The types of concrete classes that implement <see cref="IRegistrationVisitor"/> from an external assembly.
        /// </param>
        /// <returns>The <see cref="CompositionContainer"/>.</returns>
        public static CompositionContainer Compose <TExtra>(
            this RegistrationSetup <TExtra> setup,
            IEnumerable <Type> externalVisitorTypes = null)
        {
            if (setup == null)
            {
                throw new ArgumentNullException(nameof(setup));
            }

            IReadOnlyDictionary <Type, IReadOnlyList <IRegistration> > registrations =
                setup.Registrations.ToDictionary(
                    kvp => kvp.Key,
                    kvp => (IReadOnlyList <IRegistration>)kvp.Value.ToArray());

            return(registrations.Compose(typeof(TExtra), typeof(ConstructionContext <TExtra>), externalVisitorTypes));
        }
示例#19
0
        /// <summary>
        /// Constructs an <see cref="AbiocContainer"/> from the registration <paramref name="setup"/>.
        /// </summary>
        /// <param name="setup">The <see cref="RegistrationSetup"/>.</param>
        /// <param name="srcAssembly">The source assembly for the types top create.</param>
        /// <param name="code">The generated source code.</param>
        /// <returns>
        /// A <see cref="AbiocContainer"/> constructed from the registration <paramref name="setup"/>.
        /// </returns>
        public static AbiocContainer Construct(
            this RegistrationSetup setup,
            Assembly srcAssembly,
            out string code)
        {
            if (setup == null)
            {
                throw new ArgumentNullException(nameof(setup));
            }
            if (srcAssembly == null)
            {
                throw new ArgumentNullException(nameof(srcAssembly));
            }

            return(setup.Construct(new[] { srcAssembly }, out code));
        }
示例#20
0
        /// <summary>
        /// Constructs an <see cref="AbiocContainer"/> from the registration <paramref name="setup"/>.
        /// </summary>
        /// <param name="setup">The <see cref="RegistrationSetup"/>.</param>
        /// <param name="srcAssemblies">The source assemblies for the types top create.</param>
        /// <param name="code">The generated source code.</param>
        /// <returns>
        /// A <see cref="AbiocContainer"/> constructed from the registration <paramref name="setup"/>.
        /// </returns>
        public static AbiocContainer Construct(
            this RegistrationSetup setup,
            Assembly[] srcAssemblies,
            out string code)
        {
            if (setup == null)
            {
                throw new ArgumentNullException(nameof(setup));
            }
            if (srcAssemblies == null)
            {
                throw new ArgumentNullException(nameof(srcAssemblies));
            }

            (string generatedCode, object[] fieldValues) = setup.Compose().GenerateCode();
            code = generatedCode;

            AbiocContainer container = CodeCompilation.Compile(setup, code, fieldValues, srcAssemblies);

            return(container);
        }
示例#21
0
        static void Main(string[] args)
        {
            var logger = new LoggerConfiguration().MinimumLevel.Verbose()
                         .WriteTo.Console(outputTemplate: "{Timestamp:dd/MM/yy HH:mm:ss.fff}\t[{Level:u3}]\t{Message}{NewLine}{Exception}")
                         .CreateLogger();

            var loggerWrapper = new SerilogWrapper(logger);

            RegistrationSetup setup = new RegistrationSetup();

            setup.RegisterFixed <DotX.Interfaces.ILogger, SerilogWrapper>(new SerilogWrapper(logger));

            IContainer container =
                setup.Construct(typeof(DotX.Interfaces.ILogger).Assembly,
                                typeof(SerilogWrapper).Assembly);

            Services.Initialize(new AbiocWrapper(container));

            var app = new DotX.Application(new LinuxX11Platform());

            var mainWin = new MyWindow();

            app.Run();
        }
示例#22
0
 public WhenComposingAndARegistrationHasNoVisitor()
 {
     _setup =
         new RegistrationSetup()
         .Register(GetType(), c => c.Replace(new RegistrationWithoutAVisitor()));
 }
示例#23
0
 public WhenExtendingCompositionUsingARegistrationWithInvalidConstructorArguments()
 {
     _setup = new RegistrationSetup()
              .Register(GetType(), c => c.Replace(new TestRegistration()));
 }
示例#24
0
 public WhenExtendingCompositionUsingARegistrationVisitorWithNoPublicConstructor()
 {
     _setup = new RegistrationSetup()
              .Register(GetType(), c => c.Replace(new TestRegistration()));
 }
示例#25
0
 /// <summary>
 /// Constructs an <see cref="AbiocContainer"/> from the registration <paramref name="setup"/>.
 /// </summary>
 /// <param name="setup">The <see cref="RegistrationSetup"/>.</param>
 /// <param name="srcAssemblies">The source assemblies for the types top create.</param>
 /// <returns>
 /// A <see cref="AbiocContainer"/> constructed from the registration <paramref name="setup"/>.
 /// </returns>
 public static AbiocContainer Construct(this RegistrationSetup setup, params Assembly[] srcAssemblies)
 {
     return(setup.Construct(srcAssemblies, out var unused));
 }
 public WhenTryingToCompileUsingAClassWithoutAPublicConstructor()
 {
     _setup = new RegistrationSetup().Register <ClassWithoutAPublicConstructor>();
 }
 public WhenTryingToCompileUsingAClassWithMultiplePublicConstructors()
 {
     _setup = new RegistrationSetup().Register <ClassWithMultiplePublicConstructors>();
 }