Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of pattern is this ('Provider')?

At work we are using a 'pattern' that I didn't really find in the GoF book (but that may be due to lack of competence in this matter, I just skimmed the patterns) and that I'm still doubting somewhat.

Say, if we have a multi-project solution containing a project DataAccess that manages, well, the data access. Then usually I see it having a structure like this:

Providers (Folder)
 - DataAccessProvider.cs

Interfaces (Folder)
 - IFileLoader.cs

Implementors (Folder)
 - FileLoader.cs

Here, FileLoader would be an internal implementation of the interface IFileLoader, and the provider looks like this:

public static class DataAccessProvider
{
  public static IFileLoader FileLoader
  {
    get { return new FileLoader(); }
  }
}

What kind of design pattern is this (if any), and what are its real uses besides masking the specific implementation of the IFileLoader interface?

And secondly, is this really 'good style'? I wonder, for example, what happens if there are many calls like

string content = DataAccessProvider.FileLoader.LoadContentFromFile("abc.txt");

This would call new FileLoader() whenever it is used. Isn't there a more elegant way to do a similar approach?

like image 590
InvisiblePanda Avatar asked Oct 18 '25 03:10

InvisiblePanda


1 Answers

In this example the DataAccessProvider is an example of a simple Factory Method (pattern). Normally you would have a method called GetFileLoader() or CreateFileLoader() instead of a Property version, but the result is the same.

The purpose of returning the IFileProvider instead of FileProvider is for Dependency Inversion, this way one could write other types of FileProvider and inject them into the application without needed to rework or recompile all of the object that depend on an IFileProvider. It's not about masking.

If one is concerned about how many instances of FileLoader are created, then one could use the Singleton pattern for that object. However, this is normally not an issue if the FileLoader is a lightweight object, since the CLR garbage collector will take care of that automatically for you.

like image 127
Silas Reinagel Avatar answered Oct 19 '25 18:10

Silas Reinagel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!