Abstraction

Despite the fact that each platform operates in its own way, there is still some commonality that exists between them. For example, each platform’s API exposes a way to ask the device for its current location. The code to accomplish this is different for each of them, meaning it cannot be shared, but the end result is the same. In most cases, your business layer won’t necessarily care how the data is obtained; it just wants to know what that data is. By abstracting out the implementation from the definition, the shared layer can still make use of the results of these features without having to know anything about how they are implemented.

In C#, this typically means creating an interface. An interface is similar to a class definition, except that it only contains the signatures for its properties and methods rather than including the implementation as well. Any class that implements an interface must provide implementations for everything defined in that interface. A single class can also implement multiple interfaces.

Since an interface only contains the definitions, it forms a contract between the client and the implementation. The shared layer can work against that contract without needing to be tied to a particular implementation, and each platform’s application can then provide its own implementation of that contract. This allows for a better separation of concerns, keeping the core application logic in one shared place, decoupled from platform specifics.

For the parking ...

Get Mobile Development with C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.