私は何日もこれを理解しようとしてきましたが、ASP.NET MVC を使用したこの特定の主題に関する情報はほとんどないようです。私は何日もグーグルで検索してきましたが、この特定の問題について何も理解できていません。
私は3層のプロジェクトを持っています。ビジネス、DAL、および UI/Web レイヤー。DAL には、dbcontext、リポジトリ、および作業単位があります。ビジネス レイヤーには、すべてのインターフェイスと EF モデルを含むドメイン レイヤーがあります。ビジネス レイヤーには、EF モデル用の DTO を含むサービス レイヤーと、リポジトリにアクセスする汎用リポジトリ サービスもあります。この写真はそれを説明するのに役立つはずです。
私の問題は、DTO を使用してビジネス層からデータを転送する方法を理解できないように見えることです。
DTO のサービス クラスを作成しました。ImageDTO とモデルがあり、イメージ アンカーについても同じです。DTO ごとにサービス クラスを作成しました。イメージ サービスとアンカー サービスがあります。これらのサービスはリポジトリ サービスを継承し、現時点では独自のサービスを実装しています。しかし、それは私が得た限りです。これらのサービスには、IoC を介して IUnitOfWork インターフェイスを受け取るコンストラクターがあるため、私はほとんど立ち往生してしまいました。
UI から直接サービスを参照すると、すべてが正常に機能しますが、DTO を使用してサービス層から UI 層へ、またはその逆の両方でデータを送信する方法がわかりません。
私のサービス層:
ビジネス/サービス/DTO
public class AnchorDto
{
public int Id { get; set; }
public int x1 { get; set; }
public int y1 { get; set; }
public int x2 { get; set; }
public int y2 { get; set; }
public string description { get; set; }
public int imageId { get; set; }
public int targetImageId { get; set; }
public AnchorDto(int Id, int x1, int y1, int x2, int y2, string description, int imageId, int targetImageId)
{
// Just mapping input to the DTO
}
}
public class ImageDto
{
public int Id { get; set; }
public string name { get; set; }
public string title { get; set; }
public string description { get; set; }
public virtual IList<AnchorDto> anchors { get; set; }
public ImageDto(int Id, string name, string title, string description, IList<AnchorDto> anchors )
{
// Just mapping input to DTO
}
}
事業・サービス・サービス
public class RepoService<TEntity> : IRepoService<TEntity> where TEntity : class
{
private IRepository<TEntity> repo;
public RepoService(IUnitOfWork repo)
{
this.repo = repo.GetRepository<TEntity>();
}
public IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
return repo.Get(filter, orderBy, includeProperties);
}
public TEntity GetByID(object id)
{
return repo.GetByID(id);
}
public void Insert(TEntity entity)
{
repo.Insert(entity);
}
public void Delete(object id)
{
repo.Delete(id);
}
public void Delete(TEntity entityToDelete)
{
repo.Delete(entityToDelete);
}
public void Update(TEntity entityToUpdate)
{
repo.Update(entityToUpdate);
}
}
Image Service の IImageService インターフェイスは、何を実装する必要があるかを理解するまで、現在空です。
public class ImageService : RepoService<ImageModel>, IImageService
{
public ImageService(IUnitOfWork repo)
: base(repo)
{
}
}
現時点では、コントローラーが実際には機能しておらず、サービス層を使用していないため、それらを含めないことにしました。この問題を整理したら、自動マッパーを使用して DTO を ViewModel にマップする予定です。
だから今、私がこれを理解できるように、私が見逃しているその考えを私に与えるのに十分な知識のある人をお願いします?