如何根据存储库模式在控制器之间共享相同的操作逻辑

发布于 2024-09-28 12:22:14 字数 1072 浏览 3 评论 0原文

我有 CompanyController 和 DepartmentController:

public class CompanyController : BaseBackendController
{
   private ICompanyRepository repository;
   public CompanyController(ICompanyRepository repository)
   {
     this.repository = repository;
   }

  ...
   [HttpPost]
   public ActionResult BatchDelete(long[] ids)
   {

     var entities = repository.GetList().Where(item => ids.Contains(item.ID));

     repository.BatchDelete(entities);
     return RedirectToAction("Index");
   }
}

public class DepartmentController : BaseBackendController
{
   private IDepartmentRepository repository;
   public DepartmentController(IDepartmentRepository repository)
   {
     this.repository = repository;
   }

  ...
   [HttpPost]
   public ActionResult BatchDelete(long[] ids)
   {

     var entities = repository.GetList().Where(item => ids.Contains(item.ID));

     repository.BatchDelete(entities);
     return RedirectToAction("Index");
   }
}

您可以看到 BatchDelete 的逻辑是相同的,我希望将其放置到父控制器中,但有一个挑战,即存储库。我无法调用基本控制器repository.GetList()。

I have CompanyController and DepartmentController:

public class CompanyController : BaseBackendController
{
   private ICompanyRepository repository;
   public CompanyController(ICompanyRepository repository)
   {
     this.repository = repository;
   }

  ...
   [HttpPost]
   public ActionResult BatchDelete(long[] ids)
   {

     var entities = repository.GetList().Where(item => ids.Contains(item.ID));

     repository.BatchDelete(entities);
     return RedirectToAction("Index");
   }
}

public class DepartmentController : BaseBackendController
{
   private IDepartmentRepository repository;
   public DepartmentController(IDepartmentRepository repository)
   {
     this.repository = repository;
   }

  ...
   [HttpPost]
   public ActionResult BatchDelete(long[] ids)
   {

     var entities = repository.GetList().Where(item => ids.Contains(item.ID));

     repository.BatchDelete(entities);
     return RedirectToAction("Index");
   }
}

You can see that logic of BatchDelete is the same and I want it place to parent controller, but there is a challenge, the repository. I cant call in base controller repository.GetList().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶气 2024-10-05 12:22:14

您的存储库界面必须具有一些通用性。例如,您可以这样做:

public interface IRepository<T>
{
    IEnumerable<T> GetList();
    void DeleteBatch(IEnumerable<T> entities);
    // other methods here
}

在您拥有的地方:

public interface ICompanyRepository : IRepository<T>

然后

public interface IDepartmentRepository : IRepository<T>

您可以像这样设置您的基本控制器:

public abstract class DataController<TModel> : Controller
{
    protected IRepository<TModel> repository;

    public DataController(IRepository<TModel> repository)
    {
        this.repository = repository;
    }

   [HttpPost]
   public ActionResult BatchDelete(long[] ids)
   {

     var entities = repository.GetList().Where(item => ids.Contains(item.ID));

     repository.BatchDelete(entities);
     return RedirectToAction("Index");
   }
}

UPDATE 然后您的 CompanyController 将如下所示:

public CompanyController : DataController<Company>
{
    public CompanyController(IRepository<Company> repository) : base(repository)
    {
    }
}

这样就可以了。

另请注意,您的 GetList() 似乎正在从数据库中获取所有 实体,然后选择要删除的实体进行删除操作。最好从数据库中检索您感兴趣的内容,这样可以显着提高性能。

You have to have some commonality in your repository interface. For example, you could this do:

public interface IRepository<T>
{
    IEnumerable<T> GetList();
    void DeleteBatch(IEnumerable<T> entities);
    // other methods here
}

where you have:

public interface ICompanyRepository : IRepository<T>

and

public interface IDepartmentRepository : IRepository<T>

Then you can set up your base controller like this:

public abstract class DataController<TModel> : Controller
{
    protected IRepository<TModel> repository;

    public DataController(IRepository<TModel> repository)
    {
        this.repository = repository;
    }

   [HttpPost]
   public ActionResult BatchDelete(long[] ids)
   {

     var entities = repository.GetList().Where(item => ids.Contains(item.ID));

     repository.BatchDelete(entities);
     return RedirectToAction("Index");
   }
}

UPDATE Then your CompanyController will look like this:

public CompanyController : DataController<Company>
{
    public CompanyController(IRepository<Company> repository) : base(repository)
    {
    }
}

This will do the trick.

One other note of caution it looks like your GetList() is getting all entites from the database and then select the one you want to delete for the delete operation. Better to retrieve only the one you are interested from the database and save significant performance.

递刀给你 2024-10-05 12:22:14

孩子们,这就是我们将服务传递给控制器​​而不是原始存储库的原因。

This, children, is why we pass services to Controllers, not raw repositories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文