コード例 #1
0
        public IRegistrationBuilder Register <T, TAs>()
        {
            var reg = new ShittyRegistration
            {
                Type           = typeof(T),
                ResolutionType = ResolutionType.Reflection
            };

            instanceDependencyMap.Add(typeof(TAs), reg);

            return(new ShittyRegistrationBuilder(reg, this));
        }
コード例 #2
0
        public IRegistrationBuilder Register <T>(Func <T> factory)
            where T : class
        {
            var reg = new ShittyRegistration
            {
                Type           = typeof(T),
                ResolutionType = ResolutionType.Factory,
                Factory        = (Func <object>)factory
            };

            instanceDependencyMap.Add(typeof(T), reg);

            return(new ShittyRegistrationBuilder(reg, this));
        }
コード例 #3
0
        private object Resolve(ShittyRegistration registration)
        {
            if (registration == null)
            {
                return(null);
            }

            if (registration.ResolutionType == ResolutionType.Factory)
            {
                return(registration.Factory());
            }

            if (registration.Type.IsInterface)
            {
                return(null);
            }

            var resolvedType = registration.Type;
            var constructor  = resolvedType
                               .GetConstructors()
                               .OrderBy(x => x.GetParameters().Count())
                               .Where(ctor => !ctor.GetParameters().Any(pt => pt.ParameterType == typeof(string)))
                               .Where(ctor => !ctor.GetParameters().Any(pt => pt.ParameterType == typeof(bool)))
                               .LastOrDefault();

            if (constructor == null)
            {
                throw new EntryPointNotFoundException($"Unable to create an instance of {resolvedType.Name} as we cannot find a single public constructor for it. Make sure you have at least 1 public constructor (we'll pick the one with most arguments though)");
            }

            var parameters = constructor.GetParameters();

            object instance;

            if (!parameters.Any())
            {
                instance = Activator.CreateInstance(resolvedType);
            }
            else
            {
                instance = constructor.Invoke(
                    ResolveParameters(parameters).ToArray()
                    );
            }

            registration.AfterCreation?.Invoke(instance);

            return(instance);
        }
コード例 #4
0
        private object Resolve(ShittyRegistration registration)
        {
            if (registration == null)
            {
                return(null);
            }

            if (registration.ResolutionType == ResolutionType.Factory)
            {
                return(registration.Factory());
            }

            if (registration.Type.IsInterface)
            {
                return(null);
            }

            var resolvedType = registration.Type;
            var constructor  = resolvedType
                               .GetConstructors()
                               .OrderBy(x => x.GetParameters().Count())
                               .Where(ctor => !ctor.GetParameters().Any(pt => pt.ParameterType == typeof(string)))
                               .Where(ctor => !ctor.GetParameters().Any(pt => pt.ParameterType == typeof(bool)))
                               .LastOrDefault();
            var parameters = constructor.GetParameters();

            object instance;

            if (!parameters.Any())
            {
                instance = Activator.CreateInstance(resolvedType);
            }
            else
            {
                instance = constructor.Invoke(
                    ResolveParameters(parameters).ToArray()
                    );
            }

            if (registration.AfterCreation != null)
            {
                registration.AfterCreation(instance);
            }

            return(instance);
        }
コード例 #5
0
        private void RegisterDeliverable(Type deliverable)
        {
            var registration = new ShittyRegistration
            {
                Type           = deliverable,
                ResolutionType = ResolutionType.Reflection
            };

            var name = deliverable.GetCustomAttribute <DeliverableNameAttribute>();

            instanceDependencyMap.Add(name.Name, registration);

            var aliases = deliverable.GetCustomAttributes <DeliverableAliasAttribute>();

            foreach (var alias in aliases)
            {
                instanceDependencyMap.Add(alias.Alias, registration);
            }
        }
コード例 #6
0
 public ShittyRegistrationBuilder(ShittyRegistration registration, ShittyIoC container)
 {
     this.registration = registration;
     this.container    = container;
 }
コード例 #7
0
 internal void Register <T>(ShittyRegistration registration)
 {
     instanceDependencyMap.Add(typeof(T), registration);
 }