Showing posts with label Transaction using OrMapper. Show all posts
Showing posts with label Transaction using OrMapper. Show all posts

Tuesday, March 3, 2009

Working with Wilson ORmapper

Working With Wilson ORMapper

Hello ,

I am working on a project and using wilson ORMapper to communicate with the database. I faced lot of problems as I didn’t find any the proper document about how to work with ORMapper. Then I thought that after learning I would prepare a very brief document to show how to write the method to communicate with the database using ORMapper. Here are some of the method :


Updating a record.


public static User UpdateUser(IContext context, User user)
{
Global.Manager.StartTracking(user, InitialState.Updated);
Global.Manager.PersistChanges(user, PersistDepth.SingleObject);

return user;
}

Adding New Record

public static User CreateUser(IContext context, User user)
{
Global.Manager.StartTracking(user, InitialState.Inserted);
Global.Manager.PersistChanges(user, PersistDepth.SingleObject);

return user;
}

Getting Record by iD.

public static User GetUserByID(IContext context, int userID)
{
OPathQuery query = new OPathQuery("ID = ?");
return Global.Manager.GetObject(query, userID);
}

Getting collection Object of record.

public static Collection GetUsers (IContext context)
{
if (userStatus == Constants.All)
{
OPathQuery query = new OPathQuery("");
return Global.Manager.GetCollection(query);
}
}


Calling Stored Procedure using Stored Procedure

public static DataSet GetTransferDetails(IContext context, int siteID, int roomID)
{
DataSet dataSet;
SelectProcedure spTransferDetails = new SelectProcedure(null, GetTransferDetails");
spTransferDetails.AddParameter("@siteID", siteID);
spTransferDetails.AddParameter("@departmentID", departmentID); spTransferDetails.AddParameter("@roomID", roomID);

dataSet = Global.Manager.GetDataSet(spTransferDetails);
return dataSet;
}


Executing SQL query in ORMapper

public DataSet GetUserRecordHistory(int userID, string emailID)
{
string sql = String.Format("select UserName,[Time],SiteID,RoleID from User where userID = {0} and emailID = {1} Order By Time DESC", userID, emailID);
return Global.Manager.GetDataSet(sql);
}


Passing Multiple Parameter to the ORMapper query

public Product GetProduct(int manufacturerID, int productCategoryID, int productTypeID,string productName , string length, string diameter, string primaryFeature, string secondaryFeature, string otherFeature)
{
OPathQuery query = new OPathQuery("CategoryID = ? and ManufacturerID = ? and TypeID = ? and Name = ? and Length = ? and Diameter = ? and PrimaryFeature = ? and SecondaryFeature = ? and OtherFeature = ? and IsActive = 1");
return Global.Manager.GetObject(query, productCategoryID, manufacturerID, productTypeID, productName, length, diameter, primaryFeature, secondaryFeature , otherFeature);
}


Using Transaction In ORMAPPER

public static List AddProductandBarcode(IContext context, List productbarcodes)
{
Product product = null;
BarCode barcode = null;
Transaction transaction = null;
List savedProducts = new List();
try
{
transaction = Global.Manager.BeginTransaction();
foreach (ProductBarcode productBarcode in productbarcodes)
{
product = productBarcode.ProductObject;
barcode = productBarcode.BarcodeObject;

Global.Manager.StartTracking(product, InitialState.Inserted);
transaction.PersistChanges(product, PersistDepth.SingleObject);

barcode.ProductID = product.ID;

Global.Manager.StartTracking(barcode, InitialState.Inserted);
transaction.PersistChanges(barcode, PersistDepth.SingleObject);
savedProducts.Add(product);
transaction.Commit();
return savedProducts;
}
catch { transaction.Rollback(); return null; }
finally { transaction.Dispose(); }
}


Sorting Result using ORMapper

public string sortExp = "Name";
public Collection GetSites(IContext context)
{
Collection sites = null;
OPathQuery query = new OPathQuery("",sortExp);
sites = Global.Manager.GetCollection(query);
return sites;
}


Getting single Result/Execute Scalar

public static string GetTimeZoneBySiteID(int siteID)
{
string queryString = string.Format("select timezone from SiteTimeZone tz where s.SiteID = {0}", siteID);
return Global.Manager.ExecuteScalar(queryString);
}


Parameterized query in ORmapper

public static DataSet GetCaseInfo(IContext context, int siteID, DateTime dateTime)
{

DataSet dataSet = null;
string queryString = string.Format(@"select timezone,Time from SiteTimeZone tz where s.SiteID = @siteName and Time=@Time", whereString);

SelectProcedure selectProcedure = new SelectProcedure(null, queryString);
selectProcedure.AddParameter("@SiteID", siteID);
selectProcedure.AddParameter("@Time ", dateTime);

dataSet = Global.Manager.GetDataSet(selectProcedure);
return dataSet;
}


Hope u understood the methods i have written. i dont think these method need any further explanation.

if u have any query , feel free to contact/reply me .

hope to see to again.

-Thanks
Arvind - Ramp

Happy Programming