Example #1
0
        public static ICspProxyFactory Create(X509Certificate2 certificate, bool includePrivateKey)
        {
            ICspProxyFactory         factory;
            RSACryptoServiceProvider rsa;

            if (includePrivateKey && certificate.HasPrivateKey)
            {
                rsa = (RSACryptoServiceProvider)certificate.PrivateKey;
            }
            else
            {
                rsa = (RSACryptoServiceProvider)certificate.PublicKey.Key;
            }

            // Export will fail if we attempt to export private when there is none
            // Export also fails if key is not exportable
            if (rsa.PublicOnly)
            {
                factory = new DefaultFactory(rsa.ExportCspBlob(false), rsa.KeySize);
                rsa.Dispose();
            }
            else if (rsa.CspKeyContainerInfo.Exportable)
            {
                factory = new DefaultFactory(rsa.ExportCspBlob(true), rsa.KeySize);
                rsa.Dispose();
            }
            else
            {
                factory = new CachingFactory(rsa);
            }

            return(factory);
        }
        public static void ShouldBeSet <TCacheKey, TInstance>(this CachingFactory factory, TCacheKey cacheKey, TInstance instance)
            where TInstance : class
            where TCacheKey : ICacheKey
        {
            var provider = ((CachingInit)factory.TryGetValue("init")).Provider;

            Mock.Get(provider).Verify(r => r.Set(Pleasure.MockIt.Is <TCacheKey>(key => key.GetName().ShouldEqual(cacheKey.GetName())), Pleasure.MockIt.IsStrong(instance)));
        }
        public static void ShouldBeGet <TInstance, TCacheKey>(this CachingFactory factory, TCacheKey cacheKey)
            where TCacheKey : ICacheKey
            where TInstance : class, new()
        {
            var provider = ((CachingInit)factory.TryGetValue("init")).Provider;

            Mock.Get(provider).Verify(r => r.Get <TInstance>(Pleasure.MockIt.Is <string>(key => key.ShouldEqual(cacheKey.GetName()))));
        }
        internal EfPropertyUtils(EfMetaUtils metaUtils)
        {
            _metaUtils = metaUtils;

            _navigationPropertiesFac = new CachingFactory<Type, PropertyInfo[]>(GetNavigationPropertiesIntern);
            _dataPropertiesFac = new CachingFactory<Type, PropertyInfo[]>(GetDataPropertiesIntern);
            _writablePropertiesFac = new CachingFactory<Type, PropertyInfo[]>(GetWritablePropertiesIntern);
            _keyPropertiesFac = new CachingFactory<Type, PropertyInfo[]>(GetKeyPropertiesIntern);
        }
Example #5
0
        public void ZeroHash()
        {
            var cache = new CachingFactory<CacheKey, CacheValue>(512,
                k => new CacheValue(k.Value + 1),
                k => k.Value,
                (k, v) => k.Value == v.Value);

            var key = new CacheKey(0);
            Assert.Equal(CacheKey.GetHashCode(key), 0);

            CacheValue value;
            bool found = cache.TryGetValue(key, out value);
            Assert.False(found);
        }
        public void ZeroHash()
        {
            var cache = new CachingFactory <CacheKey, CacheValue>(512,
                                                                  k => new CacheValue(k.Value + 1),
                                                                  k => k.Value,
                                                                  (k, v) => k.Value == v.Value);

            var key = new CacheKey(0);

            Assert.Equal(CacheKey.GetHashCode(key), 0);

            CacheValue value;
            bool       found = cache.TryGetValue(key, out value);

            Assert.False(found);
        }
        public void result_is_created_only_once()
        {
            Foo.InstantiationCount = 0;
            CachingFactory<int, Foo> fooFactory = new CachingFactory<int, Foo>(
                (num) => new Foo(num)
            );

            var foo1 = fooFactory[1];
            for (int i = 0; i < 10; i++)
                foo1 = fooFactory[1];
            Assert.AreEqual(Foo.InstantiationCount, 1);

            var foo2 = fooFactory[2];
            for (int i = 0; i < 10; i++)
                foo1 = fooFactory[2];
            Assert.AreEqual(Foo.InstantiationCount, 2);

            fooFactory.Evict(1);
            foo1 = fooFactory[1];
            Assert.AreEqual(Foo.InstantiationCount, 3);
        }
        public void two_arg_factory_result_is_created_only_once()
        {
            Foo.InstantiationCount = 0;
            CachingFactory<int, int, Foo> fooFactory = new CachingFactory<int, int, Foo>(
                (num1, num2) => new Foo(num1)
            );

            var foo1 = fooFactory.Get(1, 2);
            for (int i = 0; i < 10; i++)
                foo1 = fooFactory.Get(1,2);
            Assert.AreEqual(Foo.InstantiationCount, 1);

            var foo2 = fooFactory.Get(3, 4);
            for (int i = 0; i < 10; i++)
                foo1 = fooFactory.Get(3, 4);
            Assert.AreEqual(Foo.InstantiationCount, 2);

            fooFactory.Evict(1, 2);
            foo1 = fooFactory.Get(1, 2);
            Assert.AreEqual(Foo.InstantiationCount, 3);
        }
