My question is: How can I maximize encapsulation for wrapper-classes that interface with hardware.
The hardware is connected to a PC through COM-Ports or USB ports and I am either reading/writing to it directly or through an API. Either of which I want to wrap in a class.
I have been reading about software development and architecture in general (in C#), but I still do not have a feeling for how to architecture classes and objects around hardware.
Things that have been going through my head in this regard are the following aspects:
- When and how should I initialize the hardware, so that the clients of the encapsulating class need not worry about this (or the littlest possible), e.g.: Is it OK to initialize the hardware/port once at program startup and close it at the end (which is what I am currently doing) or should I always open and close the hardware/port as needed.
- Related to the first point: When and how should I release and destruct the hardware and their wrapping/related class.
- Should I store hardware state in the class or always query the device?
- How to handle hardware errors/exceptions (gracefully)? Right now, since I am initializing the hardware at startup, if some of it is not found, my program will not startup correctly. I would prefer it to startup and signal that some hardware was not found, so that it can be connected. But I cannot think of a decent architecture to achieve this.
- How can I make this fit in with dependency inversion: I am currently using factories to initialize the hardware and pass out the wrapper-classes through a DI-container. This however means that I am initializing only once at startup.
I realize that this is a broad topic with various subtopics, but these points are all closely related to each other and I am finding it difficult to find good and coherent information on this topic.
FYI: I am a C# developer, but I am interested in the general concepts.