以下 示例代码 基本上调用多个虚拟服务,并合并结果。 (在实际应用中,它们将是Web服务。)
问题:创建 ContainerBuilder 的新实例是一种好习惯吗?请在Main方法中查看几个*****。
如果没有,你能指出我正确的方向吗?我对任何建议持开放态度。
注意: Build()或Update()方法只能在ContainerBuilder上调用一次。
private static void Main(string[] args)
{
var builder = new ContainerBuilder();
var container = builder.Build();
// ***** Create a new instance of ContainerBuilder *****
builder = new ContainerBuilder();
builder.RegisterType<AlfaService>().As<IService>().Named<IService>("a");
builder.RegisterType<BravoService>().As<IService>().Named<IService>("b");
builder.Update(container);
// ***** Create a new instance of ContainerBuilder *****
builder = new ContainerBuilder();
var services = new Dictionary<string, IService>
{
{"a", container.ResolveNamed<IService>("a")},
{"b", container.ResolveNamed<IService>("b")}
};
builder.RegisterType<CompositeService>().As<ICompositeService>()
.WithParameter("services", services);
builder.Update(container);
// The following is for testing purpose only.
// In real application, I'll inject ICompositeService to MVC controller.
using (ILifetimeScope scope = container.BeginLifetimeScope())
{
IList<int> ids = scope.Resolve<ICompositeService>().GetIdsBySource("a");
Console.WriteLine("AlfaService: " + string.Join(", ", ids));
ids = scope.Resolve<ICompositeService>().GetAllIds();
Console.WriteLine("All Services: " + string.Join(", ", ids));
}
Console.ReadLine();
}
public interface ICompositeService
{
IList<int> GetIdsBySource(string source);
IList<int> GetAllIds();
}
public interface IService
{
IList<int> GetIds();
}
public class CompositeService : ICompositeService
{
private readonly Dictionary<string, IService> _services;
public CompositeService(Dictionary<string, IService> services)
{
_services = services;
}
public IList<int> GetIdsBySource(string source)
{
return _services.Where(x => x.Key == source)
.Select(x => x.Value).First().GetIds();
}
public IList<int> GetAllIds()
{
return _services.SelectMany(x => x.Value.GetIds()).ToList();
}
}
public class AlfaService : IService
{
public IList<int> GetIds() { return new List<int> {1, 2, 3}; }
}
public class BravoService : IService
{
public IList<int> GetIds() { return new List<int> {4, 5, 6}; }
}
答案 0 :(得分:3)
要解析参数中的内容,您不必构建容器。构建容器需要时间,应尽可能避免。
解决参数内部内容的最简单方法是使用WithParameter
方法,如下所示:
builder.RegisterType<CompositeService>()
.As<ICompositeService>()
.WithParameter((pi, c) => pi.Name == "services", (pi, c) =>
{
return new Dictionary<string, IService> {
{ "a", c.ResolveNamed<IService>("a") },
{ "b", c.ResolveNamed<IService>("b") }
};
});
另一种解决方案是创建自己的Parameter
public class ServiceParameter : Parameter
{
public override Boolean CanSupplyValue(ParameterInfo pi,
IComponentContext context, out Func<Object> valueProvider)
{
valueProvider = null;
if (pi.Name == "services"
&& pi.ParameterType == typeof(Dictionary<String, IService>))
{
valueProvider = () =>
{
return new Dictionary<string, IService> {
{ "a", context.ResolveNamed<IService>("a") },
{ "b", context.ResolveNamed<IService>("b") }
};
};
}
return valueProvider != null;
}
}
以这种方式注册:
builder.RegisterType<CompositeService>()
.As<ICompositeService>()
.WithParameter(new ServiceParameter());
在您的情况下,您可能希望使用元数据 而不是命名服务
builder.RegisterType<AlfaService>().As<IService>().WithMetadata("Key", "a");
builder.RegisterType<BravoService>().As<IService>().WithMetadata("Key", "b");
在CompositeService中,您将依赖IEnumerable<Meta<IService>>
public CompositeService(IEnumerable<Meta<IService>> services)
{
_services = services.ToDictionary(m => (String)m.Metadata["Key"], m => m.Value);
}
如果您不想在Meta
上引入CompositeService
的依赖关系,可以使用自定义Parameter
,将IEnumerable<Meta<IService>>
翻译为IDictionary<String, IService>
public class ServiceParameter : Parameter
{
public override Boolean CanSupplyValue(ParameterInfo pi,
IComponentContext context, out Func<Object> valueProvider)
{
valueProvider = null;
if (pi.Name == "services"
&& pi.ParameterType == typeof(Dictionary<String, IService>))
{
valueProvider = () =>
{
IEnumerable<Meta<IService>> services =
context.Resolve<IEnumerable<Meta<IService>>>();
return services.ToDictionary(m => (String)m.Metadata["Key"],
m => m.Value);
};
}
return valueProvider != null;
}
}