Monday, January 30, 2012

Entity Framework and IBM DB2 Database

Recently I was doing one POC on Entity Framework and IBM DB2 database. Here is how we can make entity framework to consume IBM DB2 database.


IBM DB2 runs mostly on zOS or Linux platform. They do also have express edition which runs on Windows OS. I used the IBM DB2 Express edition which is a free downloadable from IBM site. Also, you will need the IBM DB2 connector VS Add-In to be able to establish connection with DB2 database. Don't worry, here is the link to download both these components


https://www14.software.ibm.com/webapp/iwm/web/download.do?source=swg-db2expressc&S_PKG=dlwin32&S_TACT=100KG25W&lang=en_US&dlmethod=http


Once you install and setup the IBM DB2 Express edtion and  VS Add-In just follow the below steps to establish connectivity from EF to DB2 database.


1) Create a sample project in VS2010
2) Add new ADO.NET Entity Data Model to the project












3) Click Next and select "New Connection"
4) In the connection dialog, click the "Change Datasource" button



















5) In the Data Sources screen, select "IBM DB2 and IDS servers"

















6) Select the "Server", enter "Username", "Password", "Database" and do a Test Connection to database. If DB connection is successful, goto next step.




















7) Select the options shown in the screenshot below and click "Next"



















8) Select the Tables, Views you need in this step as shown in the screenshot



















9) Voila, you should be able to see all the tables, views selected in the previous steps in the Model file














10) Now you should be able to consume the DB normally using EF.

Ok, now that you have done with the implementation; How do we redistribute the App to TEST or PROD server? Answer is yes, it is possible. You will need any one of the below drivers to achieve DB2 connectivity from TEST or PROD servers;

1) IBM Data Server Client: most complete, includes GUI Tools, drivers



2) IBM Data Server Runtime Client: a lightweight client with basic functionality, and includes drivers


3) DB2 Runtime Client Merge Modules for Windows: mainly used to embed a DB2 runtime client as part of a Windows application installation


4) IBM Data Server Driver for JDBC and SQLJ: allows Java applications to connect to DB2 servers without having to install a full client


5) IBM Data Server Driver for ODBC and CLI: allows ODBC and CLI applications to connect to a DB2 server without the large footprint of having to install a client


6) IBM Data Server Driver Package: Includes a Windows-specific driver with support for .NET environments in addition to ODBC, CLI and open source. This driver was previously known as the IBM Data Server Driver for ODBC, CLI and .NET


And of course, there are some limitations with this approach. Please read through the limitations As-Is provided by IBM before going forward with this approach.
 
http://www.ibm.com/developerworks/wikis/display/DB2/IBM%20Data%20Server%20LINQ%20Entity%20Framework%20Limitations

Friday, January 6, 2012

Binding dynamic XML objects to Silverlight Grids

Recently we had an requirement to bind the content of dynamic XML to Silverlight Grids.This is straightforward in ASP.NET with the help of DataSet. But we do not have DataSet in Silverlight or WPF. To achieve this we used the following approach;


The code is self-explanatory with the comments explaining each step;


public string Result
{
get
{
return _result;
}
set
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
//Load the xml string to XDocument
XDocument doc = XDocument.Load(new StringReader(value));
MemoryStream str = new System.IO.MemoryStream();
doc.Save(str);


//Finding the Name of the class
string objName = doc.Descendants().First().Name.LocalName.Replace("ArrayOf", "");


//Making it a fully qualified name by appending the corresponding
//namespace
objName = "MyCompany.MyApplication." + objName;


//Loading the assembly that contains the Entity which
//we are trying to de-serialize
//NOTE: To be able to do the below 3 steps, you need to ensure that
//the entities dll reference is added in Deployment.Parts of the
//App Manifest file
StreamResourceInfo sri = Application.GetResourceStream(new Uri("MyApplication.SL.Entities.dll",UriKind.Relative));
AssemblyPart myPart = new AssemblyPart();
System.Reflection.Assembly assm = myPart.Load(sri.Stream);


//Using reflection to build the List<T>
var pageToShowType = assm.GetType(objName);
Type t = typeof(List<>);
Type [] args = {pageToShowType};
Type r = t.MakeGenericType(args);


//De-serializing and assigning to the property to show in the grid
str.Position = 0;
DataContractSerializer serializer = new DataContractSerializer(r);
//Bind the ResultObject to the SL Grid
//NOTE: Ensure that SL Grid has AutoGenerateColumns = TRUE
ResultObject = serializer.ReadObject(str);
str.Close();


});


}


}

Friday, December 16, 2011

Workaround to Button using MVVM ICommand and IsEnabled issue

When you are using the Command Button in MVVM pattern with ICommand, you will not be able to use the IsEnabled property to enable/disable the command button. As a workaround to enable/disable the command button, you can put the Command Button inside the group control like StackPanel and set the IsEnabled property of StackPanel to TRUE/FALSE.

Monday, November 7, 2011

i++ kind of functionality in F#

As F# is immutable language, we will not be able to use i++ kind of functionality directly. You will have to combine the "mutable" and "module" feature of F# to achieve the i++ kind of functionality. Below here is the simple demonstration of this;

module Foo
       let mutable m_field = 0
       let next () =
             m_field <- m_field + 1; m_field

Just use Foo.next () whereever you need i++ kind of functionality.

Wednesday, October 19, 2011

Consume WCF REST service in F#

Today I successfully implemented the code for consuming WCF REST service in F#. Funny part here is that, this is my first program in F#.. LOL

Ok.. Here is the code for consuming WCF REST in F#.

module FSModule

#light

open System
open System.Text
open System.Net
open System.IO
open System.Web

// F# uses indenting to define scope. So ensure that you indent properly to get it working
let GetDataFromRest =
     let buffer = Encoding.ASCII.GetBytes("<HelloService_SayHello><input>Hello from F#</input></HelloService_SayHello>") //This is needed if its a POST call
     let req = WebRequest.Create(new Uri("http://localhost/HelloService.svc/SayHello")) :?> HttpWebRequest
     req.Method <- "POST"
     req.ContentType <- "application/xml" //Use accordingly XML or JSON
     req.ContentLength <- int64 buffer.Length
     let reqSt = req.GetRequestStream()
     reqSt.Write(buffer,0,buffer.Length)
     reqSt.Flush()
     req.Close()

     let res = req.GetResponse () :?> HttpWebResponse
     let resSt = res.GetResponseStream()
     let sr = new StreamReader(resSt)
     let x = sr.ReadToEnd()
     sr.Close()
     x.ToString()

Done... To invoke this, you can either use F# or C# or even VB.NET :)

F#

let result = GetDataFromRest()
printfn result

C#

string result = FSModule.GetDataFromRest()