0

嗨,我的工厂代码是这样的。我不想将这些值直接存储在字典中,而是将这些值存储在 app.config 文件中。正如我在下面显示的。

public class HandlerFactory
    {
        private Dictionary<string, IHandler> _handlers = new Dictionary<string,IHandler>();
        public HandlerFactory()
        {
            _handlers.Add("AMOUNT", new AmountValidator());
            _handlers.Add("FLOW", new FlowValidator());
        }
        public IHandler Create(string key)
        {
            IHandler result;
            _handlers.TryGetValue(key, out result);
            return result;
        }
    }

我会将这些设置移动到我的配置文件中,如下所示。

  <?xml version="1.0" encoding="utf-8" ?>
        <configuration>
        <configSections>
            <section name="Indentifiers" type="System.Configuration.AppSettingsSection"/>
        </configSections>
        <Indentifiers>
            <add key="AMOUNT" value="AmountValidator" />
            <add key="FLOW" value="FlowValidator" />
        </Indentifiers>
    </configuration>

我正在做这样的事情,但我没有成功。不知道如何添加到字典

NameValueCollection settings = ConfigurationManager.GetSection("Indentifiers") as NameValueCollection;
                if (settings != null)
                {
                    foreach (string key in settings.AllKeys)
                    {
                        _handlers.Add(key.ToString(), settings[key].ToString()); <-- how to handle here
                    }
                 }
4

2 回答 2

0
public interface IHandler
{
    void Handle();
}

public sealed class HandlerFactory
{
    private readonly Dictionary<string, Type> _map = new Dictionary<string, Type>();

    public HandlerFactory()
    {
        var handlers = (NameValueCollection)ConfigurationManager.GetSection("Handlers");
        if (handlers == null)
            throw new ConfigurationException("Handlers section was not found.");
        foreach (var key in handlers.AllKeys)
        {
            var typeName = handlers[key] ?? string.Empty;
            // the type name must be qualified enough to be 
            // found in the current context.
            var type = Type.GetType(typeName, false, true);
            if (type == null)
                throw new ConfigurationException("The type '" + typeName + "' could not be found.");
            if (!typeof(IHandler).IsAssignableFrom(type))
                throw new ConfigurationException("The type '" + typeName + "' does not implement IHandler.");
            _map.Add(key.Trim().ToLower(), type);
        }
    }

    // Allowing your factory to construct the value 
    // means you don't have to write construction code in a million places.
    // Your current implementation is a glorified dictionary
    public IHandler Create(string key)
    {
        key = (key ?? string.Empty).Trim().ToLower();
        if (key.Length == 0)
            throw new ArgumentException("Cannot be null or empty or white space.", "key");
        Type type;
        if (!_map.TryGetValue(key, out type))
            throw new ArgumentException("No IHandler type maps to '" + key + "'.", "key");
        return (IHandler)Activator.CreateInstance(type);
    }
}
于 2012-07-28T06:50:52.327 回答
0

正如 ChaosPandion 所指出的, CreateInstance 方法应该很有用。假设您的两种处理程序类型都实现了 IHandler,并且它们位于正在运行的程序集中,

_handler.Add(key.ToString(), Activator.CreateInstance(null, settings[key].ToString()));

应该做的伎俩! http://msdn.microsoft.com/en-us/library/d133hta4.aspx 第一个参数是程序集的名称,null 默认为正在执行的程序集。

于 2012-07-27T19:20:26.747 回答