Пример #1
0
        public MethodMapper(ITypeMapCollection typeMap)
        {
            Func <MethodInfo, bool> methodPredicate = methodInfo => !methodInfo.IsSpecialName;

            var mapped = typeMap.Select(map => new {
                ContractType       = map.ServiceType,
                ImplementationType = map.ConcreteType,
                ContractMethods    = map.ServiceType.GetPublicMethods().Where(methodPredicate),
                MethodsImpl        = map.ConcreteType.GetPublicMethods().ToSet(methodPredicate),
            });

            var mappedMethodsEnumerable = mapped.SelectMany(map => {
                var methods = map.ContractMethods;

                return(methods.Select(method => {
                    var match = method.SelectFirst(map.MethodsImpl,
                                                   (c, impl) => c.IsMatchedTo(impl),
                                                   (c, impl) => new {
                        MethodImpl = impl,
                        ContractMethod = c
                    });

                    return new MethodMap(map.ContractType,
                                         map.ImplementationType,
                                         match.ContractMethod,
                                         match.MethodImpl);
                }));
            });

            mappedMethods = mappedMethodsEnumerable.ToListOf <IMethodMap>();
        }
Пример #2
0
        public EventMapper(ITypeMapCollection typeMap)
        {
            Func <EventInfo, bool> eventPredicate = eventdInfo => !eventdInfo.IsSpecialName;

            var mapped = typeMap.Select(map => new {
                ContractType       = map.ServiceType,
                ImplementationType = map.ConcreteType,
                ContractEvents     = map.ServiceType.GetPublicEvents().Where(eventPredicate),
                EventsImpl         = map.ConcreteType.GetPublicEvents().ToSet(eventPredicate),
            });

            var mappedEventsEnumerable = mapped.SelectMany(map => {
                var events = map.ContractEvents;

                return(events.Select(@event => {
                    var match = @event.SelectFirst(map.EventsImpl,
                                                   (c, impl) => c.GetAddMethod().IsMatchedTo(impl.GetAddMethod()),
                                                   (c, impl) => new {
                        EventImpl = impl,
                        ContractEvent = c
                    });

                    return new EventMap(map.ContractType,
                                        map.ImplementationType,
                                        match.ContractEvent,
                                        match.EventImpl);
                }));
            });

            mappedEvents = mappedEventsEnumerable.ToListOf <IEventMap>();
        }
Пример #3
0
        public PropertyMapper(ITypeMapCollection mixinsMap)
        {
            var mapped = mixinsMap.Select(mixin => new {
                ServiceType = mixin.ServiceType,
                ConcreteType = mixin.ConcreteType,
                ContractProperties = mixin.ServiceType.GetProperties(),
                PropertiesImpl = mixin.ConcreteType.GetProperties().ToSet()
            });

            var mappedPropertiesEnumerable = mapped.SelectMany(map => {
                var properties = map.ContractProperties;

                return properties.Select(property => {
                    var match = property.SelectFirst(map.PropertiesImpl,
                                                     (c, impl) => c.IsMatchedTo(impl),
                                                     (c, impl) => new {
                                                         ServiceProperty = c,
                                                         ConcreteProperty = impl
                                                     });

                    return new PropertyMap(map.ServiceType,
                                           map.ConcreteType,
                                           match.ServiceProperty,
                                           match.ConcreteProperty);
                });
            });

            mappedProperties = mappedPropertiesEnumerable.ToListOf<IPropertyMap>();
        }
Пример #4
0
        public PropertyMapper(ITypeMapCollection mixinsMap)
        {
            var mapped = mixinsMap.Select(mixin => new {
                ServiceType        = mixin.ServiceType,
                ConcreteType       = mixin.ConcreteType,
                ContractProperties = mixin.ServiceType.GetProperties(),
                PropertiesImpl     = mixin.ConcreteType.GetProperties().ToSet()
            });

            var mappedPropertiesEnumerable = mapped.SelectMany(map => {
                var properties = map.ContractProperties;

                return(properties.Select(property => {
                    var match = property.SelectFirst(map.PropertiesImpl,
                                                     (c, impl) => c.IsMatchedTo(impl),
                                                     (c, impl) => new {
                        ServiceProperty = c,
                        ConcreteProperty = impl
                    });

                    return new PropertyMap(map.ServiceType,
                                           map.ConcreteType,
                                           match.ServiceProperty,
                                           match.ConcreteProperty);
                }));
            });

            mappedProperties = mappedPropertiesEnumerable.ToListOf <IPropertyMap>();
        }
