private IComponentLifetime PerRequestLifetime()
        {
            var lifetimeScopeTag = new[] { MatchingScopeLifetimeTags.RequestLifetimeScopeTag };
            var life             = new MatchingScopeLifetime(lifetimeScopeTag);

            return(life);
        }
예제 #2
0
        public void MatchesAgainstSingleTaggedScope()
        {
            const string tag           = "Tag";
            var          msl           = new MatchingScopeLifetime(tag);
            var          container     = Factory.CreateEmptyContainer();
            var          lifetimeScope = (ISharingLifetimeScope)container.BeginLifetimeScope(tag);

            Assert.Equal(lifetimeScope, msl.FindScope(lifetimeScope));
        }
예제 #3
0
        public void MatchesAgainstSingleTaggedScope()
        {
            const string tag           = "Tag";
            var          msl           = new MatchingScopeLifetime(tag);
            var          container     = new Container();
            var          lifetimeScope = (ISharingLifetimeScope)container.BeginLifetimeScope(tag);

            Assert.That(msl.FindScope(lifetimeScope), Is.EqualTo(lifetimeScope));
        }
예제 #4
0
        public void WhenNoMatchingScopeIsPresent_TheExceptionMessageIncludesTheTag()
        {
            var          container = Factory.CreateEmptyContainer();
            const string tag       = "abcdefg";
            var          msl       = new MatchingScopeLifetime(tag);
            var          rootScope = (ISharingLifetimeScope)container.Resolve <ILifetimeScope>();

            var ex = Assert.Throws <DependencyResolutionException>(() => msl.FindScope(rootScope));

            Assert.Contains(tag, ex.Message);
        }
예제 #5
0
        public void WhenNoMatchingScopeIsPresent_TheExceptionMessageIncludesTheTags()
        {
            var          container = new Container();
            const string tag1      = "abc";
            const string tag2      = "def";
            var          msl       = new MatchingScopeLifetime(tag1, tag2);
            var          rootScope = (ISharingLifetimeScope)container.Resolve <ILifetimeScope>();

            var ex = Assert.Throws <DependencyResolutionException>(() => msl.FindScope(rootScope));

            Assert.True(ex.Message.Contains(string.Format("{0}, {1}", tag1, tag2)));
        }
예제 #6
0
        public void MatchesAgainstMultipleTaggedScopes()
        {
            const string tag1 = "Tag1";
            const string tag2 = "Tag2";

            var msl       = new MatchingScopeLifetime(tag1, tag2);
            var container = Factory.CreateEmptyContainer();

            var tag1Scope = (ISharingLifetimeScope)container.BeginLifetimeScope(tag1);

            Assert.Equal(tag1Scope, msl.FindScope(tag1Scope));

            var tag2Scope = (ISharingLifetimeScope)container.BeginLifetimeScope(tag2);

            Assert.Equal(tag2Scope, msl.FindScope(tag2Scope));
        }
예제 #7
0
        public void SingletonsFromRegistrationSourceAreWrappedWithLifetimeDecorator()
        {
            var restrictedRootScopeLifetime = new MatchingScopeLifetime(new object());
            var tracker = new ScopeRestrictedRegisteredServicesTracker(restrictedRootScopeLifetime);

            var builder = new ComponentRegistryBuilder(tracker, new Dictionary <string, object>());

            builder.AddRegistrationSource(new ObjectRegistrationSource());

            var typedService = new TypedService(typeof(object));
            var registry     = builder.Build();

            registry.TryGetRegistration(typedService, out IComponentRegistration registration);

            Assert.IsType <ComponentRegistrationLifetimeDecorator>(registration);
        }
