我有3个具有相同接口的类:
cppTopicGenerator : TopicGenerator
phpTopicGenerator : TopicGenerator
javaTopicGenerator : TopicGenerator
然后我为它们创建了一个立面,在其中尝试使用它们,如下所示:
public class MyFacade
{
private readonly List<TopicGenerator> generators;
public void BusinessLogic(string extension)
{
MyParser(extenstion).generate();
}
private TopicGenerator MyParser(string extension)
{
foreach (var generator in generators)
{
if (generator.Accept(extension))
{
return generator;
}
}
throw new NotFoundException($"Generator for {extension} doesnt exist.");
}
}
问题是,应该如何创建此生成器列表?我需要手动创建所有生成器的列表,例如:
private readonly List<TopicGenerator> generators = new List(
{
new CppTopicGenerator()
new PhpTopicGenerator()
new JavaTopicGenerator()
});
还是可以自动注入它们?
答案 0 :(得分:2)
绝对可以,您可以通过IoC容器注入它。但是,这取决于您的需求。如果您的Generator
太复杂并且需要大量引用,则可以使用IoC(例如Autofac),但是如果它很轻巧,我希望手动创建它。
示例(控制台应用程序中的所有示例): 1)手动
using System;
using System.Collections.Generic;
namespace Test
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var service = new MyFacade();
service.MyParser("foo");
service.MyParser("bar");
service.MyParser("foobar");
}
}
public class MyFacade
{
private readonly IEnumerable<IGenerator> _generators;
public MyFacade()
{
_generators = new List<IGenerator>()
{
new CppTopicGenerator(),
new PhpTopicGenerator(),
new JavaTopicGenerator(),
};
}
public IGenerator MyParser(string extension)
{
foreach (var generator in _generators)
{
if (generator.Accept(extension))
{
return generator;
}
}
throw new NotFoundException($"Generator for {extension} doesnt exist.");
}
}
public interface IGenerator
{
bool Accept(string extension);
}
public class CppTopicGenerator : IGenerator
{
public bool Accept(string extension)
{
Console.WriteLine("CppTopicGenerator checking executed");
return extension == "foo";
}
}
public class PhpTopicGenerator : IGenerator
{
public bool Accept(string extension)
{
Console.WriteLine("PhpTopicGenerator checking executed");
return extension == "bar";
}
}
public class JavaTopicGenerator : IGenerator
{
public bool Accept(string extension)
{
Console.WriteLine("JavaTopicGenerator checking executed");
return extension == "foobar";
}
}
}
2)Autofac:
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()).As<IGenerator>();
builder.RegisterType<MyFacade>();
var container = builder.Build();
using (var scope = container.BeginLifetimeScope())
{
var service = scope.Resolve<MyFacade>();
service.MyParser("foo");
service.MyParser("bar");
service.MyParser("foobar");
}
}
}
public class MyFacade
{
private readonly IEnumerable<IGenerator> _generators;
public MyFacade(IEnumerable<IGenerator> generators)
{
_generators = generators;
}
public IGenerator MyParser(string extension)
{
foreach (var generator in _generators)
{
if (generator.Accept(extension))
{
return generator;
}
}
throw new NotFoundException($"Generator for {extension} doesnt exist.");
}
}
public interface IGenerator
{
bool Accept(string extension);
}
public class CppTopicGenerator : IGenerator
{
public bool Accept(string extension)
{
Console.WriteLine("CppTopicGenerator checking executed");
return extension == "foo";
}
}
public class PhpTopicGenerator : IGenerator
{
public bool Accept(string extension)
{
Console.WriteLine("PhpTopicGenerator checking executed");
return extension == "bar";
}
}
public class JavaTopicGenerator : IGenerator
{
public bool Accept(string extension)
{
Console.WriteLine("JavaTopicGenerator checking executed");
return extension == "foobar";
}
}
答案 1 :(得分:1)
您可以从程序集中反映它们,然后通过Activator实例化它们:
echo
这样,您将找到实现TopicGenerator的所有类,并且如果创建新类,则无需手动添加它们。
答案 2 :(得分:1)
您可以使用reflection来实现。
private static readonly List<TopicGenerator> generators =
Assembly.GetExecutingAssembly().GetTypes()
.Where(x => x.ImplementedInterfaces.Contains(typeof(TopicGenerator)))
.Select(x => (TopicGenerator)Activator.CreateInstance(x))
.ToList();
答案 3 :(得分:1)
您可以使用以下命令生成列表。
var list = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(p => typeof(TopicGenerator).IsAssignableFrom(p) && p.IsClass && !p.IsAbstract)
.Select(x => (TopicGenerator)Activator.CreateInstance(x)).ToList<TopicGenerator>();
它的作用是,它反映并迭代汇编中的类型,并检查是否实现了特定的接口。 !p.IsAbstract
将确保未列出抽象类。