2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

.NET MAUIとCleanArchitectureで簡単なアプリを作成してみる4~実装3~

Last updated at Posted at 2024-01-22

.NET MAUIとCleanArchitectureで簡単なアプリを作成してみる4~実装2~の続き

Interactorを実装する

フォルダ構成はフォルダ構成を決める(MauiApp1.Domain)の通り

Createユースケースを実装する

HealthCreateInteractor.cs
    public class HealthCreateInteractor : IHealthCreateUseCase
    {
        private readonly IHealthRepository _healthRepository;

        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="healthRepository"></param>
        public HealthCreateInteractor(IHealthRepository healthRepository)
        {
            this._healthRepository = healthRepository;
        }

        /// <summary>
        /// 体調情報を作成する
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public HealthCreateResponse Handle(HealthCreateRequest request)
        {
            this._healthRepository.Save(new Health(request.Id, request.RecordDate.Date, request.HeartNumber));

            var health = this._healthRepository.Find(request.RecordDate.Date);

            if (health == null) throw new Exception("体調情報の作成に失敗しました");

            return new HealthCreateResponse(health.Id);
        }
    }

GetListユースケースを実装する

HealthGetListInteractor.cs
    public class HealthGetListInteractor : IHealthGetListUseCase
    {
        private readonly IHealthRepository _healthRepository;

        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="healthRepository"></param>
        public HealthGetListInteractor(IHealthRepository healthRepository)
        {
            this._healthRepository = healthRepository;
        }

        /// <summary>
        /// 体調情報リストを取得する
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public HealthGetListResponse Handle(HealthGetListRequest request)
        {
            var healths = this._healthRepository.Find(request.StartDate, request.EndDate);

            return new HealthGetListResponse(
                healths.Select(h => new HealthDto(h.Id, h.RecordDate, h.HeartNumber)).ToArray());
        }
    }

参考にしたURL

実践クリーンアーキテクチャ

まとめ

今回はInteractorを実装しました。
次はModel、ViewModel、Viewを実装します。
.NET MAUIとCleanArchitectureで簡単なアプリを作成してみる4~実装4~

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?