具有静态注册的工厂模式

时间:2016-01-11 20:28:03

标签: c# factory static-constructor

尝试使用静态构造函数注册我的类型时遇到问题,使用以下工厂:

 public class Factory<T>
{
    public static Factory<T> Instance { get { return _instance; } }

    private static Factory<T> _instance = new Factory<T>();
    private Factory() { }
    static Factory() { }

    static Dictionary<string, Type> _registeredType = new Dictionary<string, Type>();

    public void Register(string id, T obj)
    {
        if (obj.GetType().IsAbstract || obj.GetType().IsInterface)
            throw new ArgumentException("Cannot create instance of interface or abstract class");

        _registeredType.Add(id, obj.GetType());
    }

    public T Create(string id, params object[] parameters)
    {
        Type type;

        if(!_registeredType.TryGetValue(id, out type))
            throw new UnsupportedShapeException(id);

        return (T)Activator.CreateInstance(type, parameters);
    }
} 

然后,如果我使用静态构造函数进行注册,则它不起作用:

    public interface IShape
{
    string print { get; }
}

public class Circle : IShape
{
    static Circle()
    {
        Factory<IShape>.Instance.Register("Circle", new Circle());
    }

    public string print
    {
        get
        {
            return "Circle";
        }
    }
}

我哪里错了?工厂似乎设置得很好,但我无法让ctor工作。欢呼声。

2 个答案:

答案 0 :(得分:3)

这不是答案,而是建议。首先,当您使用泛型类时,CLR会为每个实现创建类。这个类将有不同的静态变量,你不能为所有类使用一个工厂。好消息是您可以使用泛型方法而不是泛型类。而且您甚至不需要创建T对象的实例:

public class Factory
{
    public static Factory Instance { get { return _instance; } }

    private static Factory _instance = new Factory();
    private Factory() { }

    static Dictionary<string, Type> _registeredType = new Dictionary<string, Type>();

    public void Register<T>(string id)
    {
        var type = typeof(T);
        if (type.IsAbstract || type.IsInterface)
            throw new ArgumentException("Cannot create instance of interface or abstract class");

        _registeredType.Add(id, type);
    }

    public T Create<T>(string id, params object[] parameters)
    {
        Type type;

        if(!_registeredType.TryGetValue(id, out type))
            throw new UnsupportedShapeException(id);

        return (T) Activator.CreateInstance(type, parameters);
    }
} 

现在您可以使用Factory注册和解析对象:

Factory.Instance.Register<Circle>("Circle");
var firstCircle = Factory.Instance.Create<Circle>("Circle");
var secondCircle = Factory.Instance.Create<IShape>("Circle");

答案 1 :(得分:0)

我并不是100%确定我知道你要追求的是什么,但是,最好是制作包含工厂实例化的控制器和esqe类。但是,构造函数注入不适用于静态类或后代。

public static class StaticFactoryClassController
{
    private static readonly IStaticFactoryService service=AppServiceFactory.Instance.Create<IStaticFactoryService>();

    public static void DoSomething() 
    {
        Service srv = new StaticFactoryClassService(service);
        srv.DoSomething(); 
    }
}

然后你可以创建一个服务类 -

public class StaticFactoryClassService
{
    private readonly IStaticFactoryService service;

    public StaticFactoryClassService(IStaticFactoryService service)
    {
        this.service = service;
    }

    public void DoSomething()
    {
       this.service.DoSomething();
    }
}

最后你的绑定界面 -

public interface IStaticFactoryService
{
    DoSomething();
}  
相关问题