The final ConfigurationSource

A configuration source in OpenRasta is used to define what resources are available, and where (think routing in ASP.NET).

Unfortunately, once you have more than a few endpoints it rapidly becomes unwieldy.

A few of my colleagues started pulling chunks of configuration out into static methods on separate classes, which led towards… the final ConfigurationSource!

	public class ConfigurationSource : IConfigurationSource
	{
		private readonly IEnumerable<IDefineResources> _resourceDefinitions;

		public ConfigurationSource(IDefineResources[] resourceDefinitions)
		{
			_resourceDefinitions = resourceDefinitions;
		}

		public void Configure()
		{
			using (OpenRastaConfiguration.Manual)
			{
				foreach (var resourceDefinition in _resourceDefinitions)
				{
					resourceDefinition.DefineResources();
				}
			}
		}
	}

This allows you to implement IDefineResources:

public interface IDefineResources
{
	void DefineResources();
}

For example:

public class MyResourceDefinitions : IDefineResources
{
	public void DefineResources()
	{
		ResourceSpace.Has.ResourcesOfType<MyResource>()
			.AtUri("some/resource/{id}")
                        .HandledBy<MyHandler>()
			.AsXmlSerializer
			.ForMediaType(MediaType.Xml);
	}
}

And, as long as the container knows about it, your resources will be defined.

This means you can even define resources outside the main project, allowing you to move in the direction of modular, composable applications.

Advertisements

2 thoughts on “The final ConfigurationSource

    • Graham Hay January 16, 2013 / 9:30 am

      Uh, it’s been a while, but I think we registered the IConfigurationSource with the container. So OR could find it. But it expects one, and only one, hence the need for IDefineResources.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s