如何使用Fluent API处理存储库模式中的相关数据? MyProject.Repo / Car.cs MyProject.Repo / Tag.cs​​ MyProject.Repo / MyContext.cs MyProject.Repo / Repository.cs MyProject.Repo / IRepository.cs MyProject.Services/ICarService.cs MyProject.Services/CarService.c

我需要处理相关数据,我一直在寻找Fluent API,以使此操作更容易一些,我在搜索过程中从未见过的一个方面是如何使用这种方法。存储库模式。在实际情况中将这些概念结合在一起时,我需要一些帮助。我查看了所有文档,但是,再次没有任何内容真正指向存储库模式,并且我还发现某些代码看起来过于膨胀,我想尽可能简化以保持我的应用程序干净。

所以,这是我目前的设置。我有一个ASP.NET Core 3.1 Web应用程序,在一个解决方案中有4个项目。

  • MyProject.UI
  • MyProject.Data
  • MyProject.Repo
  • MyProject.Services

我想在两个实体CarTag之间建立一对多关系,其中一个Car可以有多个Tags。我已经关注了documentation,这是我到目前为止所做的

MyProject.Repo / Car.cs

using System.Collections;
using System.Collections.Generic;

namespace MyProject.Data
{
    public class Car : BaseEntity
    {
        public string CarName { get; set; }
        public ICollection<Tag> Tags { get; }
    }
}

MyProject.Repo / Tag.cs​​

namespace MyProject.Data
{
    public class Tag : BaseEntity
    {
        public string TagName { get; set; }

        public int CurrentCarId { get; set; }
        public Car Car { get; set; }
    }
}

在数据库上下文中,我已经在OnmodelCreating下进行了必要的更改,这些更改定义了期望的关系。

MyProject.Repo / MyContext.cs

using MyProject.Data;
using microsoft.AspNetCore.Identity.EntityFrameworkCore;
using microsoft.EntityFrameworkCore;

namespace MyProject.Repo
{
    public class MyContext : IdentityDbContext<ApplicationUser>
    {
        public MyContext(DbContextOptions<MyContext> options)
            : base(options) { }

        protected override void OnmodelCreating(ModelBuilder builder)
        {
            builder.Entity<Car>()
                .HasMany<Tag>(x => x.Tags)
                .HasForeignKey(s => s.CurrentCarId);

            base.OnmodelCreating(builder);
        }
        public DbSet<Car> Cars { get; set; }
        public DbSet<Tag> Tags { get; set; }
    }
}

为清楚起见,我包括了存储库模式的代码和设置。以下代码演示了如何设置和使用它。

MyProject.Repo / Repository.cs

using MyProject.Data;
using microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MyProject.Repo
{
    public class Repository<TEntity> : IRepository<TEntity>
                    where TEntity : BaseEntity
    {
        private readonly MyContext _dbContext;
        private readonly DbSet<TEntity> entities;
        string errorMessage = string.Empty;

        public Repository(MyProject context)
        {
            this._dbContext = context;
            entities = context.Set<TEntity>();
        }
        public IEnumerable<TEntity> Getall()
        {
            return entities.AsEnumerable();
        }

        public TEntity Get(int id)
        {
            return entities.SingleOrDefault(s => s.Id == id);
        }
        public void Insert(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            entities.Add(entity);
            _dbContext.SaveChanges();
        }

        public void Update(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            _dbContext.Update(entity);
            _dbContext.SaveChanges();
        }

        public void Delete(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            entities.Remove(entity);
            _dbContext.SaveChanges();
        }
        public void Remove(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            entities.Remove(entity);
        }

        public void SaveChanges()
        {
            _dbContext.SaveChanges();
        }
    }
}

MyProject.Repo / IRepository.cs

using MyProject.Data;
using System.Collections.Generic;

namespace MyProject.Repo
{
    public interface IRepository<TEntity>
                where TEntity : BaseEntity
    {
        IEnumerable<TEntity> Getall();
        TEntity Get(int id);
        void Insert(TEntity entity);
        void Update(TEntity entity);
        void Delete(TEntity entity);
        void Remove(TEntity entity);
        void SaveChanges();
    }
}

对于每个实体,我都有一个使用存储库的服务/接口,然后可以将其注入到控制器中。

MyProject.Services/ICarService.cs

using MyProject.Data;
using MyProject.Repo;
using System.Collections.Generic;

namespace MyProject.Services
{
    public class CarService : ICarService
    {
        private IRepository<Car> carRepository;

        public CarService(IRepository<Car> carRepository)
        {
            this.carRepository = carRepository;
        }
        public IEnumerable<Car> getcars()
        {
            return carRepository.Getall();
        }
        public Car getcar(int id)
        {
            return carRepository.Get(id);
        }
        public void InsertCar(Car car)
        {
            carRepository.Insert(car);
        }
        public void UpdateCar(Car car)
        {
            carRepository.Update(car);
        }
        public void DeleteCar(int id)
        {
            Car car = getcar(id);
            carRepository.Remove(car);
            carRepository.SaveChanges();
        }
    }
}

MyProject.Services/CarService.cs

using MyProject.Data;
using System.Collections.Generic;

namespace MyProject.Services
{
    public interface ICarService
    {
        IEnumerable<Car> getcars();
        Car getcar(int id);
        void InsertCar(Car car);
        void UpdateCar(Car car);
        void DeleteCar(int id);
    }
}

相同的设置也适用于Tag实体,因此为了简洁起见,我没有将其包括在内。定义接口后,我可以将它们注入到控制器中,例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyProject.Data;
using MyProject.Repo;
using MyProject.Services;
using MyProject.UI.Helpers;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using microsoft.AspNetCore.Mvc;
using microsoft.EntityFrameworkCore;

namespace MyProject.UI.Controllers
{
    public class HomeController : Controller
    {
        private readonly ICarService _carService;
        private readonly ITagService _tagService;

        public HomeController(ICarService carService,ITagService tagService)
        {
            _carService = carService;
            _tagService = tagService;
        }
        public IactionResult Index()
        {
            var cars = _carService.getcars();
            return View(cars);
        }


        [HttpGet]
        public IactionResult Edit(int id)
        {
            var cars = _carService.getcar(id);
            return View(cars);
        }
    }
}

整理好上述信息后,我现在的任务是创建一种能够读取相关数据,对其进行更新和删除的方法。查看文档显示了一种创建循环的方法,但是那部分感觉很肿,因为我使用存储库,无法在代码中定义.include()之类的东西,我的问题是,如何读取此相关数据与我的配置一起使用吗?您会在我的代码中注意到,我还使用KendoUI,在许多情况下,它需要返回JSON数据。循环是文档中唯一提及的方法吗?任何指导表示赞赏。

iCMS 回答:如何使用Fluent API处理存储库模式中的相关数据? MyProject.Repo / Car.cs MyProject.Repo / Tag.cs​​ MyProject.Repo / MyContext.cs MyProject.Repo / Repository.cs MyProject.Repo / IRepository.cs MyProject.Services/ICarService.cs MyProject.Services/CarService.c

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2295578.html

大家都在问