Я регистрирую несколько компонентов для службы и хочу предоставить дополнительные метаданные для компонентов, которые можно получить без разрешения службы.
С помощью расширяемой платформы Microsoft вы можете предоставить своим экспортам дополнительные метаданные, как показано здесь Виды метаданных MEF и метаданных , есть ли что-то похожее в Виндзоре?
В настоящее время я пытаюсь обернуть свой компонент в класс с двумя свойствами: IMetadata и Lazy, где ленивый будет разрешать компонент при необходимости.
public class Program
{
public static void Main(string[] args)
{
var container = new WindsorContainer();
// register the wrapped object that contains the Component metadata and a lazy to resolve the component
container.Register(
Classes.FromThisAssembly()
.BasedOn<IService>()
.Configure(config =>
{
// create wrapper
var wrapper = new WrappedComponent();
wrapper.ComponentImpl = new Lazy<IService>(() => container.Resolve<IService>(config.Name));
// add metadata to the wrapper
if (config.Implementation.IsDefined(typeof(ComponentMetadataAttribute), false))
{
var metadata = (IComponentMetadata)config.Implementation.GetCustomAttributes(typeof(ComponentMetadataAttribute), false)[0];
wrapper.Metadata = metadata;
}
// set the component to the wrapper
config.Instance(wrapper);
})
// also set service to wrapper
.WithService.Select((impl, services) => new List<Type>() { typeof(WrappedComponent) }));
// register the components
container.Register(
Classes.FromThisAssembly()
.BasedOn<IService>().Configure(config =>
{
if (config.Implementation.IsDefined(typeof(ComponentMetadataAttribute), false))
{
var metadata = (IComponentMetadata)config.Implementation.GetCustomAttributes(typeof(ComponentMetadataAttribute), false)[0];
config.Named(metadata.Name);
}
}));
}
}
public class WrappedComponent
{
public IComponentMetadata Metadata { get; set; }
public Lazy<IService> ComponentImpl { get; set; }
}
[ComponentMetadata]
public class MyComponent : IService
{
public void Operation()
{
// Do stuff
}
}
public interface IService
{
void Operation();
}
public class ComponentMetadataAttribute : Attribute, IComponentMetadata
{
public string Name { get; set; }
public string Description { get; set; }
public string SomeOtherMetadata { get; set; }
}
public interface IComponentMetadata
{
string Name { get; set; }
string Description { get; set; }
string SomeOtherMetadata { get; set; }
}