Question

I am just wondering in how to use Web Services with class object .I have class object in my BOL like Customer, Task, Project and etc. I use ADO.net to connect to data layer I'm just start using Web services on my project I added folder called "WebServices"and I used the method on BOL to get data and fetch data to Json object in Web Services. I'm just wondering should I connect the WebServices directly to database or use BAL to get the data after that fetch it to 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();
            }

        }

}

Was it helpful?

Solution

Yes, You can directly connect the database with your web services. But it is not proper way to do.

If you will connect directly to your database, It may be increase your code quantity and decrease the performance. Also the quality of the code will be decreased.

You should use BAL to separate the business logic and your code will be neat and clean.

here are some good examples of usually what we are using. Consuming Webservice using JQuery ASP.NET Application

Web Services using JQuery AJAX

Update

Although if you wanted to connect webservices directly to the database, here is the link : Call database from webservice

OTHER TIPS

Yes, Ideally use the BL to consume your web service. It will separate all your business logic and also your code will be scalable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top