これは、データベース、ファイル、XMLなどに格納されているかどうかを公開しないことで、ビジネスロジックから永続性を抽象化する自動車リポジトリ/ DAOがある簡単な例です。次に、ビジネスクラスは、次のことができるようにインスタンスを挿入します。この場合-データベースに保存します。ただし、CarRepositoryを実装し、コードの他の部分に触れることなくアプリケーションにデータを保存するための他の手段を提供する他のクラスを作成することもできます。
永続層
リポジトリ/DAOのインターフェース
@Local
public interface CarRepository {
List<Car> findAllCars();
// Many other methods
}
リポジトリ(ドメイン駆動設計)またはデータアクセスオブジェクト
@Stateless
public class CarSqlRepository implements CarRepository {
@PersistenceContext(unitName = "MyUnit")
private EntityManager entityManager;
public List<Car> findAllCars() {
}
// Many other methods
}
サービス/ビジネスレイヤー
@Stateless
public class CarService {
@Inject
private CarRepository carRepository;
public List<Car> findAllCars() {
return carRepository.findAllCars();
}
// Many other methods
}