I have following situation:
public interface IFormater
{
StyleInformation GetStyleInformation(Fact fact);
FactType[] Formats { get; }
}
public const string STYLENAME = "ABZ Date Style";
ILocalization localization;
public DateFormater(ILocalization localization)
{
this.localization = localization;
}
public StyleInformation GetStyleInformation(Fact fact)
{
return new StyleInformation(STYLENAME, false, false, false, false, false, true, localization.DateFormat);
}
public FactType[] Formats
{
get { return new FactType[] { FactType.Date }; }
}
public class FormattingFacade
{
List<IFormater> formatters;
IWindsorContainer container;
public FormattingFacade(IWindsorContainer container)
{
this.container = container;
formatters = new List<IFormater>();
var formaterInterface = typeof(IFormater);
foreach (Type type in this.GetType().Assembly.GetTypes())
{
if (formaterInterface.IsAssignableFrom(type) && !type.Equals(formaterInterface))
{
container.Register(Component.For(new Castle.Core.ComponentModel(type.FullName, type, type)));
formatters.Add((IFormater)container.Resolve(type));
}
}
}
public StyleInformation GetStyleInformation(Fact fact)
{
foreach (IFormater formater in formatters)
if (formater.Formats.Contains(fact.Concept.XbrlBaseType)) return formater.GetStyleInformation(fact);
throw new NoFormaterException();
}
}
The problem is that castle throws exception when tries to resolve formater that has some parameters in constructor. If there is formater with only no arg constructor it works.
How should I register and how should I resolve these types but to keep this level of flexibility.
I solved it this way:
I just registered components with generic aproach and to be able to do that I named each one of them by number key. I tested this one. It’s working well.