go-crypto-wallet / go
Cursor rule128 starsChanged 6 months agoMITView on GitHub
Dependency Injection
We split this di.mdc into the rules it gives an agent
4 rules. About 0 are common, 4 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 32
DO NOT: create internal objects within constructors:
- Distinctive: line 84
Always depend on interfaces, not concrete types:
- Distinctive: line 151
Wire dependencies: - Create and connect all objects
- Distinctive: line 188
All dependencies flow inward toward the domain layer.
≈ “Common” is an estimate: at least 1 in 5 of the 6,350 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
---
description: Dependency Injection
globs:
- "internal/di/container.go"
- "pkg/di/container.go"
alwaysApply: false
---
# Dependency Injection
## Overview
Rules for the Dependency Injection (DI) layer in go-crypto-wallet.
The DI layer is responsible for wiring all dependencies and should follow Clean Architecture principles.
## Applicable Files
- `internal/di/container.go` - Application DI container
- `pkg/di/container.go` - Infrastructure/pkg DI container
## Core Principles
### 1. Pure Constructor Pattern (Mandatory)
Constructors must be **pure assembly functions**. They only:
- Receive dependencies as parameters
- Assign parameters to struct fields
- Return the constructed instance
**DO NOT** create internal objects within constructors:
```go
// ✅ GOOD: Pure constructor - just assembles the struct
func NewCreateTransactionUseCase(
btcClient apibtc.Bitcoiner,
dbConn *sql.DB,
addrRepo repowatch.AddressRepositorier,
txRepo repowatch.BTCTxRepositorier,
) watchusecase.CreateTransactionUseCase {
return &createTransactionUseCase{
btcClient: btcClient,
dbConn: dbConn,
addrRepo: addrRepo,
txRepo: txRepo,
}
}
// ❌ BAD: Constructor creates internal dependencies
func NewCreateTransactionUseCase(
conf *config.Config,
) watchusecase.CreateTransactionUseCase {
// DON'T create dependencies inside constructor
btcClient := bitcoin.NewBitcoin(conf)
dbConn := mysql.NewConnection(conf.Database)
return &createTransactionUseCase{
btcClient: btcClient,
dbConn: dbConn,
}
}
```
### 2. No Factory Pattern for Domain/Application Objects
Factory patterns add unnecessary indirection. Use direct constructor calls instead:
```go
// ✅ GOOD: Direct constructor call
useCase := keygenusecasebtc.NewImportPrivateKeyUseCase(
btcClient,
accountKeyRepo,
)
// ❌ BAD: Factory pattern
factory := usecaseFactory.New()
useCase := factory.CreateImportPrivateKeyUseCase()
```
**Exception**: Factory pattern is acceptable only for infrastructure components that require runtime type selection (e.g., key generators based on address type).
### 3. Interface-Based Dependencies
Always depend on interfaces, not concrete types:
```go
// ✅ GOOD: Depends on interface
type createTransactionUseCase struct {
btcClient apibtc.Bitcoiner // interface
addrRepo repowatch.AddressRepositorier // interface
}
// ❌ BAD: Depends on concrete type
type createTransactionUseCase struct {
btcClient *apibtcimpl.Bitcoin // concrete type
addrRepo *watchmysql.AddressRepository // concrete type
}
```
### 4. Lazy Initialization in Container
The DI container may use lazy initialization for expensive resources:
```go
// ✅ GOOD: Lazy initialization with caching in container
func (c *container) newBTC() apibtc.Bitcoiner {
if c.btc == nil {
var err error
c.btc, err = bitcoin.NewBitcoin(
c.newRPCClient(),
&c.conf.Bitcoin,
c.conf.CoinTypeCode,
)
if err != nil {
panic(err)
}
}
return c.btc
}
```
This pattern is **only allowed in the DI container**, not in domain/application code.
### 5. Single Responsibility per Constructor
Each constructor creates exactly one type of object:
```go
// ✅ GOOD: One constructor, one responsibility
func NewBTCWatch(...) *BTCWatch { ... }
func NewETHWatch(...) *ETHWatch { ... }
// ❌ BAD: One constructor with branching logic
func NewWatch(coinType string, ...) Watch {
switch coinType {
case "BTC":
return &BTCWatch{...}
case "ETH":
return ÐWatch{...}
}
}
```
Branching logic belongs in the DI container, not in constructors.
## DI Container Responsibilities
The DI container (`internal/di/container.go`) should:
1. **Hold configuration** - Store config and shared state
2. **Wire dependencies** - Create and connect all objects
3. **Manage lifecycles** - Handle singleton-like instances
4. **Handle coin-type switching** - Branch logic based on coin type
```go
// ✅ GOOD: Coin-type switching in container
func (c *container) NewKeygener() wallets.Keygener {
switch {
case domainCoin.IsBTCGroup(c.conf.CoinTypeCode):
return c.newBTCKeygener()
case domainCoin.IsETHGroup(c.conf.CoinTypeCode):
return c.newETHKeygener()
default:
panic(fmt.Sprintf("coinType[%s] not implemented", c.conf.CoinTypeCode))
}
}
```
## Naming Conventions
| Pattern | Convention | Example |
|---------|------------|---------|
| Public constructor | `New` + TypeName | `NewBTCWatch` |
| Private container method | `new` + TypeName | `newBTCWalleter` |
| Use case constructor | `New` + UseCaseName | `NewCreateTransactionUseCase` |
| Repository constructor | `New` + RepoName | `NewAddressRepositorySqlc` |
## Dependency Flow
```
cmd/
└── calls internal/di/container.go
└── creates Use Cases (internal/application/usecase/)
└── with Repositories (internal/application/ports/repository/)
└── implemented by Infrastructure (internal/infrastructure/repository/)
```
All dependencies flow **inward** toward the domain layer.
## Anti-Patterns to Avoid
| Anti-Pattern | Why It's Bad | Alternative |
|--------------|--------------|-------------|
| Service Locator | Hidden dependencies, hard to test | Constructor injection |
| God Container | Single container does everything | Split by layer/feature |
| Circular Dependencies | Coupling, initialization issues | Refactor interfaces |
| Config in Constructor | Hard to test, coupling | Pass config values directly |
## Testing Implications
Pure constructors enable easy testing:
```go
// Test can inject mocks directly
func TestCreateTransaction(t *testing.T) {
mockBTC := &mockBitcoiner{}
mockRepo := &mockAddressRepo{}
useCase := NewCreateTransactionUseCase(
mockBTC,
nil, // dbConn not needed for this test
mockRepo,
// ...
)
// Test with full control over dependencies
}
```
## Related Rules (in this directory)
- @.claude/rules/go/conventions.md - Go coding conventions
- @.claude/rules/go/usecase.md - Use case patterns
- @.claude/rules/go/repository.md - Repository patterns
## Related Documentation
- @ARCHITECTURE.md - System architecture
- @docs/guidelines/coding-conventions.md - Coding standards
- @internal/AGENTS.md - Internal package guidelines
Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.
Of the Cursor rule files we read, the 3,978 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository62 other files it gives its agents.
- AGENTS.mdAGENTS.md
- internal/AGENTS.mdAGENTS.md
- pkg/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- internal/CLAUDE.mdCLAUDE.md
- pkg/CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- .cursor/rules/agent-files.mdcCursor rule
- .cursor/rules/apps/bun.mdcCursor rule
- .cursor/rules/apps/eth-contracts.mdcCursor rule
- .cursor/rules/apps/xrp-server.mdcCursor rule
- .cursor/rules/database/atlas-hcl.mdcCursor rule
- .cursor/rules/database/sqlc.mdcCursor rule
- .cursor/rules/documentation-language.mdcCursor rule
- .cursor/rules/general.mdcCursor rule
- .cursor/rules/github-actions.mdcCursor rule
- .cursor/rules/go/conventions.mdcCursor rule
- .cursor/rules/go/go-path.mdcCursor rule
- .cursor/rules/go/go-test.mdcCursor rule
- .cursor/rules/internal/application-layer.mdcCursor rule
- .cursor/rules/internal/btc-shared.mdcCursor rule
- .cursor/rules/internal/clean-architecture.mdcCursor rule
- .cursor/rules/internal/cli-structure.mdcCursor rule
- .cursor/rules/internal/domain-layer.mdcCursor rule
- .cursor/rules/internal/dto.mdcCursor rule
- .cursor/rules/internal/infrastructure-layer.mdcCursor rule
- .cursor/rules/internal/interface-adapters-layer.mdcCursor rule
- .cursor/rules/internal/internal-interface.mdcCursor rule
- .cursor/rules/internal/mockery.mdcCursor rule
- .cursor/rules/internal/repository.mdcCursor rule
- .cursor/rules/internal/usecase.mdcCursor rule
- .cursor/rules/proto.mdcCursor rule
- .cursor/rules/README.mdCursor rule
- .cursor/rules/security.mdcCursor rule
- .cursor/rules/shell-script.mdcCursor rule
- .cursor/rules/ssot.mdcCursor rule
- .cursor/rules/task-context-loading.mdcCursor rule
- .cursor/rules/typescript.mdcCursor rule
- .cursor/rules/yaml.mdcCursor rule
- llms.txtllms.txt
- bch-developmentSkill
- btc-terminologySkill
- db-migrationSkill
- devopsSkill
- docs-ssotSkill
- docs-updateSkill
- fix-pr-reviewSkill
- github-issue-creationSkill
- git-workflowSkill
- go-developmentSkill
- knowledge-gapSkill
- label-context-mappingSkill
- makefile-updateSkill
- mockerySkill
- openspec-apply-changeSkill
- openspec-archive-changeSkill
- openspec-exploreSkill
- openspec-proposeSkill
- shell-scriptsSkill
- solidity-developmentSkill
- The first 60 shown.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

