
# @Repository

`@Repository()` marks a class as a bean that stores and loads data.

## Why

It does exactly what [@Component](component.md) does. The name tells the reader that the class is the data-access layer: a database, a file, a remote API, an in-memory map.

Keeping data access in its own beans lets a test or a profile replace it with a fake one, without touching the services that use it.

## Example

```dart
import 'package:spring_core_dart/spring_core_dart.dart';

class Order {
  Order(this.id, this.total);
  final int id;
  final double total;
}

// The interface the services depend on.
abstract interface class Orders {
  Order? findById(int id);
  void save(Order order);
}

// In-memory storage. Another implementation could use a database;
// the services do not change.
@Repository()
class InMemoryOrders implements Orders {
  final _orders = <int, Order>{};

  @override
  Order? findById(int id) => _orders[id];

  @override
  void save(Order order) => _orders[order.id] = order;
}
```

The bean is registered under `InMemoryOrders` and under `Orders`, so a service can ask for `Orders`.

## Parameters

| Name | Type | Default | Meaning |
|---|---|---|---|
| `name` (positional) | `String?` | `null` | The bean name. `null` gives the class name in lowerCamelCase. |

## In Spring (Java)

```java
@Repository
class InMemoryOrders implements Orders {
  public Order findById(int id) { ... }
}
```

Spring also translates database exceptions of `@Repository` beans into its `DataAccessException` hierarchy. This package does not.

## See also

- [@Component](component.md)
- [@Service](service.md)
- [@Profile](profile.md): swap the repository by environment
- [@Primary](primary.md)
