LINQ To SQL CRUD and simple Update Sunday, July 12, 2009

Though Nhibernate shines on ORM world of .NET LINQ-To-SQL is faster when it comes to prototyping.

The big Mature Nhibernate has a solid session management code-base, also Burrow and Spring.Net have developed their own session management libraries for Nhibernate.
LINQ-To-SQL session management could be tricky especially with a state-less framework like ASP.NET so somehow you may find it better to create an instance every time you want to consume your database (Singleton pattern may not be the best answer here, please read Rick Strahl full article here) so basically the DataContext CRUD is all straight forward except the U part.

To Create an new entity you simply use the following code:


DbContext dbcontext = new DbContext(ConfigurationManager.ConnectionStrings[0].ConnectionString);
Request req = new Request();
req.Status = RequestStatus.PendingShipment;
req.UserId = Convert.ToInt32(signupUser.ProviderUserKey);
req.SubmittedOn = DateTime.Now;
dbcontext.Requests.InsertOnSubmit(req);
dbcontext.SubmitChanges();
dbcontext.Dispose();




to Delete a record :

DbContext dbcontext = new DbContext(ConfigurationManager.ConnectionStrings[0].ConnectionString);
Request req = dbcontext.Request.Single(r => r.id == requestId);
req.Status = RequestStatus.PendingShipment;
req.UserId = Convert.ToInt32(signupUser.ProviderUserKey);
req.SubmittedOn = DateTime.Now;
dbcontext.Requests.DeleteOnSubmit(req);
dbcontext.SubmitChanges();
dbcontext.Dispose();


and here comes the tricky Update:


DbContext dbContext = new DbContext(System.Configuration.ConfigurationManager.ConnectionStrings[0].ConnectionString);

dbContext.Requests.Attach(currentRequest);
dbContext.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, currentRequest);
dbContext.SubmitChanges();
dbContext.Dispose();




on updating your entities you don't need to reload the original entity of perform one-by-one property comparison, all you need is to Attach your entity and Refresh using KeepCurrentValues to keep unmodified items as is.

FedEx SOAP error : Schema validation failed for request Thursday, July 2, 2009

This issue is a bit tricky ! why does FedEx SOAP raises a Schema validation error ?

It seams that FedEx service is expecting you to perform a complete SOAP message including all elements even if you don't need on your service call.

Have a look on this C# code which performs a "valid" SOAP call :


PackageMovementInformationService svc = new PackageMovementInformationService();
var request = new PostalCodeInquiryRequest();
request.CarrierCode = CarrierCodeType.FDXE;
request.ClientDetail = new ClientDetail
{
AccountNumber = "ACCOUNT_NO",
Localization = new Localization
{
LanguageCode = "EN",
LocaleCode = "US-EN"
},
MeterNumber = "METER_NO"
};
request.CountryCode = "US";
request.PostalCode = "38017";
request.WebAuthenticationDetail = new WebAuthenticationDetail
{
UserCredential = new WebAuthenticationCredential
{
Key = "__KEY__",
Password = "_Password_"
}
};
request.Version = new VersionId ();
request.TransactionDetail = new TransactionDetail
{
CustomerTransactionId = "23",
Localization = new Localization { LocaleCode="US-EN", LanguageCode="EN" }
};

PostalCodeInquiryReply reply = svc.postalCodeInquiry(request);



This code didn't generate any validation errors because i simply did not leave any of PostalCodeInquiryRequest class members not instantiated.

if are using Ruby,Python or PHP just check you fill all the SOAP request elements.


UPDATE : for a comprehensive details of what elements are missing check an element named "detail" of the SOAP exception response.