User Profile Sample - Three Variations
This sample demonstrates three different variations of implementing user profile management in a VS Code extension. Each variation shows a different approach to storing and retrieving user data.
The Three Variations
Variation 1: In-Memory Storage
Simplest approach - Data exists only during the extension's lifetime.
Characteristics:
- ✅ Fast and simple
- ❌ Data is lost when VS Code restarts
- ❌ Not suitable for persistent data
Use case: Temporary session data, cache
Commands:
User Profile: Set Profile (Variation 1 - In-Memory)- Set user profileUser Profile: Show Profile (Variation 1 - In-Memory)- Display current profile
Variation 2: Workspace State Storage
Workspace-specific persistence - Data persists per workspace.
Characteristics:
- ✅ Survives VS Code restarts
- ✅ Different workspaces can have different profiles
- ❌ Data is not shared across workspaces
Use case: Project-specific user preferences, workspace-specific settings
Commands:
User Profile: Set Profile (Variation 2 - Workspace State)- Set workspace profileUser Profile: Show Profile (Variation 2 - Workspace State)- Display workspace profile
Variation 3: Global State with Settings Integration
Global persistence with defaults - Data persists globally across all workspaces.
Characteristics:
- ✅ Survives VS Code restarts
- ✅ Shared across all workspaces
- ✅ Integrates with VS Code settings for default values
- ✅ Most feature-rich
Use case: User identity, global preferences, account information
Commands:
User Profile: Set Profile (Variation 3 - Global State)- Set global profileUser Profile: Show Profile (Variation 3 - Global State)- Display global profile
Settings:
userProfile.defaultName- Default user nameuserProfile.defaultEmail- Default user email
How to Run
- Run
npm installin terminal to install dependencies - Run the
Run Extensiontarget in the Debug View (or pressF5) - In the new VS Code window, open the Command Palette (
Ctrl+Shift+PorCmd+Shift+P) - Try any of the six commands to see each variation in action
Comparison Table
| Feature | Variation 1 | Variation 2 | Variation 3 |
|---|---|---|---|
| Storage Type | In-Memory | Workspace State | Global State |
| Persists on Restart | ❌ No | ✅ Yes | ✅ Yes |
| Shared Across Workspaces | N/A | ❌ No | ✅ Yes |
| Settings Integration | ❌ No | ❌ No | ✅ Yes |
| Complexity | Low | Medium | High |
| Best For | Session data | Workspace preferences | User identity |
VS Code API Used
vscode module
ExtensionContext.workspaceState- Workspace-specific storageExtensionContext.globalState- Global storageMemento- Key-value storage interfaceworkspace.getConfiguration- Access extension settingswindow.showInputBox- User inputwindow.showInformationMessage- Display messages
Contribution Points
contributes.commands- Register commandscontributes.configuration- Define settings
Key Learning Points
- In-Memory vs Persistent Storage: Understand when to use temporary vs persistent data storage
- Workspace vs Global State: Learn the difference between workspace-specific and global data
- Settings Integration: See how to provide default values through VS Code settings
- Memento API: Master the key-value storage interface for VS Code extensions
Implementation Details
Each variation is implemented as a separate class:
InMemoryProfileManager- Simple in-memory storageWorkspaceProfileManager- UsesworkspaceStateMementoGlobalProfileManager- UsesglobalStateMemento with settings fallback
All three variations implement the same interface for setting and getting user profiles, making it easy to compare their different storage mechanisms.