DataConnection System
BeepDataConnection component -- design-time connection management, IBeepService integration, BeepConnectionRepository with Project/User/Machine storage scope, and ConnectionsChanged event
Table of Contents
Overview
Namespace: TheTechIdea.Beep.Winform.Controls
Inheritance: System.ComponentModel.Component
Designer: BeepDataConnectionDesigner (design-time component tray support)
Associated enum: ConnectionStorageScope { Project, User, Machine }
BeepDataConnection is a design-time Component for managing database connections. It wraps IBeepService for data operations, persists connections using BeepConnectionRepository across three storage scopes, and provides change notification via the ConnectionsChanged event. At design time, it appears in the Visual Studio component tray below the form designer.
Key Properties
| Property | Type | Category | Description |
|---|---|---|---|
| BeepService | IBeepService? | Runtime | Service provider for data operations. Read-only. Owned internally or shared. |
| AppRepoName | string | Persistence | Repository name for connection storage. Default: "BeepPlatformConnections". Browsable. |
| DirectoryPath | string | Persistence | Filesystem directory for connection file storage. Default: "". Browsable. |
| DataConnections | List<ConnectionProperties> | Runtime | Collection of managed database connection properties (server, database, credentials, driver). |
Constructor Behavior
The constructor executes a four-step initialization:
- InitializeBeepService() -- Creates or resolves an
IBeepServiceinstance. If the component owns the service (no external injection), sets_ownsBeepService = truefor cleanup responsibilities. - DataConnections initialization -- Creates an empty
List<ConnectionProperties>. - InitializeRepository() -- Creates a
BeepConnectionRepositoryusingAppRepoNameandDirectoryPath. Subscribes to repository change events via_repositoryChangedHandler. - ReloadConnections() -- Loads persisted connections from the repository into the
DataConnectionslist.
Both AppRepoName and DirectoryPath property setters call OnStorageConfigurationChanged() to reinitialize the repository when persistence settings change.
Connection Repository
BeepConnectionRepository handles the store/load cycle for connection definitions:
- Store -- Serializes
DataConnectionslist to a file in the configured directory - Load -- Deserializes connection definitions from file to
DataConnectionslist - Scope resolution -- File location depends on
ConnectionStorageScope(Project, User, or Machine) - Change notification -- Fires events when connections change on disk, triggering
ConnectionsChanged
Storage Scopes
| Scope | Storage Location | Use Case |
|---|---|---|
| Project | Project directory relative to executable/solution | Connections specific to the current application project |
| User | User's appdata/local folder | Per-user connections, persists across projects |
| Machine | Common application data folder | Machine-wide shared connections, all users |
IBeepService Integration
The component wraps IBeepService for database operations:
- Creates data sources from connection properties
- Provides connection testing/validation
- Manages the service lifecycle (creation, disposal)
- The
_ownsBeepServiceflag determines whether Dispose should clean up the service
Events
| Event | Type | Fires When |
|---|---|---|
| ConnectionsChanged | EventHandler? | Connection list is modified (add, remove, update) or repository detects changes |
Design-Time
The BeepDataConnectionDesigner provides the Visual Studio design-time experience:
- Appears in the component tray below the form (non-visual component)
- Properties (
AppRepoName,DirectoryPath) browsable in the Properties window under "Persistence" category - Connection list editable via collection editor at design time
Code Examples
var dc = new BeepDataConnection();
dc.AppRepoName = "MyAppConnections";
dc.DirectoryPath = @"C:\MyApp\Data";
// Add a connection
dc.DataConnections.Add(new ConnectionProperties
{
Name = "MainDB",
Server = "localhost",
Database = "MyDatabase",
Driver = "SQLServer"
});
dc.ConnectionsChanged += (s, e) =>
Console.WriteLine("Connections modified");
// Change storage scope at runtime
// ConnectionStorageScope enum: Project, User, Machine