Thursday, June 26, 2014

Roslyn (Next Version of C# Compiler ) part 01

This article is about what is available in C# 6.0 with next version of compiler. You can have the binaries from roslyn.codeplex.com and use it. Here im going to demonstrate some key futures that useful in day to day programming .


1. Indexed Members and Element Initializers

There is several versions of it. Here is example for assign collection when it initialize in the code,
Ex : List<Ttype,Tvalue>

 Dictionary<string, string> sampleDemo = 
new Dictionary<string, string>()
{
{"Mango", "Yellow"},
{"Banana", "Green"},
{"Pineapple", "Orange"},
{"Apple", "Red"}
};

//You can access each element like here
var c = sampleDemo ["Mango"];


indexed Members with $ mark

 Dictionary<string, string> sampleDemo = 
new Dictionary<string, string>()
{
$Mango="Yellow",
$Banana="Green",
$Pineapple="Orange",
$Apple="Red"
};

//You can access each element like here
var c = sampleDemo.$Mango;

indexing with Json data

Code:
string JsonData =@"{"widget": {
"
debug": "on",
"
window": {
"
title": "Sample Konfabulator Widget",
"
name": "main_window",
"
width": 500,
"
height": 500
},
"
image": {
"
src": "Images/Sun.png",
"
name": "sun1",
"
hOffset": 250,
"
vOffset": 250,
"
alignment": "center"
},
"
text": {
"
data": "Click Here",
"
size": 36,
"
style": "bold",
"
name": "text1",
"
hOffset": 250,
"
vOffset": 100,
"
alignment": "center",
"
onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}"
;



//You can access each element like here
JObject jObject = JObject.Parse(JsonData );
var output = jObject.$window.$Keyword);




Thanks and enjoy... Meet with next post with another feature

Monday, June 23, 2014

Web service with DB interaction (Entity framework 6), Part 1

Lot of people have problems with database interaction with Web services. connection problems with databases , retrieving, inserting etc. In this post I will show you hoe to connect DB and manage connections efficiently with Web service.

To archive this goal entity frame work is very helpful tool.


Lets see how to use it.

Here is my SQL server table that im going to use. Insted of SQL server You can use any Database type such as Oracle, Mysql etc.













Lets Code . :)

1. Create new WCF application in Visual Studio












2. R-Click on Solution and add -> new Item -> ADO.NET Entity data Model






 

 3. then select EF designer from database











 4. Connect your database with New Connection button and select relevant schema that you going to use

5. Then -> NEXT and select Entity framework version.. here im using 6.0 then click NEXT again

 6. Select relevant Tables , Views or stored procedures you need to use and click FINISH
 














 7. Then you can see that it automatically added classes and additional libraries that need to use the database . and also it will have separate classes for each Tables, Stored procedures and Views that you selected in the initialization space. Check  those classes in your project










8. Then create separate function for our database select and add it to interface([OperationContract]). In here I need to output all the data in the Contact table. Entity Framework creates relevant mapped class for my table. I just need to do is use that class as my return type. Like this , My function will return all the contacts because of that I'm using list of that contact objects


IEFDemo.cs:
 public interface IEFDemo
{
[OperationContract]
string DoWork();


[OperationContract]
List<Contact> myContacts();
}


EFDemo.svc.cs:
 public List<Contact> myContacts()
{

}

9. Then we need to create instance of database connection and initialize the instance. than open the connection using that instance and get the data from database then close the connection. We are done
Here is full code.


 public class EFDemo : IEFDemo
{
//instance of database entities
private demoEntities DB;
public string DoWork()
{
return "hello";
}

public List<Contact> myContacts()
{
DB = new demoEntities();

//database connection open and close
DB.Database.Connection.Open();

//add all items in DB to items variable
var items = DB.Contacts;
DB.Database.Connection.Close();

return items.ToList();
}
}

Run it and test ...

Here is my results,














Enjoy.







Full Code In Media Fire 
http://bit.ly/1qDPrBF

 

Friday, June 20, 2014

Cross platform Development With Xamarin

Having a same App on all major platforms and all of them are shared a same  source code is dream. But today it is not. There is several tools to do that  here few of them,

  • Xamarin
  • PhoneGap
  • Rho Mobile
  • Appcelerator
  • Widget Pad
  • Whoop
  • MoSync
  Importance of Xamarin is ,
  • Share code between platforms
  • Using .NET skills
  • C# Language
  • Let developers create UI’s that are unique to each platform.
Xamarin is cross platform development tool that created by Xamarin Studio and it supports to build applications on Windows , iOS and Android platforms.

 Most importantly it have integration with rich IDE , Visual Studio.

You can download Xamarin Trial version here.
https://store.xamarin.com/account/my/subscription/computers

Try it and Enjoy.