Results 1 to 2 of 2

Thread: Configuring Interceptors via CodeConfig

  1. #1
    Join Date
    Aug 2012
    Location
    Glasgow, UK
    Posts
    2

    Default Configuring Interceptors via CodeConfig

    I'm currently trying to convert the XML configuration files in a project I'm working on into CodeConfig, with mixed success; I've succeeded in bringing the DbProvider config over to CodeConfig, but I'm running into trouble with the interceptors.

    So here is the original xml config for my services/interceptors:

    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <objects xmlns="http://www.springframework.net" xmlns:tx="http://www.springframework.net/tx">
    
      <!-- Spring transaction support -->
        <object id="TransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32">
            <property name="SessionFactory" ref="SessionFactory"/>
            <property name="EntityInterceptorObjectName" value="AuditInterceptor"/>
        </object>
        <object id="AttributesTransactionAttributeSource" type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data" />
    
        <!-- Interceptors -->
        <object id="TransactionInterceptor" singleton="false" type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data" >
            <property name="TransactionManager" ref="TransactionManager"/>
            <property name="TransactionAttributeSource" ref="AttributesTransactionAttributeSource"/>
        </object>
        <object name="ServiceLoggingInterceptor" type="LoggingInterceptor, Services">
            <property name="LogUniqueIdentifier"  value="false"/>
            <property name="LogExecutionTime"     value="true"/>
            <property name="LogMethodArguments"   value="false"/>
            <property name="Separator"            value=" | "/>
            <property name="UseDynamicLogger"     value="true"/>
            <property name="HideProxyTypeNames"   value="true"/>
            <property name="LogLevel"             value="Info"/>
        </object>
      <object id="ServiceMethodInterceptor" type="MethodInterceptor, Services" >
        <property name="SessionFactory" ref="SessionFactory"/>
        <property name="DbSchemaManager" ref="DbSchemaManager"/>
      </object>
      <object id="AuditInterceptor" type="AuditInterceptor, Model" singleton="false"/>
    
        <!-- Service proxy template -->
        <object id="ServiceProxyTemplate">
            <property name="isSingleton" value="false"/>
            <property name="isFrozen" value="false"/>
            <property name="interceptorNames">
                <list>
                    <value>ServiceLoggingInterceptor</value>
                    <value>TransactionInterceptor</value>
                    <value>ServiceMethodInterceptor</value>
                </list>
            </property>
        </object>
    
        <!-- Service template -->
        <object id="ServiceTemplate">
        </object>
    
        <!-- Play service -->
        <object id="RealPlayService"            type="PlayService, Services"     singleton="false" parent="ServiceTemplate"/>
        <object id="PlayServicePrototypeTarget" type="Spring.Aop.Target.PrototypeTargetSource, Spring.Aop"  singleton="false">
            <property name="TargetObjectName">
                <idref object="RealPlayService"/>
            </property>
        </object>
        <object id="PlayService"                type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop" parent="ServiceProxyTemplate">
            <property name="targetName" value="PlayServicePrototypeTarget"/>
        </object>
    
      <!-- Security service -->
      <object id="RealSecurityService"            type="SecurityService, Services"     singleton="false" parent="ServiceTemplate"/>
      <object id="SecurityServicePrototypeTarget" type="Spring.Aop.Target.PrototypeTargetSource, Spring.Aop"  singleton="false">
        <property name="TargetObjectName">
          <idref object="RealSecurityService"/>
        </property>
      </object>
      <object id="SecurityService"                type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop" parent="ServiceProxyTemplate">
        <property name="targetName" value="SecurityServicePrototypeTarget"/>
      </object>
    
      <!-- Data Collection service -->
      <object id="RealDataCollectionService"            type="DataCollectionService, Services"     singleton="false" parent="ServiceTemplate"/>
      <object id="DataCollectionServicePrototypeTarget" type="Spring.Aop.Target.PrototypeTargetSource, Spring.Aop"  singleton="false">
        <property name="TargetObjectName">
          <idref object="RealDataCollectionService"/>
        </property>
      </object>
      <object id="DataCollectionService"                type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop" parent="ServiceProxyTemplate">
        <property name="targetName" value="DataCollectionServicePrototypeTarget"/>
      </object>
    
      <!-- Information service -->
      <object id="RealInformationService"     type="InformationService, Services"     singleton="false" parent="ServiceTemplate"/>
      <object id="InformationServicePrototypeTarget" type="Spring.Aop.Target.PrototypeTargetSource, Spring.Aop"  singleton="false">
        <property name="TargetObjectName">
          <idref object="RealInformationService"/>
        </property>
      </object>
      <object id="InformationService"                type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop" parent="ServiceProxyTemplate">
        <property name="targetName" value="InformationServicePrototypeTarget"/>
      </object>
    
      <!-- Diagnostics service -->
      <object id="RealDiagnosticsService"     type="DiagnosticsService, Services"     singleton="false" parent="ServiceTemplate"/>
      <object id="DiagnosticsServicePrototypeTarget" type="Spring.Aop.Target.PrototypeTargetSource, Spring.Aop"  singleton="false">
        <property name="TargetObjectName">
          <idref object="RealDiagnosticsService"/>
        </property>
      </object>
      <object id="DiagnosticsService"                type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop" parent="ServiceProxyTemplate">
        <property name="targetName" value="DiagnosticsServicePrototypeTarget"/>
      </object>
        
    </objects>
    Gonna make a second post for the CodeConfig...
    Last edited by henry.wilson; 08-08-2012 at 10:46 AM.

  2. #2
    Join Date
    Aug 2012
    Location
    Glasgow, UK
    Posts
    2

    Default

    So code config looks like this:

    Code:
    [Configuration]
        public class ServicesConfig : IApplicationContextAware
        {
            #region IApplicationContextAware Implementation
    
            //field to hold the injected context
            private IApplicationContext _context;
    
            //property setter defined by the IApplcationContextAware interface
            //     so that the context can inject itself into the class
            public IApplicationContext ApplicationContext { set { _context = value; } }
    
            #endregion
    
    
            #region Transaction Support
    
            [Definition]
            public virtual HibernateTransactionManager TransactionManager()
            {
                var tm = new HibernateTransactionManager(_context.GetObject<ISessionFactory>("SessionFactory")) { EntityInterceptorObjectName = "AuditInterceptor" };
                return tm;
            }
    
            [Definition]
            public virtual AttributesTransactionAttributeSource AttributesTransactionAttributeSource()
            {
                return new AttributesTransactionAttributeSource();
            }
    
            #endregion
    
    
            #region Interceptors
    
            [Definition]
            public virtual TransactionInterceptor TransactionInterceptor()
            {
                var transactionInterceptor = new TransactionInterceptor
                                                 {
                                                     TransactionManager =
                                                         _context.GetObject<IPlatformTransactionManager>("TransactionManager"),
                                                     TransactionAttributeSource =
                                                         _context.GetObject<AttributesTransactionAttributeSource>()
                                                 };
                return transactionInterceptor;
            }
    
            [Definition]
            public virtual LoggingInterceptor ServiceLoggingInterceptor()
            {
                return new LoggingInterceptor
                {
                    LogUniqueIdentifier = false,
                    LogExecutionTime = true,
                    LogMethodArguments = false,
                    Separator = " | ",
                    UseDynamicLogger = true,
                    HideProxyTypeNames = true,
                    LogLevel = LogLevel.Info
                };
            }
    
            [Definition]
            public virtual MethodInterceptor ServiceMethodInterceptor()
            {
                return new MethodInterceptor
                                {
                                    SessionFactory = _context.GetObject<ISessionFactory>("SessionFactory"),
                                    DbSchemaManager = _context.GetObject<IDbSchemaManager>("DbSchemaManager")
                                };
            }
    
            [Definition]
            [Scope(ObjectScope.Prototype)]
            public virtual AuditInterceptor AuditInterceptor()
            {
                return new AuditInterceptor();
            }
    
            #endregion
    
    
            #region Play Service
    
            [Definition]
            [Scope(ObjectScope.Prototype)]
            public virtual PlayService RealPlayService()
            {
                return new PlayService();
            }
    
            [Definition]
            [Scope(ObjectScope.Prototype)]
            public virtual PrototypeTargetSource PlayServicePrototypeTarget()
            {
                return new PrototypeTargetSource
                                    {
                                        TargetObjectName = FormatRealTargetObjectName(typeof(PlayService))
                                    };
            }
    
            [Definition]
            public virtual ProxyFactoryObject PlayService()
            {
                var pfo = new ProxyFactoryObject();
                SetupProxyFactoryObject(pfo);
                pfo.TargetName = FormatPrototypeTargetObjectName(typeof(PlayService));
                return pfo;
            }
    
            #endregion
    
    
            #region Security Service
    
            [Definition]
            [Scope(ObjectScope.Prototype)]
            public virtual SecurityService RealSecurityService()
            {
                return new SecurityService();
            }
    
            [Definition]
            [Scope(ObjectScope.Prototype)]
            public virtual PrototypeTargetSource SecurityServicePrototypeTarget()
            {
                return new PrototypeTargetSource
                {
                    TargetObjectName = FormatRealTargetObjectName(typeof(SecurityService))
                };
            }
    
            [Definition]
            public virtual ProxyFactoryObject SecurityService()
            {
                var pfo = new ProxyFactoryObject();
                SetupProxyFactoryObject(pfo);
                pfo.TargetName = FormatPrototypeTargetObjectName(typeof(SecurityService));
                return pfo;
            }
    
            #endregion
    
    
            #region Data Collection Service
    
            [Definition]
            [Scope(ObjectScope.Prototype)]
            public virtual DataCollectionService RealDataCollectionService()
            {
                return new DataCollectionService();
            }
    
            [Definition]
            [Scope(ObjectScope.Prototype)]
            public virtual PrototypeTargetSource DataCollectionServicePrototypeTarget()
            {
                return new PrototypeTargetSource
                {
                    TargetObjectName = FormatRealTargetObjectName(typeof(DataCollectionService))
                };
            }
    
            [Definition]
            public virtual ProxyFactoryObject DataCollectionService()
            {
                var pfo = new ProxyFactoryObject();
                SetupProxyFactoryObject(pfo);
                pfo.TargetName = FormatPrototypeTargetObjectName(typeof(DataCollectionService));
                return pfo;
            }
    
            #endregion
    
    
            #region Information Service
    
            [Definition]
            [Scope(ObjectScope.Prototype)]
            public virtual InformationService RealInformationService()
            {
                return new InformationService();
            }
    
            [Definition]
            [Scope(ObjectScope.Prototype)]
            public virtual PrototypeTargetSource InformationServicePrototypeTarget()
            {
                return new PrototypeTargetSource
                {
                    TargetObjectName = FormatRealTargetObjectName(typeof(InformationService))
                };
            }
    
            [Definition]
            public virtual ProxyFactoryObject InformationService()
            {
                var pfo = new ProxyFactoryObject();
                SetupProxyFactoryObject(pfo);
                pfo.TargetName = FormatPrototypeTargetObjectName(typeof(InformationService));
                return pfo;
            }
    
            #endregion
    
    
            #region Diagnostics Service
    
            [Definition]
            [Scope(ObjectScope.Prototype)]
            public virtual DiagnosticsService RealDiagnosticsService()
            {
                return new DiagnosticsService();
            }
    
            [Definition]
            [Scope(ObjectScope.Prototype)]
            public virtual PrototypeTargetSource DiagnosticsServicePrototypeTarget()
            {
                return new PrototypeTargetSource
                {
                    TargetObjectName = FormatRealTargetObjectName(typeof(DiagnosticsService))
                };
            }
    
            [Definition]
            public virtual ProxyFactoryObject DiagnosticsService()
            {
                var pfo = new ProxyFactoryObject();
                SetupProxyFactoryObject(pfo);
                pfo.TargetName = FormatPrototypeTargetObjectName(typeof(DiagnosticsService));
                return pfo;
            }
    
            #endregion
    
    
            #region Helpers
    
            public string FormatRealTargetObjectName(Type serviceType)
            {
                return "Real" + serviceType.Name;
            }
    
            public string FormatPrototypeTargetObjectName(Type serviceType)
            {
                return serviceType.Name + "PrototypeTarget";
            }
    
            public void SetupProxyFactoryObject(ProxyFactoryObject proxyFactoryObject)
            {
                proxyFactoryObject.IsFrozen = false;
                proxyFactoryObject.IsSingleton = false;
                //add all interceptors except Audit
                proxyFactoryObject.InterceptorNames = GetType().GetMethods()
                    .Where(x => x.Name.Contains("Interceptor") && !x.Name.Equals("AuditInterceptor"))
                    .Select(x => x.Name).ToArray();
            }
    
            #endregion
        }
    Everything compiles fine but at runtime I get a null reference error for one of the objects that should be set up by an interceptor, and if I put break points in the interceptors I can see they're never getting hit. Can anyone see what I'm doing wrong?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •