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
    {
        get;
        set;
    }
 
    #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;
    }
 
    #endregion
}

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: ,

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

  1. SHANE says:

    Get@Advair.Online” rel=”nofollow”>.

    Buygeneric drugs hwi…

  2. DWAYNE says:

    Buy@Generic.Advair” rel=”nofollow”>.

    Buygeneric drugs ouw…

  3. AARON says:

    Buy@Generic.Advair.Without.Prescription” rel=”nofollow”>..

    Buyno prescription jwi…

  4. RANDY says:

    Purchase@Albenza.Online” rel=”nofollow”>…

    Buydrugs without prescription ttp…

  5. NATHAN says:

    Purchase@Cheap.Albenza” rel=”nofollow”>…

    Buyit now knc…

  6. KEN says:

    arava@adverse.events” rel=”nofollow”>…

    Buygeneric meds…

  7. STEPHEN says:

    accutane@verdict.buy” rel=”nofollow”>..

    Buydrugs without prescription…

  8. GENE says:

    aciphex@muscle.pain” rel=”nofollow”>..

    Buygeneric pills…

  9. AUSTIN says:

    actos@side.effects.edema” rel=”nofollow”>..

    Buygeneric drugs…

  10. DOUGLAS says:

    adalat@asus.buy” rel=”nofollow”>.

    Buydrugs without prescription…

  11. STEPHEN says:

    allopurinol@atrial.fibrillation” rel=”nofollow”>.

    Buygeneric pills…

  12. JERRY says:

    norvasc and theophylline

    Buy_drugs without prescription…

  13. RAY says:

    birth control pills for pms symptoms

    Buy_drugs without prescription…

  14. ZACHARY says:

    citalopram length of use

    Buy_drugs without prescription…

  15. CHRIS says:

    rapid weight loss

    Buy_generic drugs…

  16. HERMAN says:

    weight gain stories feeders

    Buy_drugs without prescription…

  17. BRYAN says:

    cheap travel nebulizer

    Buy_generic drugs…

  18. TRAVIS says:

    xanax and pregnancy

    Buy_generic pills…

  19. HOMER says:

    dhea treatment of depression

    Buy_drugs without prescription…

  20. JEFF says:

    pregnancy issues with men

    Buy_no prescription…

  21. FREDRICK says:

    colon cancer lymph node

    Buy_without prescription…

  22. DALE says:

    hot spots or cancer

    Buy_without prescription…

  23. RAMON says:

    seed implant for prostate cancer

    Buy_drugs without prescription…

  24. FRANCIS says:

    adult male wheezing new onset

    Buy_drugs without prescription…

  25. DAN says:

    medical nebulizer

    Buy_generic meds…

Leave a Reply