Dynamicweb 8 Dynamicweb.DataManager refactoring



Dynamicweb.DataManager is a helper object used for updating datasets against the databases. It exposes some of the same members as Dynamicweb.Database. With the refactoring of DataManager, these two types now behave similarly.
 


Background

I previously wrote about the refactoring of Dynamicweb.Database that solved some usability issues. The same issues are valid for Dynamicweb.DataManager.

Here go the issues that are solved with the refactoring.
  • In 7.2 Database.getDataSet and DataManager. getDataSet takes the same parameters, but in reverse order.
  • Getting identity has different names in Database (UpdateDatasetIdentity) and DataManager (getIdentity)
  • Naming of types needs to be aligned with the similar types of Database API.
All of this can will be optimized on the DataManager API so it is aligned with Database API and is faster and easier to use.


The change

  • getDataSet(database, sql) is renamed to CreateDataSet(sql, database)
  • getIdentity is renamed to GetAddedIdentityKey
  • updateDataSet is renamed to Update

Full usage example

Below a complete sample of how the DataManager can be used after the refactoring.
class DataManagerSample
{
	public int Save(int id)
	{
		//Create a new datamanager instance
		using (var dtm = new DataManager())
		{
			//Create a dataset with zero, one or more rows to update.
			var ds = dtm.CreateDataSet("SELECT * FROM UrlPath WHERE UrlPathID=" + id, "Dynamic.mdb");
			DataRow row;

			//If the id is 0, there will be no records in the returned dataset
			if (id == 0)
			{
				//Add a new row to the table in the dataset
				row = ds.Tables[0].NewRow();
				ds.Tables[0].Rows.Add(row);
				//Set column values that only needs to be set on new rows
				row["UrlPathCreated"] = System.DateTime.Now;
			}
			else
			{
				//We had a record in the table
				row = ds.Tables[0].Rows[0];
			}

			//Update the column values
			row["UrlPathPath"] = "Contact";
			row["UrlPathRedirect"] = "Default.aspx?ID=1";
			row["UrlPathStatus"] = 301;
			row["UrlPathUpdated"] = System.DateTime.Now;
			row["UrlPathActive"] = true;

			//Pass the dataset to the datamanagers UpdateDataset method that will commit our changes to the database
			dtm.Update(ds);

			//If the id is 0, it was a new record - GetIdentity will return the id of the newest row added.
			if (id == 0)
			{
				id = dtm.GetAddedIdentityKey();
			}

			//Dispose the dataset
			ds.Dispose();
		}
		return id;
	}

}

So what does that mean?


Well, not much.

All your use of DataManager does not need to change. getDataset and getIdentity are still in the API, but hidden for intellisense and obsoleted so you will get compile warnings – but no errors.

Bottom line: Your code using Dynamicweb. DataManager does not need any upgrade when upgrading solutions to from Dynamicweb 7 to Dynamicweb 8.

Implementing new solutions using this API should be easier since it now behaves exactly like Dynamicweb.Database.




3 Comments

  1. Imar Spaanjaars 01 November 2011 23:37
    >> using this API should be easier since it now behaves exactly like Dynamicweb.Database.


    If that's the case, is there still room for both solutions? What would be the use case for this Manager class?

    Imar

  2. Nicolai Pedersen 02 November 2011 09:39
    DataManager is for connected datasets that are updateable - adding, deleting and editing rows. It has an open underlying connection and command object that are reused to make the update. Thats also why this object is disposable.

    Database class returns disconnected datasets/tables.

  3. Rasmus Pedersen 09 November 2011 09:43
    About time you cleaned up some of my mess.

Leave a comment, thanks!

Log in in the top with the username and password you use on the developer site. Then the form will know who you are. Profile images from http://www.gravatar.com/.




 
Dynamicweb Software A/S
© All rights reserved
Tlf: +45 70 25 20 90