【问题标题】:Connect WebService Restful in asp.net to a database将asp.net中的WebService Restful连接到数据库
【发布时间】:2019-02-06 12:14:22
【问题描述】:

我是新来的,如果我不能很好地解释自己,我很抱歉。 我有一个基本的 asp.net RESTful API,它返回 2 个联系人,我从一个 Android 项目“使用它”,我的问题是我是否可以或如何将 web 服务连接到我在另一台机器上的 Microsoft SQL Server 2014 数据库。 我应该将 ContactRepository 中的连接作为一个简单的连接还是我需要做一些不同的事情? 我将代码留在下面:

ContactRepository:

    namespace PruebaRestful.Services
{
    public class ContactRepository
    {
        public Contact[] GetAllContacts()
        {
            return new Contact[]
            {
            new Contact
            {
                Id = 1,
                Name = "Glenn Block"
            },
            new Contact
            {
                Id = 2,
                Name = "Dan Roth"
            }
            };
        }
    }
}

联系人控制器:

namespace PruebaRestful.Controllers
{
    public class ContactController : ApiController
    {
        private ContactRepository contactRepository;

        public ContactController()
        {
            this.contactRepository = new ContactRepository();
        }


        public Contact[] Get()
        {
            return contactRepository.GetAllContacts();        
        }
    }
}

谢谢大家,如果我需要写更多的东西,请在评论中告诉我。

【问题讨论】:

    标签: c# sql-server rest web-services asp.net-mvc-4


    【解决方案1】:

    您可以使用Dependency Injection,即它通常就像让 ContactController 在构造函数中接受连接对象一样简单。通常,您会使用 IConnection 接口而不是具体的连接类来模拟它以进行测试。

    namespace PruebaRestful.Services
    {
        public class ContactRepository
        {
            private IConnection _connection;
    
            public ContactRepository(IConnection connection)
            {
                _connection = connection;
            }
    
            public Contact[] GetAllContacts()
            {
                return new Contact[]
                {
                new Contact
                {
                    Id = 1,
                    Name = "Glenn Block"
                },
                new Contact
                {
                    Id = 2,
                    Name = "Dan Roth"
                }
                };
            }
        }
    }
    

    要了解更多如何在 Asp.Net 中执行此操作,请参阅 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2

    【讨论】:

      【解决方案2】:

      简短的回答,您可以通过任何方式(ADO.net 或任何其他 ORM)进行操作。

      在您的场景中,您可以应用依赖倒置原则。

      高层模块不应该依赖低层抽象

      在您的情况下,消费者不应该知道数据是从哪里提供的。您只需更改 GetAllContacts 以使用 ORM 从 db 读取数据。

      public class DbConnection: IConnection 
      {
          // Code for managing DB Connection and data querying
      }
      
      public class ContactRepository: IContactRepository
      {
          private IDbConnection _dbConnection;
      
          public ContactRepository(IConnection dbConnection)
          {
              _dbConnection= dbConnection;
          }
      
          public Contact[] GetAllContacts()
          {
               // Add code to read from DB here
          }
      }
      
      public class ContactController : ApiController
      {
          private IContactRepository _contactRepository;
      
          public ContactController()
          {
              _contactRepository = <using any DI container resolve dependency with ContactRepository>;
          }
      
      
          public Contact[] Get()
          {
              return _contactRepository.GetAllContacts();        
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-29
        • 1970-01-01
        • 1970-01-01
        • 2014-01-19
        • 2011-08-23
        • 2016-05-21
        • 1970-01-01
        相关资源
        最近更新 更多