Пример #5
0
        public EventMapper(ITypeMapCollection typeMap)
        {
            Func<EventInfo, bool> eventPredicate = eventdInfo => !eventdInfo.IsSpecialName;

            var mapped = typeMap.Select(map => new {
                ContractType = map.ServiceType,
                ImplementationType = map.ConcreteType,
                ContractEvents = map.ServiceType.GetPublicEvents().Where(eventPredicate),
                EventsImpl = map.ConcreteType.GetPublicEvents().ToSet(eventPredicate),
            });

            var mappedEventsEnumerable = mapped.SelectMany(map => {
                var events = map.ContractEvents;

                return events.Select(@event => {
                    var match = @event.SelectFirst(map.EventsImpl,
                                                  (c, impl) => c.GetAddMethod().IsMatchedTo(impl.GetAddMethod()),
                                                  (c, impl) => new {
                                                      EventImpl = impl,
                                                      ContractEvent = c
                                                  });

                    return new EventMap(map.ContractType,
                                        map.ImplementationType,
                                        match.ContractEvent,
                                        match.EventImpl);
                });
            });

            mappedEvents = mappedEventsEnumerable.ToListOf<IEventMap>();
        }
Пример #6
0
        public MethodMapper(ITypeMapCollection typeMap)
        {
            Func<MethodInfo, bool> methodPredicate = methodInfo => !methodInfo.IsSpecialName;

            var mapped = typeMap.Select(map => new {
                ContractType = map.ServiceType,
                ImplementationType = map.ConcreteType,
                ContractMethods = map.ServiceType.GetPublicMethods().Where(methodPredicate),
                MethodsImpl = map.ConcreteType.GetPublicMethods().ToSet(methodPredicate),
            });

            var mappedMethodsEnumerable = mapped.SelectMany(map => {
                var methods = map.ContractMethods;

                return methods.Select(method => {
                    var match = method.SelectFirst(map.MethodsImpl,
                                                  (c, impl) => c.IsMatchedTo(impl),
                                                  (c, impl) => new {
                                                      MethodImpl = impl,
                                                      ContractMethod = c
                                                  });

                    return new MethodMap(map.ContractType,
                                         map.ImplementationType,
                                         match.ContractMethod,
                                         match.MethodImpl);
                });
            });

            mappedMethods = mappedMethodsEnumerable.ToListOf<IMethodMap>();
        }
Пример #7
0
        private Func <IContext, object> BuildExpression(TypeMap typeMap, ITypeMapCollection dependencies)
        {
            var contextParameter  = Expression.Parameter(typeof(IContext), "context");
            var @params           = dependencies.ToArray(d => d.ServiceType);
            var ctorInfo          = typeMap.ConcreteType.GetConstructor(@params);
            var genericMethodInfo = typeof(IContext).GetMethods().First(method => {
                return(method.Name.Equals("GetInstance") &&
                       method.IsGenericMethodDefinition &&
                       method.GetParameters().Length == 1);
            });

            var getInstanceCallExpressions = dependencies.Select(dependency => {
                var nameParam  = Expression.Constant(dependency.Name, typeof(string));
                var methodInfo = genericMethodInfo.MakeGenericMethod(new[] { dependency.ServiceType });

                return(Expression.Call(contextParameter, methodInfo, new[] { nameParam }));
            });

            var lambda = Expression.Lambda <Func <IContext, object> >(
                Expression.New(ctorInfo, getInstanceCallExpressions),
                contextParameter);

            return(lambda.Compile());
        }