Using attributes with Spring.NET to configure your application

When using Spring.Net you end up editing xml files to wire everything together. While something like fluent Spring.Net would be very nice to have, some of the xml authoring can be solved by using attributes. As sample I’ll use a real life example where I implemented it.

I had to add reporting to an application which consists of different modules. Depending on the license of the user he’d have a contacts module, a document management module, tasks, etc. The user would select the contents of his report and then fire up the designers to choose fields, order or group them, etc.

The end-user components we use are quite powerful so I only had to find a way to get the content into the report component as a dataset. Normally I keep away from using datasets but in this case it’s allowed. Note that I’m not using data adapters and other stuff, I just transform my rich object model into a flat dataset.

Each module writes an entitymapper for all of the entities that can be used as reporting source.

[EntityMapper(EntityType = typeof(ContactDto))]
public class ContactMapper
    : EntityMapper
    public override DataSet MapToDataSet(IEnumerable<ContactDto> entities)
        DataSet dataSet = new DataSet();
        //fill with data
        return dataSet;

I only wanted to add the object definition to the configuration specific for this module. I didn’t want to edit the config for reporting module. It should just pick all the mappers up and use them when entities of that type are handed to it.

The solution for this is using an IObjectPostProcessor to inspect everything that is configured in your container and do some additional logic.

public class MapperPostProcessor
    : IObjectPostProcessor
    private readonly Type attributeType;
    public MapperPostProcessor()
        attributeType = typeof (EntityMapperAttribute);
    public DataSetMapper Mapper
    #region IObjectPostProcessor Members
    public object PostProcessAfterInitialization(object instance, string objectName)
        object[] o = instance.GetType().GetCustomAttributes(attributeType, true);
        if(o.Length == 1)
            var attribute = o[0] as EntityMapperAttribute;
            var entityMapper = instance as IEntityReportMapper;
            Mapper.RegisterMapper(entityMapper, attribute.EntityType);
        return instance;
    public object PostProcessBeforeInitialization(object instance, string name)
        return instance;

Any postprocessor in your Spring context is automatically picked up by Spring and the methods will be called on the appropriate time. In this implementation I check every instance that is handed to the PostProcessAfterInitialization method for my own attribute, if it’s present I register it to my mapper manager for the specified type. For more info about this you should check out the documentation.

Out of the box Spring.NET has some of these already for you. For instance you can put the [Required] attribute on the properties that need to have a value after they have been constructed. If at that time they haven’t been initialized you’ll get an exception.

Tags: ,

104 Responses to “Using attributes with Spring.NET to configure your application”

  1. JIM says:

    children during the depression

    Buy_drugs without prescription…

  2. sharon scott says:

    short hairstyles

    Undeniably believe that which you stated. Your favorite reason seemed to be on the internet the easiest thing to be aware of. I say to you, I definitely get irked while people consider worries that they plainly do not know about. You managed to hit the…

  3. JOHN says:

    Buy Viagra

    Buy Quality Drugs Now!…

Leave a Reply