Results 1 to 8 of 8

Thread: @Configurable Style Depenency Injection into domain objects that are created via new

  1. #1
    Join Date
    Dec 2006
    Location
    Saarbrücken / Saarland / Germany
    Posts
    24

    Default @Configurable Style Depenency Injection into domain objects that are created via new

    Hello,

    in Java you can easily inject Dependencies into domain objects that are created manually via new ... (...) using the Spring / AspectJ -Integration together with the @Configurable Annotation.
    http://www.springframework.org/docs/reference/aop.html ->@Configurable

    Here is an exmaple how one could accomplish the same with Spring.Net using Postsharp:
    http://www.postsharp.org/community/f...laos/972353196

    Best regards,
    Thomas

  2. #2
    Join Date
    Jan 2006
    Location
    Cambridge, UK
    Posts
    1,340

    Default

    Hi Thomas,

    this is indeed great stuff! Thanks for sharing it with us. If you already have a more elaborate/production-ready version would you mind sharing it with others in the Spring.NET universe? *wink* ;-)

    cheers,
    Erich

  3. #3
    Join Date
    Oct 2005
    Location
    Toulouse, France
    Posts
    1,409

    Default

    Hi,

    Quote Originally Posted by Thomas.Darimont View Post
    Here is an exmaple how one could accomplish the same with Spring.Net using Postsharp:
    http://www.postsharp.org/community/f...laos/972353196
    The link does not work for me. Neither http://www.postsharp.org/.

    Is this normal ?

    - Bruno
    My english is as poor as my taylor is rich

  4. #4
    Join Date
    Jan 2006
    Location
    Cambridge, UK
    Posts
    1,340

    Default

    Hi Bruno,

    actually I had the same problems. The domain name of www.postsharp.org needs 3 recursions to be resolved and some DNS servers are not capable of this. The first output below is from my default DNS, the second one from our office DNS:
    Code:
    > www.postsharp.org
    Server:  ns1.sil.at
    Address:  213.129.232.1
    
    ------------
    Got answer:
        HEADER:
            opcode = QUERY, id = 2, rcode = NXDOMAIN
            header flags:  response, want recursion, recursion avail.
            questions = 1,  answers = 1,  authority records = 0,  additional = 0
    
        QUESTIONS:
            www.postsharp.org, type = A, class = IN
        ANSWERS:
        ->  www.postsharp.org
            canonical name = backbone.fraiteur.net
            ttl = 1800 (30 mins)
    
    ------------
    ------------
    Got answer:
        HEADER:
            opcode = QUERY, id = 3, rcode = NXDOMAIN
            header flags:  response, want recursion, recursion avail.
            questions = 1,  answers = 1,  authority records = 0,  additional = 0
    
        QUESTIONS:
            www.postsharp.org, type = A, class = IN
        ANSWERS:
        ->  www.postsharp.org
            canonical name = backbone.fraiteur.net
            ttl = 1800 (30 mins)
    
    ------------
    *** ns1.sil.at can't find www.postsharp.org: Non-existent domain
    Code:
    > www.postsharp.org
    Server:  dd1.diamonddogs.at
    Address:  193.41.201.242
    
    ------------
    Got answer:
        HEADER:
            opcode = QUERY, id = 5, rcode = NOERROR
            header flags:  response, want recursion, recursion avail.
            questions = 1,  answers = 3,  authority records = 2,  additional = 0
    
        QUESTIONS:
            www.postsharp.org, type = A, class = IN
        ANSWERS:
        ->  www.postsharp.org
            canonical name = backbone.fraiteur.net
            ttl = 1800 (30 mins)
        ->  backbone.fraiteur.net
            canonical name = vs185.server4u.cz
            ttl = 1800 (30 mins)
        ->  vs185.server4u.cz
            internet address = 81.91.84.124
            ttl = 600 (10 mins)
        AUTHORITY RECORDS:
        ->  server4u.cz
            nameserver = ns.web4u.cz
            ttl = 600 (10 mins)
        ->  server4u.cz
            nameserver = ns2.web4u.cz
            ttl = 600 (10 mins)
    
    ------------
    Non-authoritative answer:
    Name:    vs185.server4u.cz
    Address:  81.91.84.124
    Aliases:  www.postsharp.org, backbone.fraiteur.net
    As u can see it takes the aliases www.postsharp.org->backbone.fraiteur.net->vs185.server4u.cz to resolve the IP.

    To put it short: If anyone experiences problems accessing www.postsharp.org, try a different nameserver and write to the webmaster

    cheers,
    Erich

  5. #5
    Join Date
    Dec 2006
    Location
    Saarbrücken / Saarland / Germany
    Posts
    24

    Default

    Hello Erich,

    If you already have a more elaborate/production-ready version would you mind sharing it with others in the Spring.NET universe? *wink* ;-)
    My current implementation looks like this:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using PostSharp.Laos;
    using Spring.Context.Support;
    using Spring.Context;
    using System.Reflection;
    using PostSharp.Extensibility;
    using PostSharp;
    
    namespace De.Tutorials.Spring.Laos.Example.Spring
    {
        [Serializable]
        [MulticastAttributeUsage(MulticastTargets.Method | MulticastTargets.Constructor, PersistMetaData=true)]
        public class Configurable : PostSharp.Laos.OnMethodBoundaryAspect
        {
            [NonSerialized]
            IApplicationContext applicationContext;
    
            public override void CompileTimeInitialize(MethodBase method)
            {
                base.CompileTimeInitialize(method);
            }
    
            public override void RuntimeInitialize(System.Reflection.MethodBase method)
            {
                base.RuntimeInitialize(method);
                applicationContext = ContextRegistry.GetContext();
            }
    
    
            public override void OnEntry (MethodExecutionEventArgs eventArgs)
            {
                //Console.WriteLine(eventArgs.Method.Name);
                if (eventArgs.Method.IsConstructor)
                {
                    if (null != eventArgs.Instance)
                    {
                        //need to check the constructor because the Configurable attribute will be added to the constructor...
                        object[] configurableAttributes = eventArgs.Method.GetCustomAttributes(typeof(Configurable), true);
    
                        if (null != configurableAttributes && configurableAttributes.Length == 1)
                        {
                            //Inject Dependencies into the given instance according to the spring object configuration
                            applicationContext.ConfigureObject(eventArgs.Instance, eventArgs.Instance.GetType().FullName);
                        }
                    }
                }
            }
        }
    }
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using De.Tutorials.Spring.Laos.Example.Spring;
    
    namespace De.Tutorials.Spring.Laos.Example
    {
        
        [Serializable]
        [Configurable(AttributeTargetMembers = ".ctor")]
        public class Person
        {
            
            public IGreetingService GreetingService
            {
                get { return greetingService; }
                set { greetingService = value; }
            }
            [NonSerialized]
            private IGreetingService greetingService;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            } private string name;
    
    
            //public Person()
            //    : this("Nobody")
            //{
    
            //}
    
            public Person(string name)
            {
                this.name = name;
            }
    
            public override string ToString()
            {
                return GreetingService.Greet(Name);
            }
        }
    }
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace De.Tutorials.Spring.Laos.Example
    {
        public interface IGreetingService
        {
            string Greet(string arg);
        }
    }
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace De.Tutorials.Spring.Laos.Example.Internal
    {
        public class GreetingService : IGreetingService
        {
            #region IService Member
    
            public string Greet(string name)
            {
                return "Hello: " + name;
            }
    
            #endregion
        }
    }
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Spring.Context.Support;
    using Spring.Context;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace De.Tutorials.Spring.Laos.Example
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                IApplicationContext applicationContext = ContextRegistry.GetContext();
    
                Person person = new Person("Thomas");
    
                Console.WriteLine(person);
    
                Person transportedPerson = (Person)Transport(person);
    
                Console.WriteLine(transportedPerson );
            }
    
            private static object Transport(object o)
            {
                MemoryStream memoryStream = new MemoryStream();
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(memoryStream, o);
                memoryStream.Position = 0;
                return formatter.Deserialize(memoryStream);
            }
        }
    }
    [xml]
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <configSections>
    <sectionGroup name="spring">
    <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
    <section name="objects" type="Spring.Context.Support.DefaultSectionHandler , Spring.Core" />
    </sectionGroup>
    </configSections>
    <spring>
    <context name="spring.root">
    <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">

    <object name="greetingService" singleton="true"
    type="De.Tutorials.Spring.Laos.Example.Internal.Gr eetingService,De.Tutorials.Spring.Laos.Example">
    </object>

    <object type="De.Tutorials.Spring.Laos.Example.Person,De.T utorials.Spring.Laos.Example" abstract="true">
    <property name="GreetingService" ref="greetingService"/>
    </object>

    </objects>
    </spring>
    </configuration>
    [/xml]
    This works quite fine ( except for Serialization... but I can solve that problem in the same way it is done in AspectJ for Java (via introduction of a readResolve() Method equivalent for .net ;-) ) for me but Gael Fraiteur (the mastermind behind postsharp) told me that this is not the optimal implementation and that it would be better to use a specific CompositeAspect to do this kind of thing. I try to come up with another example in the next few days.

    Best regards,
    Thomas
    Last edited by Thomas.Darimont; 09-17-2007 at 08:24 AM.

  6. #6
    Join Date
    Dec 2006
    Location
    Saarbrücken / Saarland / Germany
    Posts
    24

  7. #7
    Join Date
    Jan 2006
    Location
    Cambridge, UK
    Posts
    1,340

    Default

    Hi Thomas,

    cool! Thanks a lot - I'll have a look into it as soon as possible.

    cheers,
    Erich

  8. #8
    Join Date
    Dec 2006
    Location
    Saarbrücken / Saarland / Germany
    Posts
    24

    Default

    Hello,

    so the Serialization Issue is fixed in http://postsharp4spring.googlecode.com/svn/

    Best regards,
    Thomas

Posting Permissions

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