静态提供者字典设计

发布于 2025-01-08 07:18:07 字数 1269 浏览 0 评论 0原文

我正在重新考虑我们目前正在使用的 WCF 服务。我们将 XML 加载到各种数据库中进行了大量的工作。在某些情况下,我们可以将其存储为 XML 数据,而在其他情况下,我们需要将其存储为行集。

所以我正在重新设计这项服务以接受不同的提供商。我的第一个想法是经典的抽象工厂,但现在我有了疑问。本质上,服务类有一个操作契约方法,Load。但对我来说,每次调用 Load 时都更新提供程序实例似乎很愚蠢。

目前:

// Obviously incomplete example:
public class XmlLoaderService : IXmlLoaderService
{
    readonly IXmlLoaderFactory _xmlLoaderFactory;
    readonly IXmlLoader _xmlLoader;

    public XmlLoaderService()
    {
        _xmlLoader = _xmlLoaderFactory(ProviderConfiguration configuration);
    }

    public void Load(Request request)
    {
        _xmlLoader.Load(request);
    }
}

我正在考虑改为:

public class XmlLoaderService : IXmlLoaderService
{
    static readonly IDictionary<int, IXmlLoader> _providerDictionary;

    static public XmlLoaderService()
    {
        _providerDictionary = PopulateDictionaryFromConfig();
    }

    public void Load(Request request)
    {
        // Request will always supply an int that identifies the
        // request type, can be used as key in provider dictionary

        var xmlLoader = _providerDictionary[request.RequestType];
        xmlLoader.Load(request);
    }
}

这是一个好方法吗?我喜欢缓存提供者的想法,对我来说似乎更有效......不过,有时我倾向于忽略显而易见的事情。让我知道你的想法!

I'm rethinking a current WCF service we're using right now. We do A LOT of loading XML to various databases. In some cases, we can store it as XML data, and in others, we need to store it as rowsets.

So I'm redesigning this service to accept different providers. My first thought, classic abstract factory, but now I'm having my doubts. Essentially, the service class has one operation contract method, Load. But to me, it seems silly to new-up provider instances every time Load is called.

Currently:

// Obviously incomplete example:
public class XmlLoaderService : IXmlLoaderService
{
    readonly IXmlLoaderFactory _xmlLoaderFactory;
    readonly IXmlLoader _xmlLoader;

    public XmlLoaderService()
    {
        _xmlLoader = _xmlLoaderFactory(ProviderConfiguration configuration);
    }

    public void Load(Request request)
    {
        _xmlLoader.Load(request);
    }
}

I'm thinking about changing to:

public class XmlLoaderService : IXmlLoaderService
{
    static readonly IDictionary<int, IXmlLoader> _providerDictionary;

    static public XmlLoaderService()
    {
        _providerDictionary = PopulateDictionaryFromConfig();
    }

    public void Load(Request request)
    {
        // Request will always supply an int that identifies the
        // request type, can be used as key in provider dictionary

        var xmlLoader = _providerDictionary[request.RequestType];
        xmlLoader.Load(request);
    }
}

Is this a good approach? I like the idea of caching the providers, seems more efficient to me... though, I tend to overlook the obvious sometimes. Let me know your thoughts!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠北森屋 2025-01-15 07:18:07

为什么不能两者都使用?将您的依赖项传递到 Load 方法中,如果类型已缓存,则使用缓存的实例。

public void Load(Request request)
{
    // Request will always supply an int that identifies the
    // request type, can be used as key in provider dictionary

    IXmlLoader xmlLoader;
    if(_providerDictionary.ContainsKey(request.RequestType))
    {
        xmlLoader = _providerDictionary[request.RequestType];
    }
    else 
    {
        xmlLoader =  //acquire from factory
        _providerDictionary.Add(request.RequestType, xmlLoader);
    }
    xmlLoader.Load(request);
}

Why can't you use both? Pass in your dependency into the Load method and if the type is already cached use the cached instance.

public void Load(Request request)
{
    // Request will always supply an int that identifies the
    // request type, can be used as key in provider dictionary

    IXmlLoader xmlLoader;
    if(_providerDictionary.ContainsKey(request.RequestType))
    {
        xmlLoader = _providerDictionary[request.RequestType];
    }
    else 
    {
        xmlLoader =  //acquire from factory
        _providerDictionary.Add(request.RequestType, xmlLoader);
    }
    xmlLoader.Load(request);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文