private void OnRegistering(object sender, RegisterEventArgs e)
        {
            m_Entries.Add(new TypeRegistrationEntry(e.TypeFrom ?? e.TypeTo, e.Name));

            if (Registering != null)
                Registering(sender, e);
        }
            private void OnRegistering(object sender, RegisterEventArgs e)
            {
                var component = e.TypeTo.GetCustomAttribute<ComponentAttribute>();
                if (component == null)
                    return;

                foreach (var iface in e.TypeTo.GetTypeInfo().ImplementedInterfaces)
                {
                    // Register the named component with the full type name since
                    // Unity does not allow multiple registrations with the same type.
                    this.Context.RegisterNamedType(iface, e.TypeTo.FullName);
                    // Next, we set the policy for building the key for the type without
                    // key and map it to the one we registered above, with type + name.
                    this.Context.Policies.Set<IBuildKeyMappingPolicy>(
                        new BuildKeyMappingPolicy(new NamedTypeBuildKey(e.TypeTo, null)),
                        new NamedTypeBuildKey(iface, e.TypeTo.FullName));

                    var named = e.TypeTo.GetCustomAttribute<NamedAttribute>(true);

                    if (named != null)
                    {
                        // We must also register the key mapping with the custom key.
                        // key and map it to the one we registered above, with type + name.
                        this.Context.Policies.Set<IBuildKeyMappingPolicy>(
                            new BuildKeyMappingPolicy(new NamedTypeBuildKey(e.TypeTo, named.Name)),
                            new NamedTypeBuildKey(iface, e.TypeTo.FullName));
                    }
                }
            }
Ejemplo n.º 3
0
        private void TypeRegistering(object sender, RegisterEventArgs args)
        {
            if (!typeof(IMessageConsumer).IsAssignableFrom(args.TypeTo))
                return;

            args.LifetimeManager = new TransientLifetimeManager();
            interceptor.ItemCreated(args.TypeTo, true);
        }
        private void OnRegistering(object sender, RegisterEventArgs e)
        {
            string returnedName = EnsureAndReturnName(e.Name, e.TypeFrom);

            if (!string.IsNullOrEmpty(returnedName))
            {
                e.Name = returnedName;
            }
        }
Ejemplo n.º 5
0
 void OnRegistering(object sender, RegisterEventArgs e)
 {
    Trace.WriteLine(String.Format("Registering: {0}", e.Name ?? "_Default_"));
    Trace.WriteLine(String.Format("   Type:     {0}", e.TypeTo));
    Trace.WriteLine(String.Format("   MappedTo: {0}", e.TypeFrom));
    if (e.LifetimeManager != null) {
       Trace.WriteLine(String.Format("   Lifetime: {0}", e.LifetimeManager.GetType()));
    }
 }
        private void OnRegister(object sender, RegisterEventArgs e)
        {
            if (ShouldIntercept(e))
            {
                var container = sender as IUnityContainer;

                var i = new Interceptor<InterfaceInterceptor>();
                i.AddPolicies(e.TypeFrom, e.TypeTo, e.Name, Context.Policies);

                _interceptionBehaviors.ForEach(
                    (b) =>
                        {
                            var interfaceBehavior = new InterceptionBehavior(b);
                            interfaceBehavior.AddPolicies(e.TypeFrom, e.TypeTo, e.Name, Context.Policies);
                        });
            }
        }
