Пример #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);
 }