Example #1
0
        static void Main(string[] args)
        {
            try
            {
                GCSettings.LatencyMode = GCLatencyMode.Batch;
                GC.TryStartNoGCRegion(150 * 1024 * 1024, false);

                Console.WriteLine("== Memory consumption ==");
                Console.WriteLine();

                for (var i = 1; i <= 90_000; i++)
                {
                    var x = LazyProxyGenerator.CreateLazyProxyFor <IService>(() => new Service());

                    x.Incrementa(); // instance is created here!

                    if (i % 2000 == 0)
                    {
                        Console.WriteLine($"{ i.ToString("0.0,0").PadLeft(10) } >> { (GC.GetTotalMemory(false) / 1024).ToString("0.0,0") } Kb");
                    }
                }
            }
            finally
            {
                Console.ReadLine();

                GC.EndNoGCRegion();
            }
        }
Example #2
0
        public void GenericInterfaceImplementation()
        {
            var proxy = LazyProxyGenerator.CreateLazyProxyFor <IGenericInterface <int> >(() => new GenericInterface <int>(() => 10));

            var atual = proxy.Cache;

            Assert.AreEqual(default, atual);
Example #3
0
        public void DisposeNonLoadedLazy()
        {
            var svc = new Service();

            Assert.AreEqual(false, svc.Disposed);

            var construiuLazy = false;

            IService svcProxy = LazyProxyGenerator.CreateLazyProxyFor <IService>(() =>
            {
                construiuLazy = true;
                return(svc);
            });

            Assert.IsFalse(construiuLazy);

            // dispose não deve ser invocado!
            svcProxy.Dispose();
            Assert.IsFalse(construiuLazy);
            Assert.IsFalse(svc.Disposed);

            // chama um método para criar o lazy
            svcProxy.Do();
            Assert.IsTrue(construiuLazy);
            Assert.IsFalse(svc.Disposed);

            // chama dispose
            svcProxy.Dispose();
            Assert.IsTrue(svc.Disposed);
        }
Example #4
0
        public void GitHubIssue1Test()
        {
            var proxy = LazyProxyGenerator.CreateLazyProxyFor <IUsuarioRepository>(() => new UsuarioRepository());

            var usr = proxy.GetById(Guid.NewGuid());

            Assert.IsNotNull(usr);
        }
Example #5
0
 public void PerformanceTest()
 {
     for (var i = 1; i <= 1_000_000; i++)
     {
         LazyProxyGenerator.CreateLazyProxyFor <IService>(() => new Service());
     }
     Assert.IsTrue(true);
 }
Example #6
0
 public static IUnityContainer RegisterLazy <TFrom, TTo>(
     this IUnityContainer container,
     string name,
     LifetimeManager lifetimeManager,
     params InjectionMember[] injectionMembers)
     where TTo : TFrom
 {
     return(container.RegisterType(
                typeof(TFrom),
                LazyProxyGenerator.GetLazyProxyType <TFrom, TTo>(),
                name, lifetimeManager, injectionMembers));
 }
Example #7
0
 public override void PreBuildUp(IBuilderContext context)
 {
     if (context.Existing == null)
     {
         var policy = context.Policies.Get <ILazyProxyPolicy>(context.OriginalBuildKey);
         if (policy != null)
         {
             var lazyProxyType = LazyProxyGenerator.GetLazyProxyType(policy.ServiceType, policy.ImplementationType);
             context.Existing = context.NewBuildUp(new NamedTypeBuildKey(lazyProxyType, context.OriginalBuildKey.Name));
         }
     }
     base.PreBuildUp(context);
 }
Example #8
0
        public void ThrowCorrectNullReferenceException()
        {
            var svc = LazyProxyGenerator.CreateLazyProxyFor <IService>(() => null);

            svc.Do();
        }
Example #9
0
        public void GeneratedDynamicProxyTest()
        {
            var proxy = LazyProxyGenerator.CreateLazyProxyFor <IService>(() => new Service(0));

            try
            {
                proxy.Increments(); // void method;

                var valor = proxy.GetValue();
                Assert.AreEqual(1, valor);

                // Readonly Property
                Assert.AreEqual("ReadonlyProperty", proxy.ReadonlyProperty);

                // Normal property
                proxy.Key = "Olá";
                Assert.AreEqual("Olá", proxy.Key);

                var result = proxy.IncrementsWith(2, 3, 6, 12);
                Assert.AreEqual(24, result);

                // out parameter
                proxy.GetVAlue(out valor);
                Assert.AreEqual(valor, 24);

                // varios out's e com valor de retorno;
                var boolResult = proxy.GetOneTwoThree(out var one, out var two, out var three);
                Assert.IsTrue(boolResult);
                Assert.IsNotNull(one); Assert.IsNotNull(two); Assert.IsNotNull(three);
                Assert.AreEqual(1, one.GetValue());
                Assert.AreEqual(2, two.GetValue());
                Assert.AreEqual(3, three.GetValue());

                // reference parameter
                IService metade = null; new Service(0);
                proxy.Half(ref metade);
                Assert.AreEqual(12, metade.GetValue());

                // varios ref's e com valor de retorno.
                var      zero = new Service(0);
                IService four = zero, five = zero, siz = zero;
                boolResult = proxy.GetQuatroCincoSeis(ref four, ref five, ref siz);
                Assert.IsTrue(boolResult);
                Assert.IsFalse(ReferenceEquals(four, zero)); Assert.AreEqual(4, four.GetValue());
                Assert.IsFalse(ReferenceEquals(five, zero)); Assert.AreEqual(5, five.GetValue());
                Assert.IsFalse(ReferenceEquals(siz, zero)); Assert.AreEqual(6, siz.GetValue());

                // params
                result = proxy.IncrementParams(1, 2, 3);
                Assert.AreEqual(30, result);

                proxy.WriteonlyProperty = 10;
                Assert.AreEqual(10, proxy.GetValue());

                var objResult = proxy.GetIncrementedObj();
                Assert.AreNotEqual(objResult, proxy);
                Assert.AreEqual(11, objResult.GetValue());
            }
            catch (Exception ex)
            {
                var y = ex;
                throw;
            }
        }