agentleFS
hiromailyhiromaily/go-crypto-wallet/.cursor/rules/go/di.mdc

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
  1. Distinctive:

    DO NOT: create internal objects within constructors:

    line 32
  2. Distinctive:

    Always depend on interfaces, not concrete types:

    line 84
  3. Distinctive:

    Wire dependencies: - Create and connect all objects

    line 151
  4. Distinctive:

    All dependencies flow inward toward the domain layer.

    line 188

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
di.mdc
---
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 &ETHWatch{...}
    }
}
```

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

Download now (ZIP)

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.