コード例 #1
0
        public object ResolveComponent(Service service)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("I am dead~");
            }
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }

            ComponentRegistration componentRegistration = GetComponentRegistration(service);
            ILifetimeScope        lifetimeScope         = componentRegistration.Lifetime.FindLifetimeScope(this);

            return(lifetimeScope.GetCreateShare(componentRegistration));
        }
コード例 #2
0
        public void Register(ComponentRegistration registration)
        {
            #region Please implement the code to pass the test

            /*
             * We have moved the odd method from Container to ComponentRegistry. Please
             * implement the method.
             */
            if (registration == null)
            {
                throw new ArgumentNullException();
            }
            var service = registration.Service;
            serviceInfos[service] = registration;
            #endregion
        }
コード例 #3
0
        public bool TryGetRegistration(Service service, out ComponentRegistration registration)
        {
            #region Please implement the code to pass the test

            /*
             * Please implement the method to get registration from the registered services.
             */
            if (!this.serviceInfos.ContainsKey(service))
            {
                registration = null;
                throw new DependencyResolutionException();
            }
            registration = this.serviceInfos[service];
            return(true);

            #endregion
        }
        public object ResolveComponent(Service service)
        {
            #region Please modifies the following code to pass the test

            /*
             * The lifetime scope will track lifetime for instances created.
             */
            if (service == null) { throw new ArgumentNullException(nameof(service)); }
            if(IsDisposed) throw new ObjectDisposedException("Object has been disposed!");

            ComponentRegistration componentRegistration = GetComponentRegistration(service);
            object resolveComponent = componentRegistration.Activator.Activate(this);
            disposer.AddItemsToDispose(resolveComponent);
            return resolveComponent;

            #endregion
        }
コード例 #5
0
        public object ResolveComponent(Service service)
        {
            #region Please modifies the following code to pass the test

            /*
             * The lifetime scope will track lifetime for instances created.
             */

            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            ComponentRegistration componentRegistration = GetComponentRegistration(service);
            return(componentRegistration.Activator.Activate(this));

            #endregion
        }
コード例 #6
0
        public ComponentRegistration Register(ComponentRegistration registration)
        {
            #region Please implement the code to pass the test

            /*
             * We have moved the odd method from Container to ComponentRegistry. Please
             * implement the method.
             */

            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }
            serviceInfos[registration.Service] = registration;
            return(registration);

            #endregion
        }
コード例 #7
0
        public bool TryGetRegistration(Service service, out ComponentRegistration registration)
        {
            #region Please implement the code to pass the test

            /*
             * Please implement the method to get registration from the registered services.
             */

            if (serviceInfos.ContainsKey(service))
            {
                registration = serviceInfos[service];
                return(true);
            }
            registration = null;
            return(false);

            #endregion
        }
コード例 #8
0
        public Container Build()
        {
            if (hasBeenBuilt)
            {
                throw new InvalidOperationException("The container has been built.");
            }

            var container = new Container();

            foreach (IRegistrationBuilder builder in registrations)
            {
                ComponentRegistration registration = builder.Build();
                container.Register(registration);
            }

            hasBeenBuilt = true;
            return(container);
        }
コード例 #9
0
        public static IRegistrationBuilder RegisterComponent(
            this ContainerBuilder cb,
            ComponentRegistration registration)
        {
            #region Please re-implement the code to pass the test

            /*
             * Since we have create a concrete type ComponentRegistry to manage the registion
             * work, all the registration operation can be considered as an action that add
             * somekind of component registration to the registry.
             *
             * In order to reuse the code, we re-implement the extension method to replace the
             * instance member function.
             */

            throw new NotImplementedException();

            #endregion
        }
コード例 #10
0
        public bool TryGetRegistration(Service service, out ComponentRegistration registration)
        {
            if (serviceInfos.ContainsKey(service))
            {
                registration = serviceInfos[service];
                return(true);
            }

            #region Please implement the source query logic

            /*
             * The following code will go through the source list and find the first source who
             * can match the service. If no-one can match, then query failed. The function will
             * return fasle. If we have found one. Then create a concrete component registration
             * and add it to serviceInfos for speed acceleration.
             */
            throw new NotImplementedException();

            #endregion
        }
