Spring.Net Overview

Configure

  • Add to References
    • Spring.Core.dll
  • Add to App.config or Web.config
    <sectionGroup name="spring">
      <section name="context"
        type="Spring.Context.Support
                .ContextHandler,
        Spring.Core" />
        <section name="objects"
        type="Spring.Context.Support
                .DefaultSectionHandler,
      Spring.Core" />
    </sectionGroup>
  • Define an object interface
    public interface ISimpleMessage
    {
    String Message { get; }
    }
  • Do an implementation
    public class SimpleMessage  
      : ISimpleMessage{
      private String _message;
      public SimpleMessage(){}
      public SimpleMessage(String message)
      {
        _message = message;
      }
     
      public String Message
      {
        get { return _message; }
        set { _message = value; }
      }
    }
  • Map objects in App.config
    <spring>
    <context>
      <resource uri="config://spring/objects" />
    </context>
    <objects xmlns=
      "http://www.springframework.net">
      <!-- Messages -->
      <object name="SimpleMessage"
        type="Test.Spring.SimpleMessage, 
        Test.Spring"
        singleton="false">
        <constructor-arg name="message"
        value="constructor-based injection." />
      </object>
      <object name="SimpleMessageByProperty"
        type="Test.Spring.SimpleMessage, 
          Test.Spring"
        singleton="false">
        <property name="Message" 
          value="property-based injection." />
      </object>
    </objects>
    </spring>
  • Use
    using Spring.Context;
    using Spring.Context.Support;
     
    ...
     
    IApplicationContext ctx = 
      ContextRegistry.GetContext();
    ISimpleMessage smsg = (ISimpleMessage) 
      ctx.GetObject("SimpleMessage");
    label1.Text = smsg.Message;
    ...
  • Constructor injection
    <object name="SimpleMessage"
      type="Test.Spring.SimpleMessage, 
      Test.Spring"
      singleton="false">
      <constructor-arg name="message"
      value="constructor-based injection." />
    </object>
  • Property injection
    <object name="SimpleMessageByProperty"
      type="Test.Spring.SimpleMessage, 
        Test.Spring"
      singleton="false">
      <property name="Message" 
        value="property-based injection." />
    </object>
  • IMessageSource
    • For retrieving localized resources
      • Text
      • Image
    • Add a resouce file called TestResource.resx
    • Add a resouce file called TestImages.resx
    • Add to App.config
    • <object name="messageSource" 
         type="Spring.Context.Support
         .ResourceSetMessageSource, 
          Spring.Core">
        <property name="resourceManagers">
         <list>
          <value>Test.Spring.TestImages, 
           Test.Spring</value>
         <ref object="myResourceManager" />
          </list>
        </property>
      </object>
       
      <object name="myResourceManager" 
        type="Spring.Objects.Factory
         .Config.ResourceManagerFactoryObject, 
         Spring.Core">
        <property name="baseName" 
         value="Test.Spring.TestResource" />
        <property name="assemblyName" 
         value="Test.Spring" />
      </object>
    • Use
      // Default culture
      label1.Text = ctx.GetMessage("FirstName", 
        CultureInfo.CurrentCulture);
      //Display spanish
      CultureInfo spanish = new CultureInfo("es");
      label1.Text = 
        ctx.GetMessage("FirstName", spanish);
      // Get image
      pbxTest.Image = ctx.GetResourceObject(
        "Sunset") as Image;
  • References

    Dependency Injection with Spring.Net

    
    
This entry was posted in c#. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.