简体   繁体   中英

What is the difference between Provider and Resolver

I often need a class/service that will give me some data trough fetching it from a DB, transforming an existing data structure or do both internally but I sometimes have a difficulty naming them properly.

I am currently working with Sylius and they are using classes/services with suffixes such as Checker , Applicator , Processor ... I have clear understanding of these names and their implications as to what and how they are doing things. But there are also suffixes Provider and Resolver and I have a difficulty differentiating between them. I don't understand the exact differences of their naming.

What I observed is:

  • Provider : fetching data that are not yet available (internally fetching data from DB or external API)
  • Resolver : I already have a bunch of data (and I don't need any additional data) and I need to filter, transform or get some subset of it.

Is there some convention or design pattern to names Resolver and Provider ? Am I somewhat right here? Or is there more nuance to this naming?

In my view, patterns are not depend on technology or language, so this article can be applied here :

Content Providers provide an interface, eg for publishing and consuming data

and:

Content Resolver resolves a publishing and consuming data to a specific Content provider. The Content Resolver includes the CRUD (create, read, update, delete) methods corresponding to the abstract methods (insert, query, update, delete) in the Content Provider class.

UPDATE

Provider is an abstraction that can be implemented by concrete providers. Eg, there is DataProvider and DataProvider is an abstraction. So we want concrete implementations of SqlServerProvider, PostgreProvider, OracleProvider.

Let me show an example via C#:

public interface IDataProvider
{
    string GetById();
}

public class SqlServerProvider : IDataProvider
{
    public string GetById()
    {
        return "Data retrieved with SqlServerProvider";
    }
}

public class PostgreProvider : IDataProvider
{
    public string GetById()
    {
        return "Data retrieved with PostgreProvider";
    }
}


public class OracleProvider : IDataProvider
{
    public string GetById()
    {
        return "Data retrieved with OracleProvider";
    }
}

Then we need to resolve the above dependenies to use them. But how? We can create DataResolver :

public enum DataProviderType
{
    SqlServer, Posgre, Oracle
}

public class DataResolver
{
    private Dictionary<DataProviderType, IDataProvider> _dataProviderByType =
        new Dictionary<DataProviderType, IDataProvider>()
        {
            { DataProviderType.SqlServer, new SqlServerProvider() },
            { DataProviderType.Posgre, new PostgreProvider() },
            { DataProviderType.Oracle, new OracleProvider() },
        };

    public IDataProvider Resolve(DataProviderType dataProviderType) 
    {
        return _dataProviderByType[dataProviderType];
    }    
}

and then we can run the above code like this:

DataResolver dataResolver = new DataResolver();
string someValue = dataResolver.Resolve(DataProviderType.SqlServer).GetById();
Console.WriteLine(someValue); // OUTPUT: Data retrieved with SqlServerProvider

See more examples of code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM