Results 1 to 3 of 3

Thread: Code Based Configuration IOC

Hybrid View

  1. #1
    Join Date
    Apr 2010
    Posts
    6

    Default Code Based Configuration IOC

    Does anyone know when the "code based configuration" will be available? We would rather not use xml files for declaring our dependencies. Also, does anybody know if it will target .net 2.0?

  2. #2
    Join Date
    Sep 2008
    Location
    Houston, TX
    Posts
    111

    Default

    They are about to release Spring 2.0 which allows for attribute based injection which is very slick, even easier than a fluent configuration. I dunno what the ETA on Spring 2.0 is but I've been expecting it any day for about a month ;-).

    Erich did a nice blog post on Spring.Net with code config: http://eeichinger.blogspot.com/2009/...t-for-net.html. Also there is a project out there called Recoil: http://code.google.com/p/spring-recoil/ which gives you fluent configuration of Spring.

  3. #3
    Join Date
    May 2012
    Posts
    1

    Default

    Hi All,

    I'm new to this forum and I was here recently trying to find an answer for how to use Attribute based configuration for setting up my spring container.

    In my lack of luck in finding good examples of how I would do this in Spring.net I created the following scanner that together with CodeConfigApplicationContext does the trick:

    Code:
    using System;
    using Spring.Context.Attributes;
    using Spring.Objects.Factory.Support;
    using Spring.Stereotype;
    
    namespace AttributeBasedSpringConfig
    {
        public class AttributeBasedObjectDefinitionScanner : AssemblyObjectDefinitionScanner
        {
            protected override bool IsCompoundPredicateSatisfiedBy(Type type)
            {
                var attribute = Attribute.GetCustomAttribute(type, typeof(ComponentAttribute), true);
                if (attribute != null)
                {
                    return true;
                }
                return base.IsCompoundPredicateSatisfiedBy(type);
            }
    
            public override void ScanAndRegisterTypes(IObjectDefinitionRegistry registry)
            {
                
                IEnumerable<Type> configTypes = base.Scan();
    
                //if we have at least one config class, ensure the post-processor is registered
                if (configTypes.Any())
                {
                    AttributeConfigUtils.RegisterAttributeConfigProcessors(registry);
                }
    
                foreach (Type type in configTypes)
                {
                    ObjectDefinitionBuilder definition = ObjectDefinitionBuilder.GenericObjectDefinition(type);
    
                    registry.RegisterObjectDefinition(GetObjectRegistryName(definition.ObjectDefinition),
                                                      definition.ObjectDefinition);
                }
            }
    
            private string GetObjectRegistryName(AbstractObjectDefinition objectDefinition)
            {
                var attribute = Attribute.GetCustomAttribute(objectDefinition.ObjectType, typeof (ComponentAttribute), true);
    
                if (attribute != null)
                {
                    var componentName = ((ComponentAttribute) attribute).Name;
                    if (!String.IsNullOrWhiteSpace(componentName))
                    {
                        return componentName;
                    }
                }
    
                return objectDefinition.ObjectTypeName;
            }
        }
    }
    You can find a full example with explanation on how I did it in my blog post: Spring.net Attribute Based Configuration

    P.S. I'm new to C# (almost 1 month exp) so please forgive me if I'm offending the good practices within the language.
    Last edited by Nuno Ferreira; 05-14-2012 at 11:04 PM.

Posting Permissions

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