Implementing an API with a pluggable version of the backend in .NET (F #)

I am trying to understand how to implement plug-in components or a service provider interface in the .NET world. I suspect that I simply do not know the appropriate terminology for the search.

In particular, I play with a class Matrixthat has different servers. In its simplest matrix, two-parameter get and set methods and a constructor are presented. Implementation is not important to the end user. For example, depending on the size of the matrix, the matrix may be supported by an array in memory, a file, or a distributed keystore. I would like to hide the backend implementation and allow third parties to provide new backend implementations.

An ideal API called IronPython, say, could be something like

a = matrix(data = 0, rows = 1000, cols = 10, backend = 'file://test.txt')
a[100, 2] = 1
print a[100, 2]

What should I read to understand the pattern for this type of problem?

I play F # and IronPython, but I don't believe this question is specific to any particular .Net language.

+3
source share
3 answers

Yes, you can create an interface IMatrixand a specific class that implements it, for example:

type IMatrix =
    abstract Item : (int * int) -> single with get, set

type ConcreteMatrix (data:single[,])=
    interface IMatrix with
        member t.Item with get((x,y)) = data.[x,y]
                      and set((x,y)) value = do data.[x,y] <- value

let printCoordinate (x, y) (matrix : #IMatrix) =
    printf "%A" matrix.[x, y]
+6
source

You want to create an interface that is a contract that is a matrix. You will probably end up calling it something like IMatrix. Then create several implementations of this interface: MemoryMatrix, FileMatrix, DistributedKeyValueMatrix. When you pass a specific implementation in your code, just refer to the interface instead of the specific type.

System.Collections.Generic, , ICollection, , .

: MatrixFactory, , . Inversion of Control , .

+3

@Stringer @Khalid .

,

  • - API, ,
  • - for different classes (new ArrayMatrix, new FileMatrix) or overloads (CreateMatrix (...), CreateMatrix (..., string filename)) or just data-based logic (MakeMatrix (..., string backend), where backend is this is the name of the file, except that "array" or nullmay mean something else) is enough
+3
source

Source: https://habr.com/ru/post/1732270/


All Articles