Results 1 to 3 of 3

Thread: Proxying an object with a factory method

  1. #1
    Join Date
    Apr 2012
    Posts
    2

    Default Proxying an object with a factory method

    I'd like to apply some interceptors to the Open method of a .NET DbConnection object. Since I'm proxying the object, I was expecting that even the composition-based proxy will still allow me to call the factory methods of IDbConnection, most important of which for my purposes is IDbConnection.CreateCommand. Unfortunately, when I attempt to retrieve the context in my .NET code, I'm confronted by an exception telling me that the proxy doesn't have a CreateCommand method. The message looks something like this:
    Error creating context 'spring.root': Cannot find matching factory method 'CreateCommand on Type [CompositionAopProxy_18aab00c18884dce99c31f25d711df 1b].

    To complicate matters further, when I examine typeof(CompositionAopProxy_18aab00c18884dce99c31f2 5d711df1b) in the immediate window, I'm able to call the GetInterfaces method and see that the proxy indeed does implement IDbConnection.

    I was able to do something similar using Autofac and Castle DynamicProxy, so I'd be surprised if I couldn't do the same thing in Spring.NET.

    Here's the configuration I'm using:
    Code:
                <object id="Factory" factory-method="GetFactory"
                     type="System.Data.Common.DbProviderFactories, System.Data">
                    <constructor-arg index="0" value="System.Data.SqlClient"/>
                </object>
                <object id="Connection" 
                        factory-method="CreateConnection"
                        factory-object="Factory"
                        init-method="Open">
                    <property name="ConnectionString"
                              value="server=localhost\SQLEXPRESS;database=MusicCatalog2;integrated security=SSPI"/>
                </object>
                <object id="ProxiedConnection" type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop">
                    <property name="TargetName" value="Connection"/>
                    <property name="ProxyInterfaces" value="System.Data.IDbConnection"/>
                </object>
    
                <object id="Command"
                       factory-object="ProxiedConnection"
                       factory-method="CreateCommand">
                    <property name="CommandText" value="SELECT * FROM Composers"/>
                </object>
    And the .NET code is simple:

    Code:
                    
    var context = ContextRegistry.GetContext();
    var command = context.GetObject("Command") as IDbCommand;
    Any help with getting this to work will be greatly appreciated!

    musicologyman

  2. #2
    Join Date
    Jul 2010
    Posts
    245

    Default

    Initial investigation into this suggests that SPRNET doesn't support proxying FactoryObjects as you are attempting to do here. We've opened a JIRA for this https://jira.springsource.org/browse/SPRNET-1505 so that you can easily track it and are looking into adding this capability to SPRNET 2.0.

    Thanks,
    -Steve B.

  3. #3
    Join Date
    Apr 2012
    Posts
    2

    Default

    Okay. Thanks!

    musicologyman

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
  •