【问题标题】:Communicate between Web Api and the Dal layer to query on DbContextWeb Api 与 Dal 层通信以在 DbContext 上进行查询
【发布时间】:2019-02-23 01:43:33
【问题描述】:

好时光

我有三层 UI,BLL,DAL。

我添加了一个 WEB API 层。我还有一个名为 PersonController 的类,我将它添加到 WEB API 项目中。班级是:

  public class PersonController : ApiController
{
    public IEnumerable<CommonPerson> GetAllPersons()
    {
         PersonRepository PersonRep = new PersonRepository();
        return PersonRep.SelectAll();   
    }
}

在 GetAllPersons 操作中,PersonRepository 类被调用,并在数据库查询中从 DbContext 转到 DAL。 这样做的问题是在DAL中无法读取,可以读取连接字符串,会出错。

如何将 web api 与 dal 层连接起来?

【问题讨论】:

  • 这部分我没看懂:问题是在DAL中无法读取,可以读取连接字符串,报错。请澄清更多。

标签: asp.net-web-api2


【解决方案1】:

一个好的做法是:

DbContext => 存储库 => 工作单元 => BLL => ApiController

这里有一个很好的资源:
http://jasonwatmore.com/post/2015/01/28/unit-of-work-repository-pattern-in-mvc5-and-web-api-2-with-fluent-nhibernate-and-ninject

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-29
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2013-06-20
    • 2022-12-09
    相关资源
    最近更新 更多