Example #9
0
        public static CachingFactory GetCurrentDomainCachingFactory()
        {
            var dataName = typeof(CachingFactory).FullName;
            var data     = AppDomain.CurrentDomain.GetData(dataName);

            var cachingFactory = data as CachingFactory;

            if (cachingFactory != null)
            {
                return(cachingFactory);
            }

            var disposable = data as IDisposable;

            if (disposable != null)
            {
                disposable.Dispose();
            }

            cachingFactory = new CachingFactory();
            AppDomain.CurrentDomain.SetData(dataName, cachingFactory);
            return(cachingFactory);
        }
 public static void Stub(this CachingFactory factory, Action <Mock <ICachedProvider> > action)
 {
     factory.Initialize(init => StubInit(init, action));
 }
        public static void ShouldBeDelete <TCacheKey>(this CachingFactory factory, TCacheKey cacheKey) where TCacheKey : ICacheKey
        {
            var provider = ((CachingInit)factory.TryGetValue("init")).Provider;

            Mock.Get(provider).Verify(r => r.Delete(Pleasure.MockIt.Is <TCacheKey>(key => key.GetName().ShouldEqual(cacheKey.GetName()))));
        }
        public static ICspProxyFactory Create(X509Certificate2 certificate, bool includePrivateKey)
        {
            ICspProxyFactory factory;
            RSACryptoServiceProvider rsa;

            if (includePrivateKey && certificate.HasPrivateKey)
                rsa = (RSACryptoServiceProvider)certificate.PrivateKey;
            else
                rsa = (RSACryptoServiceProvider)certificate.PublicKey.Key;

            // Export will fail if we attempt to export private when there is none
            // Export also fails if key is not exportable
            if (rsa.PublicOnly)
            {
                factory = new DefaultFactory(rsa.ExportCspBlob(false), rsa.KeySize);
                rsa.Dispose();
            }
            else if (rsa.CspKeyContainerInfo.Exportable)
            {
                factory = new DefaultFactory(rsa.ExportCspBlob(true), rsa.KeySize);
                rsa.Dispose();
            }
            else
            {
                factory = new CachingFactory(rsa);
            }

            return factory;
        }
Example #13
0
 public SharedDictionaryManager() : this(CachingFactory.GetInstanceForDomain())
 {
 }
 public SharedDictionaryManager(CachingFactory factory)
 {
     this.factory = factory;
 }
 public static void Stub(this CachingFactory factory)
 {
     factory.Initialize(init => StubInit(init, mock => { }));
 }
 public static void StubGet <TCacheKey, TInstance>(this CachingFactory factory, TCacheKey cacheKey, TInstance instance)
     where TInstance : class
     where TCacheKey : ICacheKey
 {
     factory.Stub(mock => mock.Setup(r => r.Get <TInstance>(Pleasure.MockIt.Is <TCacheKey>(key => key.GetName().ShouldEqual(cacheKey.GetName())))).Returns(instance));
 }
Example #17
0
        public SharedDictionaryManager(CachingFactory factory)
        {
            Guard.ArgumentNotNull(factory, nameof(factory));

            this.factory = factory;
        }