【问题标题】:Web services and Class objectWeb 服务和类对象
【发布时间】:2012-12-16 00:18:05
【问题描述】:

我只是想知道如何将 Web 服务与类对象一起使用。我的 BOL 中有类对象,例如客户、任务、项目等。我使用 ADO.net 连接到数据层我刚刚开始使用 Web我在项目中添加了名为“WebServices”的文件夹,并使用 BOL 上的方法获取数据并将数据提取到 Web 服务中的 Json 对象。我只是想知道我应该将 WebServices 直接连接到数据库还是使用 BAL 来获取数据,然后将其获取到 Json。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Compudata_ProjectManager.CodeFile.BOL;
using System.Web.Script.Services;



namespace Compudata_ProjectManager.WebServices
{
    /// <summary>
    /// Summary description for CustomerList
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class CustomerList : System.Web.Services.WebService
    {


            [WebMethod]
            [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public List<Customer> FetchCustomersList(string name)
            {
                var cust = new Customer();
                var fetchNames = cust.GetAllCustomerNames().Where(n => n.FirstName.ToLower().StartsWith(name.ToLower()));
                return fetchNames.ToList();
            }


            [WebMethod]
            [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public List<Location> FetchCustomerAddressList(string name)
            {
                var Addresses = new Location();
                var fetchAddress = Location.GetAllAddress();
                return fetchAddress.ToList();
            }

        }

}

【问题讨论】:

    标签: asp.net web-services ado.net business-logic-layer


    【解决方案1】:

    是的,您可以直接将数据库与您的 Web 服务连接起来。但这不是正确的做法。

    如果您将直接连接到您的数据库,可能会增加您的代码量并降低性能。代码的质量也会降低。

    你应该使用 BAL 来分离业务逻辑,你的代码会整洁干净。

    这里有一些我们通常使用的很好的例子。 Consuming Webservice using JQuery ASP.NET Application

    Web Services using JQuery AJAX

    更新

    虽然如果你想将 web 服务直接连接到数据库,这里是链接: Call database from webservice

    【讨论】:

      【解决方案2】:

      是的,理想情况下使用 BL 来使用您的 Web 服务。它将分离您的所有业务逻辑,并且您的代码将具有可扩展性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多