コード例 #11
0
        public object GetCreateShare(ComponentRegistration registration)
        {
            /*
             * There is a missing concept in lifetime scope is instance storge. Currently, part of this
             * function is provided by ResolveComponent method. But Resolving is not equivalent with
             * storage. Resolving means that it will not only creat the instance, but store it to
             * correct lifetime scope (may be current one, and may be not) as well. And this is why
             * we extract the method.
             *
             * This method will create, track and cache(if needed) instance in current lifetime scope.
             * Simple enough huh? The Sharing property will help you to determine whether the activated
             * instace be shared.
             */

            #region Please implement this method

            throw new NotImplementedException();

            #endregion
        }
コード例 #12
0
        public static IRegistrationBuilder Register <T>(
            this ContainerBuilder cb,
            Func <IComponentContext, T> func)
        {
            #region Please modify the following code to pass the test

            /*
             * Since we have changed the definition of activator. Please re-implement
             * the register extension method.
             */
            if (func == null)
            {
                throw new ArgumentNullException();
            }
            var typedService = new TypedService(typeof(T));
            var delegatedInstanceActivator = new DelegatedInstanceActivator(t => func(t));
            var componentRegistration      = new ComponentRegistration(typedService, delegatedInstanceActivator);
            return(cb.RegisterComponent(componentRegistration));

            #endregion
        }
コード例 #13
0
        public bool TryGetRegistration(Service service, out ComponentRegistration registration)
        {
            if (serviceInfos.ContainsKey(service))
            {
                registration = serviceInfos[service];
                return(true);
            }

            ComponentRegistration sourceCreatedRegistration = sources
                                                              .Select(s => s.RegistrationFor(service))
                                                              .FirstOrDefault(cr => cr != null);

            if (sourceCreatedRegistration == null)
            {
                registration = null;
                return(false);
            }

            Register(sourceCreatedRegistration);
            registration = sourceCreatedRegistration;
            return(true);
        }
コード例 #14
0
        public object GetCreateShare(ComponentRegistration registration)
        {
            if (registration.Sharing == InstanceSharing.Shared)
            {
                object component;
                if (sharedInstances.TryGetValue(registration.Service, out component))
                {
                    return(component);
                }
            }

            object instance = registration.Activator.Activate(this);

            Disposer.AddItemsToDispose(instance);

            if (registration.Sharing == InstanceSharing.Shared)
            {
                sharedInstances.Add(registration.Service, instance);
            }

            return(instance);
        }
コード例 #15
0
        public object GetCreateShare(ComponentRegistration registration)
        {
            /*
             * There is a missing concept in lifetime scope is instance storge. Currently, part of this
             * function is provided by ResolveComponent method. But Resolving is not equivalent with
             * storage. Resolving means that it will not only creat the instance, but store it to
             * correct lifetime scope (may be current one, and may be not) as well. And this is why
             * we extract the method.
             *
             * This method will create, track and cache(if needed) instance in current lifetime scope.
             * Simple enough huh? The Sharing property will help you to determine whether the activated
             * instace be shared.
             */

            #region Please implement this method
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }
            object result;
            if (registration.Sharing == InstanceSharing.Shared)
            {
                if (!sharedInstances.ContainsKey(registration.Service))
                {
                    sharedInstances[registration.Service] = registration.Activator.Activate(this);
                }

                result = sharedInstances[registration.Service];
            }
            else
            {
                result = registration.Activator.Activate(this);
            }

            Disposer.AddItemsToDispose(result);
            return(result);

            #endregion
        }
コード例 #16
0
        public static IRegistrationBuilder RegisterComponent(
            this ContainerBuilder cb,
            ComponentRegistration registration)
        {
            #region Please re-implement the code to pass the test

            /*
             * Since we have create a concrete type ComponentRegistry to manage the registion
             * work, all the registration operation can be considered as an action that add
             * somekind of component registration to the registry.
             *
             * In order to reuse the code, we re-implement the extension method to replace the
             * instance member function.
             */
            var builder = new RegistrationBuilder {
                Service   = registration.Service,
                Activator = registration.Activator
            };

            cb.RegisterCallback(r => r.Register(builder.Build()));
            return(builder);

            #endregion
        }
コード例 #17
0
 public void Register(ComponentRegistration registration)
 {
     registrations[registration.Service] = registration;
 }
コード例 #18
0
 public object GetCreateShare(ComponentRegistration registration)
 {