Using dOOdads in a Web Service

All your dOOdad needs ...

Using dOOdads in a Web Service

Postby mike.griffin on Thu Feb 17, 2005 2:56 am

This post demonstrates using a dOOdad in a web service. I used the Employees table from the SQL Northwind database. Remember, the heart of a dOOdads is a DataTable, not a DataSet, and we really need to remote a DataSet, so I added two methods to my concrete class.

Code: Select all
public class Employees : _Employees
{
   public void FromDataSet(DataSet ds)
   {
      this.DataTable = ds.Tables[0];
   }

   public DataSet ToDataSet()
   {
      DataSet dataSet = new DataSet();
      dataSet.Tables.Add(this.DataTable);
      return dataSet;
   }
}

Next, I created my web service which does only two things, LoadAll and Save. Notice that my web service uses the two methods I added in my concrete class ToDataSet and FromDataSet

Code: Select all
public class EmployeesService : System.Web.Services.WebService
{
   [WebMethod]
   public DataSet LoadAll()
   {
      try
      {
         Employees emps = new Employees();
         emps.LoadAll();
         return emps.ToDataSet();
      }
      catch { return null; }
   }

   [WebMethod]
   public bool Save(DataSet dataset)
   {
      bool wasSaved = false;

      try
      {
         Employees emps = new Employees();
         emps.FromDataSet(dataset);
         emps.Save();
         wasSaved = true;
      }
      catch { }

      return wasSaved;
   }
}


My next step was to test it. I tested the following code snippet in both a web form and win form, it worked fine in both cases.

Code: Select all
//----------------------------------------------------------
// Invoke the Web Service and Load all of the Data
//----------------------------------------------------------
EmployeesService empsService = new EmployeesService();
DataSet ds = empsService.LoadAll();

//----------------------------------------------------------
// Load that Data into a local Employees object
//----------------------------------------------------------
Employees emps = new Employees();
emps.FromDataSet(ds);
emps.FirstName = \"WebService!!\";
emps.GetChanges();  // very important

//----------------------------------------------------------
// Reconvert to a DataSet (after calling GetChanges)
//----------------------------------------------------------
ds = emps.ToDataSet();

//----------------------------------------------------------
// Marshall it back to the web service
//----------------------------------------------------------
empsService.Save(ds);

Notice that I call GetChanges(). This reduced the data to only modified, inserted, or deleted rows, this way you only marshall back to the web service data that has changed.

There are more elegant solutions I'm sure, if I were going to support a query mechanism in my web service I would use the LoadFromRawSql() method and syntax. I would also add those two methods (FromDataSet and ToDataSet) to my concrete class template and regenerate.
Last edited by mike.griffin on Tue Mar 01, 2005 12:54 pm, edited 1 time in total.
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby pinoybug on Sat Feb 19, 2005 1:48 pm

Hi Mike,

Your sample works great. However, I tried implementing a LoadFromRawSql() form my webservice, I then get the same error as I originaly sent you.

Please let me know if I am doing something wrong.

Thanks,

TE:-D
pinoybug
Lurker
 
Posts: 5
Joined: Mon Oct 04, 2004 11:12 am
Location: Philippines

Postby mike.griffin on Sat Feb 19, 2005 1:57 pm

I will add that to my sample later today and see what happens.
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby mike.griffin on Sat Feb 19, 2005 3:10 pm

Are you trying to trick me into writing your project? If so, so far it is working ... :wink: Everything I'm trying is working without issue: Here's what I did.

Code: Select all
public class Employees : _Employees
{
    public void DynamicQuery(string sql, object[] parameters)
    {
        this.LoadFromRawSql(sql, parameters);
    }
}


Code: Select all
[WebMethod]
public DataSet Query(string sql, object[] parameters)
{
   try
   {
      Employees emps = new Employees();
      emps.DynamicQuery(sql, parameters);
      return emps.ToDataSet();
   }
   catch { return null; }
}


Code: Select all
EmployeesService empsService = new EmployeesService();

object[] parameters = new Object[1];
parameters[0] = \"%a%\";

