Matt Blair

Matt Blair

I read that you learn more from a poor example than from a correct one. I don't believe this but that means my site will be a success.

1-Minute Read

Had an issue at work where I wanted to store my rules and handlers for a class outside of the class definition, so I could better test the component. Found an easy way to add all your rules into StructureMap, and then retrieve those rules as a list via constructor injection.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using StructureMap;
using StructureMap.Configuration.DSL;

public class TravelRegistry : Registry
{
    public TravelRegistry()
    {
        For<ITransportHandler>().Add<ApprovedAccommodationHandler>();
        For<ITransportHandler>().Add<ApprovedCharterFlightHandler>();
        For<ITransportHandler>().Add<ApprovedCommercialFlightHandler>();
        For<ITransportHandler>().Add<ApprovedGroundTransportHandler>();

        For<IEnumerable<ITransportHandler>>().Use(x => x.GetAllInstances<ITransportHandler>());

        For<ITravelRule>().Add<StartLessThanEndRule>();
        For<ITravelRule>().Add<CurrentEndsAfterPreviousRule>();
        For<ITravelRule>().Add<CurrentStartAfterPreviousEndRule>();
        For<ITravelRule>().Add<UniqueEndsRule>();
        For<ITravelRule>().Add<UniqueStartAndEndRule>();
        For<ITravelRule>().Add<UniqueStartsRule>();

        For<IEnumerable<ITravelRule>>().Use(x => x.GetAllInstances<ITravelRule>());

    }
}

Recent Posts

Categories

About

This theme was developed for Hugo.