Example #1
0
 public void Should_Pass_One()
 {
     var configName = AppDomain.CurrentDomain.BaseDirectory.Split('\\').Last();
     var fp         = new NugetPackageRetriever(new[] { Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                                     NugetPackageLocation, configName) });
     var ha = new InstantiatorFactory <ISomething>(fp);
     var z  =
         ha.Instantiate(new InstantiatorKey("Impromptu.Tests.Something", "1.0.0",
                                            "Impromptu.Tests.Something1"));
     var x  = z.DoSomething();
     var z1 =
         ha.Instantiate(new InstantiatorKey("Impromptu.Tests.Something", "1.0.0",
                                            "Impromptu.Tests.Something2"));
     var x1 = z1.DoSomething();
 }
Example #2
0
        public void Should_Successfully_Instantiate_Multithreaded()
        {
            var configName = AppDomain.CurrentDomain.BaseDirectory.Split('\\').Last();
            var fp         = new NugetPackageRetriever(new[] { Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                                            NugetPackageLocation, configName) });

            var ha    = new InstantiatorFactory <ISomething>(fp);
            var tasks = new List <Task>();

            var start = DateTime.Now;

            for (var i = 0; i < 1000000; i++)
            {
                tasks.Add(Task.Run(() =>
                {
                    var z = ha.Instantiate(new InstantiatorKey("Impromptu.Tests.Something", "1.0.0", "Impromptu.Tests.Something1"));
                    var x = z.DoSomething();
                }));
            }
            Task.WaitAll(tasks.ToArray());

            var elapsed = DateTime.Now.Subtract(start).TotalMilliseconds;

            _output.WriteLine($"Total elapsed {elapsed} ms.");
        }
Example #3
0
        public void Should_Successfully_Instantiate()
        {
            var configName = AppDomain.CurrentDomain.BaseDirectory.Split('\\').Last();
            var fp         = new NugetPackageRetriever(new[] { Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                                            NugetPackageLocation, configName) });
            var ha = new InstantiatorFactory <ISomething>(fp, _basePath);
            {
                // let it jit compile
                var z = ha.Instantiate(new InstantiatorKey("Impromptu.Tests.Something", "1.0.0", "Impromptu.Tests.Something1"));
            }

            var start = DateTime.Now;

            for (var i = 0; i < 1000000; i++)
            {
                var z = ha.Instantiate(new InstantiatorKey("Impromptu.Tests.Something", "1.0.0", "Impromptu.Tests.Something1"));
                var x = z.DoSomething();
            }
            var elapsed = DateTime.Now.Subtract(start).TotalMilliseconds;

            _output.WriteLine($"Total elapsed {elapsed} ms.");
        }
Example #4
0
        public void Should_Fail_No_Ctor()
        {
            var configName = AppDomain.CurrentDomain.BaseDirectory.Split('\\').Last();
            var fp         = new NugetPackageRetriever(new[] { Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                                            NugetPackageLocation, configName) });

            var       ha = new InstantiatorFactory <ISomething>(fp);
            Exception ex = Assert.Throws <InstantiatorException>(() =>
            {
                var z =
                    ha.Instantiate(
                        new InstantiatorKey("Impromptu.Tests.Something", "1.0.0",
                                            "Impromptu.Tests.Something1"), 100);
            }
                                                                 );

            Assert.Equal(ex.GetType(), typeof(InstantiatorException));
        }
Example #5
0
        static void Main(string[] args)
        {
            if (AppDomain.CurrentDomain.IsDefaultAppDomain())
            {
                var appDomainSetup = AppDomain.CurrentDomain.SetupInformation;

                appDomainSetup.DisallowApplicationBaseProbing = true;
                var workerDomain = AppDomain.CreateDomain("Worker Domain", null, appDomainSetup);
                workerDomain.ExecuteAssembly(typeof(Program).Assembly.Location);
            }
            else
            {
                DefaultContext.WireUpResolver();

                var config = AppDomain.CurrentDomain.BaseDirectory.Split('\\').Last(s => !string.IsNullOrWhiteSpace(s));

                const string additorRelativePath    = "..\\..\\..\\Calculator.Extension.Additor\\bin\\";
                const string subtractorRelativePath = "..\\..\\..\\Calculator.Extension.Subtractor\\bin\\";

                var nugetPackageRetriever =
                    new Impromptu.Package.NugetPackageRetriever(new[]
                {
                    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, additorRelativePath, config),
                    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, subtractorRelativePath, config)
                });

                // cleanup
                var additorFolderPath =
                    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                                 "ImpromptuPackages", "Calculator.Extension.Additor.1.0.0");

                var subtractorFolderPath =
                    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                                 "ImpromptuPackages", "Calculator.Extension.Subtractor.1.0.0");

                // cleanup
                if (Directory.Exists(additorFolderPath))
                {
                    Directory.Delete(additorFolderPath, true);
                }

                if (Directory.Exists(subtractorFolderPath))
                {
                    Directory.Delete(subtractorFolderPath, true);
                }

                var factory = new InstantiatorFactory <ICalculator>(nugetPackageRetriever);
                // this line shows that it is referencing shared type from its local directory
                Console.WriteLine(
                    $"Main Program SharedType Runtime Version: {typeof(SharedType).Assembly.ImageRuntimeVersion}. Codebase: {typeof(SharedType).Assembly.CodeBase}.");

                var additionResult =
                    factory.Instantiate(new InstantiatorKey("Calculator.Extension.Additor", "1.0.0", "Calculator.Extension.Additor"))
                    .Calculate(10, 5);
                Console.WriteLine($"Addition Result = {additionResult}");

                var subtractionResult =
                    factory.Instantiate(new InstantiatorKey("Calculator.Extension.Subtractor", "1.0.0", "Calculator.Extension.Subtractor"))
                    .Calculate(10, 5);
                Console.WriteLine($"Subtraction Result = {subtractionResult}");

                Console.ReadKey();
            }
        }