예제 #8
0
        public void MatchesAgainstMultipleTaggedScopes()
        {
            const string tag1 = "Tag1";
            const string tag2 = "Tag2";

            var msl       = new MatchingScopeLifetime(tag1, tag2);
            var container = new Container();

            var tag1Scope = (ISharingLifetimeScope)container.BeginLifetimeScope(tag1);

            Assert.That(msl.FindScope(tag1Scope), Is.EqualTo(tag1Scope));

            var tag2Scope = (ISharingLifetimeScope)container.BeginLifetimeScope(tag2);

            Assert.That(msl.FindScope(tag2Scope), Is.EqualTo(tag2Scope));
        }
        public IEnumerable <IComponentRegistration> RegistrationsFor(Service service, Func <Service, IEnumerable <IComponentRegistration> > registrationAccessor)
        {
            var swt = service as IServiceWithType;
            IEnumerable <Type> repositoryInterfaces = new List <Type> {
                typeof(IRepository <>), typeof(IRepository <,>),
                typeof(ICompoundKeyRepository <, ,>), typeof(ICompoundKeyRepository <, , ,>), typeof(ICompoundKeyRepository <>)
            };

            bool isRepositoryFunc(Type repo) => repo.GetTypeInfo().IsGenericType ? repositoryInterfaces.Contains(repo.GetGenericTypeDefinition()) : false;
            bool isCompoundRepositoryFunc(Type repo) => repo.GetTypeInfo().IsGenericType ? typeof(ICompoundKeyRepository <>) == repo.GetGenericTypeDefinition() : repo.GetInterfaces().Where(i => i.GetTypeInfo().IsGenericType).Select(i => i.GetGenericTypeDefinition()).Any(i => typeof(ICompoundKeyRepository <>) == i);

            if (swt == null || !isRepositoryFunc(swt.ServiceType))
            {
                // It's not a request for the base handler type, so skip it.
                return(Enumerable.Empty <IComponentRegistration>());
            }

            IComponentLifetime lifetime;
            var instanceSharing = InstanceSharing.None;

            if (LifetimeScopeTag != null && LifetimeScopeTag.Count() > 0)
            {
                lifetime        = new MatchingScopeLifetime(LifetimeScopeTag);
                instanceSharing = InstanceSharing.Shared;
            }
            else
            {
                lifetime = new CurrentScopeLifetime();
            }

            var registration = new ComponentRegistration(
                Guid.NewGuid(),
                new DelegateActivator(swt.ServiceType, (c, p) =>
            {
                Type modelType = swt.ServiceType.GetGenericArguments().First();
                if (isCompoundRepositoryFunc(swt.ServiceType))
                {
                    return(RepositoryFactory.GetCompoundKeyInstance(modelType, Configuration, RepositoryName));
                }
                else if (isRepositoryFunc(swt.ServiceType))
                {
                    switch (swt.ServiceType.GetGenericArguments().Count())
                    {
                    case 1:
                        return(RepositoryFactory.GetInstance(modelType, Configuration, RepositoryName));

                    case 2:
                        var key1Type = swt.ServiceType.GetGenericArguments()[1];
                        return(RepositoryFactory.GetInstance(modelType, key1Type, Configuration, RepositoryName));

                    case 3:
                        var key1Type2 = swt.ServiceType.GetGenericArguments()[1];
                        var key2Type2 = swt.ServiceType.GetGenericArguments()[2];
                        return(RepositoryFactory.GetInstance(modelType, key1Type2, key2Type2, Configuration, RepositoryName));

                    case 4:
                        var key1Type3 = swt.ServiceType.GetGenericArguments()[1];
                        var key2Type3 = swt.ServiceType.GetGenericArguments()[2];
                        var key3Type3 = swt.ServiceType.GetGenericArguments()[3];
                        return(RepositoryFactory.GetInstance(modelType, key1Type3, key2Type3, key3Type3, Configuration, RepositoryName));

                    default:
                        throw new NotImplementedException("Error resolving repository: " + swt.ServiceType.Name);
                    }
                }
                else
                {
                    throw new NotImplementedException("Error resolving repository: " + swt.ServiceType.Name);
                }
            }),
                lifetime,
                instanceSharing,
                InstanceOwnership.OwnedByLifetimeScope,
                new[] { service },
                new Dictionary <string, object>()
                );


            return(new IComponentRegistration[] { registration });
        }