Ejemplo n.º 7
0
 private void CheckStepScope(object sender, RegisterEventArgs args)
 {
     var isStepScope = args.LifetimeManager is StepScopeLifetimeManager;
     // First we need to check any registered dependency with the same registered type and name
     Container.Registrations.Where(r => r.RegisteredType != (args.TypeFrom ?? args.TypeTo) &&
         r.MappedToType == args.TypeTo && r.Name == args.Name).ForEach(r =>
     {
         if (StepScopeSynchronization.IsStepScope(r.MappedToType, r.Name))
         {
             // if the previously registered dependency is in step scope, a newly defined
             // liftetime manager takes precedence. If no lifetime manager is defined for
             // the new dependency, it is in step scope.
             if (args.LifetimeManager == null)
             {
                 isStepScope = true;
             }
             if (!isStepScope)
             {
                 StepScopeSynchronization.RemoveScopeDependency(r.RegisteredType, r.Name);
                 StepScopeSynchronization.RemoveScopeDependency(r.MappedToType, r.Name);
             }
         }
         else if (isStepScope)
         {
             // if the previously registered dependency is not in step scope but the new dependency
             // is, the previously registered dependency is put in step scope.
             if (r.RegisteredType != r.MappedToType)
             {
                 StepScopeSynchronization.AddStepScopeDependency(r.RegisteredType, r.Name, Container, r.MappedToType);
             }
             StepScopeSynchronization.AddStepScopeDependency(r.MappedToType, r.Name, Container, r.MappedToType);
         }
     });
     // Finally, we register the new dependency in the step scope if required
     if (isStepScope)
     {
         if (args.TypeFrom != null)
         {
             StepScopeSynchronization.AddStepScopeDependency(args.TypeFrom, args.Name, Container, args.TypeTo);
         }
         StepScopeSynchronization.AddStepScopeDependency(args.TypeTo, args.Name, Container, args.TypeTo);
     }
 }
		void OnRegistering( object sender, RegisterEventArgs e )
		{
			//var model = e.ComponentModel;
			var conventions = this.Container.Resolve<BootstrapConventions>();

			if ( e.TypeFrom.Is<IRegionInjectionHandler>() )
			{
				if ( this.buffer.Any() )
				{
					var mh = this.Container.Resolve<IRegionInjectionHandler>();
					foreach ( var kvp in this.buffer )
					{
						mh.RegisterViewsAsInterestedIn(
							regionName: kvp.Key,
							views: kvp.Value.AsEnumerable() );
					}

					this.buffer.Clear();
				}
			}
			else
			{
				var regionName = conventions.GetInterestedRegionNameIfAny( e.TypeTo );
				if ( !String.IsNullOrWhiteSpace( regionName ) )
				{
					var viewType = e.TypeTo;

					if ( this.Container.IsRegistered<IRegionInjectionHandler>() )
					{
						var mh = this.Container.Resolve<IRegionInjectionHandler>();
						mh.RegisterViewAsInterestedIn( regionName, viewType );
					}
					else if ( this.buffer.ContainsKey( regionName ) )
					{
						this.buffer[ regionName ].Add( viewType );
					}
					else
					{
						this.buffer.Add( regionName, new List<Type>() { viewType } );
					}
				}
			}
		}
Ejemplo n.º 9
0
        private void AddRegistration(object sender, RegisterEventArgs e)
        {
            if (e.TypeFrom == null || !e.TypeFrom.GetTypeInfo().IsInterface)
            {
                return;
            }

            List<Type> stack = null;
            if (!_typeStacks.ContainsKey(e.TypeFrom))
            {
                stack = new List<Type>();
                _typeStacks.Add(e.TypeFrom, stack);
            }
            else
            {
                stack = _typeStacks[e.TypeFrom];
            }

            stack.Add(e.TypeTo);
        }
		void Attach( String key, RegisterEventArgs h )
		{
			var broker = this.Container.Resolve<IMessageBroker>();

			var invocationModel = h.TypeTo.Is<INeedSafeSubscription>() ? InvocationModel.Safe : InvocationModel.Default;
			h.TypeTo.GetAttributes<SubscribeToMessageAttribute>()
				.Return( attributes =>
				{
					return attributes.Select( a => typeof( IMessageHandler<> ).MakeGenericType( a.MessageType ) )
					.Where( gh => this.IsInterestingHandler( h, gh ) );
				}, () =>
				{
					return h.TypeTo.GetInterfaces()
						.Where( i => ( i.Is<IMessageHandler>() || i.Is<IHandleMessage>() ) && i.IsGenericType );
				}, attributes =>
				{
					return !attributes.Any();
				} )
				.ForEach( t => this.Subscribe( broker, key, h, t, invocationModel ) );
		}