DataSet ds = empsService.Query(\"SELECT * FROM Employees WHERE LastName LIKE {0}\", parameters);
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby pinoybug on Sat Feb 19, 2005 3:32 pm

Hi Mike,

Well, it is not the intention. I'm just unfamiliar with the behaviour of doodads as of the moment, newby syndrome maybe. I hope you're not offended :oops: I appreciate the patience and the very quick replies.

Have a great day,

TE:-D
pinoybug
Lurker
 
Posts: 5
Joined: Mon Oct 04, 2004 11:12 am
Location: Philippines

Postby mike.griffin on Sat Feb 19, 2005 5:28 pm

I was just razzing you :D :D I've been wanting to get a Web Service example together for some time now, you finally prodded me into doing it so I'm grateful. Let me know if that works for you.

Actually, when I get MySQL implemented I might actually create a Web Service dOOdad template and post it on the online template library, at least this could serve as a starter for folks wanting to do the same.
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby mike.griffin on Mon Sep 26, 2005 3:16 pm

If you want to use your dOOdads in a web service on both the client and server you use this AddNewClient() on your client side. You need to add this to your C# dOOdads busiiness entity template. It would be easy to convert this to VB.NET as well.

Code: Select all
public void AddNewClient()
{
   if(this.DataTable == null)
   {
      this.DataTable = new DataTable();
      DataColumnCollection colls = this.DataTable.Columns;
      <%
      Dim orig
      orig = MyMeta.Language
      MyMeta.Language = \"C# System Types\"
      For Each objColumn in objTable.Columns %>
      colls.Add(new DataColumn(\"<%=objColumn.Name%>\", Type.GetType(\"<%=objColumn.LanguageType%>\"))); <%
      Next
      MyMeta.Language = orig
      %>
   }
   DataRow newRow = this.DataTable.NewRow();
   this.DataTable.Rows.Add(newRow);
   this._dataRow = newRow;            
}

The resulting output looks like this:

Code: Select all
public void AddNewClient()
{
   if(this.DataTable == null)
   {
      this.DataTable = new DataTable();
      DataColumnCollection colls = this.DataTable.Columns;
      
      colls.Add(new DataColumn(\"EmployeeID\", Type.GetType(\"System.Int32\")));
      colls.Add(new DataColumn(\"LastName\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"FirstName\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"Title\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"TitleOfCourtesy\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"BirthDate\", Type.GetType(\"System.DateTime\")));
      colls.Add(new DataColumn(\"HireDate\", Type.GetType(\"System.DateTime\")));
      colls.Add(new DataColumn(\"Address\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"City\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"Region\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"PostalCode\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"Country\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"HomePhone\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"Extension\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"Photo\", Type.GetType(\"System.Byte[]\")));
      colls.Add(new DataColumn(\"Notes\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"ReportsTo\", Type.GetType(\"System.Int32\")));
      colls.Add(new DataColumn(\"PhotoPath\", Type.GetType(\"System.String\")));
      colls.Add(new DataColumn(\"test\", Type.GetType(\"System.String\")));
   }
   DataRow newRow = this.DataTable.NewRow();
   this.DataTable.Rows.Add(newRow);
   this._dataRow = newRow;            
}

The normal AddNew does this which hits the database and this is no good on the client side of things.

Code: Select all
virtual public void AddNew()
{
   if(_dataTable == null)
   {
      this.LoadFromSql(\"SELECT * FROM [\" +  QuerySource + \"] WHERE 1=0\", null, CommandType.Text);
   }

   DataRow newRow = _dataTable.NewRow();
   _dataTable.Rows.Add(newRow);
   _dataRow = newRow;
}
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

WebServices using Doodads and WebServices

Postby john_kattenhorn on Sat Jan 21, 2006 7:31 pm

Hi Mike,

I'm currently using these techniques to return an untyped dataset from a WebMethod.

My problem is the client end. Because i have an untyped dataset i'm having to write my own wrapper for the data because i'm using PocketPC and cannot create objects using EO or DooDads.

It kinda feels wrong that i have to manually create everything and i'm sure i'm missing something :oops:

Is there a way to send out a typed dataset or perphaps converting a dataset from untyped to typed on the client side of things.

Thanks

John
john_kattenhorn
Sergeant
 
Posts: 27
Joined: Fri Sep 09, 2005 4:33 pm

Postby mike.griffin on Sat Jan 21, 2006 9:52 pm

I'm curious and not very knowledgeable about the PocketPC. Why is it that dOOdads cannot run on it? Are you sure that dOOdads can't run on it?
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby Swampy on Fri Jul 14, 2006 8:38 am

Hi Mike,

i am newbie to MyGeneration. can you explain how to use a web service using VB.NET

thanks :D
Swampy
Lurker
 
Posts: 1
Joined: Fri Jul 14, 2006 8:36 am
Location: UK

Re: Using dOOdads in a Web Service

Postby happyhippy on Mon Aug 21, 2006 4:06 am

Thank you very much!
1. I heard that that in ADO.Net 2.0, The dataTable also be serializable as dataset, and it can be the return value of web service.

2. I have a question that whether the returned dataset is a typed dataset or untyped dataset?

I have another way to use the entity in web service than I turn the entity to a string(use ToXml() method):
Code: Select all
   [WebMethod]
   public string LoadAll()
   {
      try
      {
         Employees emps = new Employees();
         emps.LoadAll();
         return emps.ToXml()
      }
      catch { return null; }
   }


Client Code:invoke emps.FromXml() to get the datatable.
Can you tell me which method have better performance?

Thank you.
happyhippy
Lurker
 
Posts: 6
Joined: Sun Aug 20, 2006 3:43 am


Return to dOOdads - MyGeneration's .NET Architecture

Who is online

Users browsing this forum: No registered users and 0 guests