Ejemplo n.º 11
0
 private void OnRegister(object sender, RegisterEventArgs e)
 {
     Context.RegisterNamedType(e.TypeFrom ?? e.TypeTo, e.Name);
     if (e.TypeFrom != null)
     {
         if (e.TypeFrom.GetTypeInfo().IsGenericTypeDefinition&& e.TypeTo.GetTypeInfo().IsGenericTypeDefinition)
         {
             Context.Policies.Set <IBuildKeyMappingPolicy>(
                 new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(e.TypeTo, e.Name)),
                 new NamedTypeBuildKey(e.TypeFrom, e.Name));
         }
         else
         {
             Context.Policies.Set <IBuildKeyMappingPolicy>(
                 new BuildKeyMappingPolicy(new NamedTypeBuildKey(e.TypeTo, e.Name)),
                 new NamedTypeBuildKey(e.TypeFrom, e.Name));
         }
     }
     if (e.LifetimeManager != null)
     {
         this.SetLifetimeManager(e.TypeTo, e.Name, e.LifetimeManager);
     }
 }
 private void OnRegister(object sender, RegisterEventArgs e)
 {
     Context.RegisterNamedType(e.TypeFrom ?? e.TypeTo, e.Name);
     if (e.TypeFrom != null)
     {
         if (e.TypeFrom.GetTypeInfo().IsGenericTypeDefinition && e.TypeTo.GetTypeInfo().IsGenericTypeDefinition)
         {
             Context.Policies.Set<IBuildKeyMappingPolicy>(
                 new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(e.TypeTo, e.Name)),
                 new NamedTypeBuildKey(e.TypeFrom, e.Name));
         }
         else
         {
             Context.Policies.Set<IBuildKeyMappingPolicy>(
                 new BuildKeyMappingPolicy(new NamedTypeBuildKey(e.TypeTo, e.Name)),
                 new NamedTypeBuildKey(e.TypeFrom, e.Name));
         }
     }
     if (e.LifetimeManager != null)
     {
         SetLifetimeManager(e.TypeTo, e.Name, e.LifetimeManager);
     }
 }
        private void OnRegisterType(object sender, RegisterEventArgs e)
        {
            object[] atts = null;
            if (e.TypeFrom != null)
            {
                atts = e.TypeFrom.GetCustomAttributes(typeof(SystemImmutableComponentAttribute), false);
            }

            object[] atts2 = null;
            if (e.TypeTo != null)
            {
                atts2 = e.TypeTo.GetCustomAttributes(typeof(SystemImmutableComponentAttribute), false);
            }

            if ((atts != null && atts.Length > 0) || (atts2 != null && atts2.Length > 0)
                && (SistemComponentTypeRegistered != null))
            {
                var duplicated = (_registeredTypes.Contains(e.TypeFrom) || (_registeredTypes.Contains(e.TypeTo)));

                SistemComponentTypeRegistered(this, new TypeRegisterEventArgs(e, duplicated));
            }

            AddType(e.TypeFrom, e.TypeTo);
        }
 private bool ShouldIntercept(RegisterEventArgs e)
 {
     return e != null && e.TypeFrom != null && e.TypeFrom.IsInterface && _interfaces.Contains(e.TypeFrom);
 }
 void Context_Registering(object sender, RegisterEventArgs e)
 {
     _logger.Log(String.Format("REG TYPE {0}", (e.TypeTo ?? e.TypeFrom).Name), Category.Debug, Priority.None);
 }
		Boolean IsInterestingHandler( RegisterEventArgs e, Type t )
		{
			return e.TypeFrom.Is( t ) || e.TypeTo.Is( t );
		}
		void Subscribe( IMessageBroker broker, String key, RegisterEventArgs h, Type genericHandler, InvocationModel invocationModel )
		{
			var messageType = genericHandler.GetGenericArguments().Single();

			logger.Debug
			(
				"\tSubscribing to message: {0}",
				messageType.ToString( "SN" )
			);

			if ( genericHandler.Is<IMessageHandler>() )
			{
				broker.Subscribe( this, messageType, invocationModel, msg =>
				{
					if ( this.Container != null )
					{
						var handler = this.Container.Resolve( h.TypeFrom, key ) as IMessageHandler;
						if ( handler != null )
						{
							logger.Debug
							(
								"Dispatching message {0} to IMessageHandler {1}",
									msg.GetType().ToString( "SN" ),
									handler.GetType().ToString( "SN" )
							);

							if ( handler.ShouldHandle( msg ) )
							{
								handler.Handle( msg );
							}
						}
						else
						{
							logger.Debug
							(
								"IMessageHandler for {0} is null.",
								msg.GetType().ToString( "SN" )
							);
						}
					}
					else
					{
						logger.Debug( "Kernel is null." );
						logger.Debug( "Kernel is null." );
					}
				} );
			}
			else if ( genericHandler.Is<IHandleMessage>() )
			{
				broker.Subscribe( this, messageType, invocationModel, ( s, msg ) =>
				{
					if ( this.Container != null )
					{
						var handler = this.Container.Resolve( h.TypeFrom, key ) as IHandleMessage;
						if ( handler != null )
						{
							logger.Debug
							(
								"Dispatching message {0} to IHandleMessage {1}",
									msg.GetType().ToString( "SN" ),
									handler.GetType().ToString( "SN" )
							);

							if ( handler.ShouldHandle( s, msg ) )
							{
								handler.Handle( s, msg );
							}
						}
						else
						{
							logger.Debug
							(
								"IHandleMessage for {0} is null.",
								msg.GetType().ToString( "SN" )
							);
						}
					}
					else
					{
						logger.Debug( "Kernel is null." );
					}
				} );
			}
		}
Ejemplo n.º 18
0
 private void TypeRegistering(object sender, RegisterEventArgs args)
 {
     var type = args.TypeTo;
     if (_interceptor.Match(type))
         _interceptor.ItemCreated(type, args.LifetimeManager == null || args.LifetimeManager is TransientLifetimeManager);
 }
Ejemplo n.º 19
0
 public TypeRegisterEventArgs(RegisterEventArgs e, bool isDuplicated)
     : this(e.TypeFrom, e.TypeTo, e.Name, e.LifetimeManager, isDuplicated)
 {
 }