Wednesday, September 18, 2013

N-Frequencies (AKA Online Radio Streamer) : Release 4

Thought to change the name of the app and renamed “Online Radio Streamer” to “ N-Frequencies”. This release will fix following issues.
  • Volume slider only contained 0 and 1. Either you can keep sound on or off. Now you can reduce the volume.
  • Some radio channels are not shown in some categories which has more that 30 channels. Now it’s shown.
Read more about previous releases.

Release 3

http://jaliyaudagedara.blogspot.com/2013/06/online-radio-streamer-release-3.html

Release 2

http://jaliyaudagedara.blogspot.com/2013/06/online-radio-streamer-release-2.html

Release 1

http://jaliyaudagedara.blogspot.com/2013/06/online-radio-streamer-release-1.html

Happy Coding.

Regards,
Jaliya

Tuesday, September 17, 2013

Visual C# Technical Guru – August 2013

This month became second in Technical Guru (Visual C#) competition for the month of August, 2013. The TechNet Guru Awards celebrate the technical articles on Microsoft TechNet.

Post in WikiNinjas Official Blog,
http://blogs.technet.com/b/wikininjas/archive/2013/09/11/technet-guru-awards-august-2013.aspx

image
Visual C# Technical Guru - August 2013
Happy Coding.

Regards,
Jaliya

Wednesday, September 11, 2013

IEnumerable<T> Vs. IQueryable<T>

I am sure most of you have seen following interfaces when you are writing some code with .NET Framework.
Have you ever wondered what these really are. Let’s see the beauty of these items and specially let’s have a dig in to IEnumerable<T> and IQueryable<T>.

First Let’s see how MSDN describes these four interfaces.

IEnumerable

Exposes an enumerator, which supports a simple iteration over a non-generic collection.
public interface IEnumerable

IEnumerable<T>

Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
public interface IEnumerable<out T> : IEnumerable

IQueryable

Provides functionality to evaluate queries against a specific data source wherein the type of the data is not specified.
public interface IQueryable : IEnumerable
{
   Type ElementType { get; }
   Expression Expression { get; }
   IQueryProvider Provider { get; }
}

IQueryable<T>

Provides functionality to evaluate queries against a specific data source wherein the type of the data is known.
public interface IQueryable<out T> : IEnumerable<T>,
    IQueryable, IEnumerable
{
    Type ElementType { get; }
    Expression Expression { get; }
    IQueryProvider Provider { get; }
}

So it seems mainly what is common about all four of these is, they are iterators. Which means we can traverse through the items. If we know the type of the collection that we are going to iterate (a generic type), we can use IEnumerable<T> or IQueryable<T>, and if we don’t know the type(a non-generic type) we can use IEnumerable or IQueryable.

As you can see IQueryable and IQueryable<T> contains some three additional properties which are not present in IEnumerable and IEnumerable<T>.

Assuming that we know the type (which means we can use either IEnumerable<T> or IQueryable<T>), let’s have a compare and contrast on IEnumerable<T> and IQueryable<T>.

IEnumerable<T> Vs. IQueryable<T>

For better understanding let me go with a example here. I have a very simple database which “DemoDB” and it contains a single table “Employee”. “Employee” table consists of three columns which are “EmployeeId”, ”FirstName” and “LastName”. I have following values in it.

image
Sample Data
Now I have added a ADO.NET Entity Data Model to my application and selected Generate from database and selected my “DemoDB”. Entity framework will create all the necessary classes to query my database.

I have following two helper methods to query data with the condition Employees with their Last Name “Smith”, one for IEnumerable<T> and the other for IQueryable<T>. I am calling them from my Main method.
static void CallWithIEnumerable(DemoDBEntities oDemoDBEntities)
{
    IEnumerable<Employee> employeeList = 
        oDemoDBEntities.Employees.Where(e => e.LastName == "Smith");
 
    foreach (Employee employee in employeeList)
    {
        Console.WriteLine(employee.FirstName);
    }
}
 
static void CallWithIQueryable(DemoDBEntities oDemoDBEntities)
{
    IQueryable<Employee> employeeList = 
        oDemoDBEntities.Employees.Where(e => e.LastName == "Smith");

    foreach (Employee employee in employeeList)
    {
        Console.WriteLine(employee.FirstName);
    }
}
Now if we examine the SQL queries generated for above two scenarios we are getting the following.
--IEnumerable
SELECT 
[Extent1].[EmployeeId] AS [EmployeeId], 
[Extent1].[FirstName] AS [FirstName], 
[Extent1].[LastName] AS [LastName]
FROM [dbo].[Employee] AS [Extent1]
WHERE 'Smith' = [Extent1].[LastName]

--IQueryable
SELECT 
[Extent1].[EmployeeId] AS [EmployeeId],
[Extent1].[FirstName] AS [FirstName], 
[Extent1].[LastName] AS [LastName]
FROM [dbo].[Employee] AS [Extent1]
WHERE 'Smith' = [Extent1].[LastName]
Basically it’s the same. Now let’s modify our helper methods to get TOP 1 record.
static void CallWithIEnumerable(DemoDBEntities oDemoDBEntities)
{
    IEnumerable<Employee> employeeList = 
        oDemoDBEntities.Employees.Where(e => e.LastName == "Smith");
    employeeList = employeeList.Take(1);

    foreach (Employee employee in employeeList)
    {
        Console.WriteLine(employee.FirstName);
    }
}

static void CallWithIQueryable(DemoDBEntities oDemoDBEntities)
{
    IQueryable<Employee> employeeList = 
        oDemoDBEntities.Employees.Where(e => e.LastName == "Smith");
    employeeList = employeeList.Take(1);

    foreach (Employee employee in employeeList)
    {
        Console.WriteLine(employee.FirstName);
    }
}
And if we examine the queries now we are getting the following.
--IEnumerable with top
SELECT 
[Extent1].[EmployeeId] AS [EmployeeId], 
[Extent1].[FirstName] AS [FirstName], 
[Extent1].[LastName] AS [LastName]
FROM [dbo].[Employee] AS [Extent1]
WHERE 'Smith' = [Extent1].[LastName]

--IQueryable with top
SELECT TOP (1) 
[Extent1].[EmployeeId] AS [EmployeeId],
[Extent1].[FirstName] AS [FirstName], 
[Extent1].[LastName] AS [LastName]
FROM [dbo].[Employee] AS [Extent1]
WHERE 'Smith' = [Extent1].[LastName]
For IEnumerable<T>, it was the same query as the previous, but for the IQueryable<T>, the query has changed amazingly to get TOP 1 directly.

Another similar example would be while doing a projection. When using IEnumerable<T> it will run the query to select all the properties while IQueryable<T> will only run the query selecting the necessary properties to do the projection.

So now we know something additional is happening with IQueryable<T> and let’s find out what.

Now comes the property Expression of IQueryable<T> to the scene. The Expression property gets the expression tree that is associated with the instance. In simple Expression trees represent code in a tree-like data structure (I am not going to go further with Expression trees here, may be it can be separate a post).

So what happens here is, the instruction set will get embed as a Expression and passes to the compiler. The compiler analyses the instructions (in here it is a SQL Query) and selects the best way to execute it.

Now comes the question when to use IEnumerable<T> or IQueryable<T>.
  • If you want to select some data in the same process use IEnumerable<T>.
  • If you want to contact some other process to select some data (in here, it’s the SQL Server), use IQueryable<T> and let compiler decide the best way.

So that's it. Appreciate your feedback. And please correct me if I am wrong here.

You can find the sample code here.


Happy Coding.

Regards,
Jaliya

Monday, September 9, 2013

WCF Data Services with Reflection Provider

Today let’s see how to implement a WCF Data Service with Reflection Provider. The reflection provider exposes data in classes which returns an IQueryable<T>

I am going to use the same scenario which I used to demonstrate WCF Data Services with Entity Framework Provider.

Scenario

I have three entities which are “Employee”, “Department” and “JobRole”. The relationships among these three entities are as follows.
  • Employee can only belong to one Department and one Department can have many Employees.
  • Employee can have many JobRoles and one JobRole can have many Employees.
Let me start off by creating a WCF Service application.

image
Creating a WCF Service Application

I am deleting the service and it’s interface which was created initially when creating the project. I am going to add latest WCF Data Services references through Nuget. I am searching online for “WCF Data Services Server” and installing it.

Untitled
Adding references through Nuget

Once all the needed references are installed, I am going to create a class which I am going to name as “MyDataContext”.

I am going to create three classes for each of my entity types.
public class Department
{
    public int DepartmentId { get; set; }
    public string DepartmentName { get; set; }
}

public class JobRole
{
    public int JobRoleId { get; set; }
    public string JobRoleName { get; set; }
}

public class Employee
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Department Departments { get; set; }
    public List<JobRole> JobRoles { get; set; }
}
OK, now you can see I have three classes. In the “Employee” class, I have two properties along with basic properties which are “Departments” and “JobRoles”. By having “Departments” property which is of type “Department”, I am mentioning the department a particular employee belong to. By having list of “JobRoles” which is of type “List<JobRole>”, I am mentioning the job roles a particular employee can have.

Now I need to have primary key fielda for each of these entity types. Of course, I have three properties which are “DepartmentId”,”JobRoleId” and “EmployeeId” which I can use as primary key fields. So for that I am going to decorate the class with the attribute DataServiceKeyAttribute.

So now I am going to create a method in each of these classes, which will return some data I want to expose to outside.
[DataServiceKeyAttribute("DepartmentId")]
public class Department
{
    public int DepartmentId { get; set; }
    public string DepartmentName { get; set; }

    public static List<Department> GetDepartments()
    {
        List<Department> deptList = new List<Department>();
        deptList.Add(new Department() { DepartmentId = 1, DepartmentName = "Information Technology" });
        deptList.Add(new Department() { DepartmentId = 2, DepartmentName = "Finance" });
        deptList.Add(new Department() { DepartmentId = 2, DepartmentName = "Human Resources" });
        return deptList;
    }
}

[DataServiceKeyAttribute("JobRoleId")]
public class JobRole
{
    public int JobRoleId { get; set; }
    public string JobRoleName { get; set; }

    public static List<JobRole> GetJobRoles()
    {
        List<JobRole> jobRoleList = new List<JobRole>();
        jobRoleList.Add(new JobRole() { JobRoleId = 1, JobRoleName = "Software Engineer" });
        jobRoleList.Add(new JobRole() { JobRoleId = 2, JobRoleName = "Project Manager" });
        jobRoleList.Add(new JobRole() { JobRoleId = 3, JobRoleName = "Accountant" });
        jobRoleList.Add(new JobRole() { JobRoleId = 4, JobRoleName = "HR Executive" });
        return jobRoleList;
    }
}
 
[DataServiceKeyAttribute("EmployeeId")]
public class Employee
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Department Departments { get; set; }
    public List<JobRole> JobRoles { get; set; }

    public static List<Employee> GetEmployees()
    {
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee()
        {
            EmployeeId = 1,
            FirstName = "Jaliya",
            LastName = "Udagedara",
            Departments = Department.GetDepartments().First(dept => dept.DepartmentName == "Information Technology"),
            JobRoles = new List<JobRole>() 
            { 
                JobRole.GetJobRoles().First(jobRole => jobRole.JobRoleName == "Software Engineer"),
                JobRole.GetJobRoles().First(jobRole => jobRole.JobRoleName == "Project Manager") 
            }
        });
        empList.Add(new Employee()
        {
            EmployeeId = 2,
            FirstName = "John",
            LastName = "Doe",
            Departments = Department.GetDepartments().First(dept => dept.DepartmentName == "Finance"),
            JobRoles = new List<JobRole>() 
            { 
                JobRole.GetJobRoles().First(jobRole => jobRole.JobRoleName == "Accountant") 
            }
        });
        empList.Add(new Employee()
        {
            EmployeeId = 3,
            FirstName = "Jane",
            LastName = "Doe",
            Departments = Department.GetDepartments().First(dept => dept.DepartmentName == "Human Resources"),
            JobRoles = new List<JobRole>() 
            { 
                JobRole.GetJobRoles().First(jobRole => jobRole.JobRoleName == "HR Executive") 
            }
        });
        return empList;
    }
}

Once it is done, I have some entities and some methods which return some data. Now I am going to create my model or the context that I am going to expose through a WCF Data Service.

So I am modifying my “MyDataContext” class as follows.
public class MyDataContext
{
    public IQueryable<Employee> Employees
    {
        get
        {
            return Employee.GetEmployees().AsQueryable();
        }
    }

    public IQueryable<Department> Departments
    {
        get
        {
            return Department.GetDepartments().AsQueryable();
        }
    }

    public IQueryable<JobRole> JobRoles
    {
        get
        {
            return JobRole.GetJobRoles().AsQueryable();
        }
    }
}

I have added three Properties which is of IQueryable<T>. You might be wondering why specially IQueryable<T>, don’t worry about it now, we will discuss about it later.

Now I am almost done. Let’s add a WCF Data Service to the project and put “EntitySetRights.AllRead” as the entity access rule for all the entities, so consumers can read all entities.
using System.Data.Services;
using System.Data.Services.Common; 

namespace WCFDataServicesWithReflectionProvider
{
    public class WcfDataService1 : DataService<MyDataContext>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        }
    }
}

Now when I hit F5, I can see the WCF Data Service up and running.

image
WCF Data Service
When you view the metadata of this service by typing,
http://domain/service.svc/$metadata
you can see the following.

image
metadata

Now I am going to write the theoretical things related to Reflection Provider. The reason for me to didn’t mention these in the beginning and to mention now is, the best way to understand theory is, comparing it with the practical output.

Of course I will be copy and pasting the things from the MSDN, so you might will bored. But when you contrast them with WCF Data Service we just created, you will find them all interesting.

When you create the data service, the provider infers the data model by using reflection. The following list shows how the reflection provider infers the data model.
  • Entity container - the class that exposes the data as properties that return an IQueryable<T> instance.
    • Here it’s our class “MyDataContext”
  • Entity sets - properties that return IQueryable<T> instances are treated as entity sets.
    • Here it’s our properties in the “MyDataContext” class which are “Employees”, “Departments” and “JobRoles”. The reason to create properties that return IQueryable<T> instances are, basically it is a must. If you return IEnumerable<T>, you will not be able to see those entity sets in the WCF Data Service. Here is a nice post which describes IQueryable<T> vs. IEnumerable<T>
  • Entity types - the type T of the IQueryable<T> that the entity set returns.
    • Here it’s our entity classes which are “Employee”, “Department” and “JobRole”
  • Entity keys - each data class that is an entity type must have a key property. This property is attributed with the DataServiceKeyAttribute attribute ([DataServiceKeyAttribute]).
    • Here it’s “EmployeeId”, “DepartmentId” and “JobRoleId”.
  • Entity type properties - other than the entity key, the reflection provider treats the accessible, non-indexer properties of a class that is an entity type as follows:
    • Here it’s the properties of our entity classes.
    • If the property returns a primitive type, then the property is assumed to be a property of an entity type.
    • If the property returns a type that is also an entity type, then the property is assumed to be a navigation property that represents the "one" end of a many-to-one or one-to-one relationship.
      • Departments property of Employee class
    • If the property returns an IEnumerable<T> of an entity type, then the property is assumed to be a navigation property that represents the "many" end of a one-to-many or many-to-many relationship.
      • JobRoles property of Employee
    • If the return type of the property is a value type, then the property represents a complex type.
So that’s it. I am uploading the sample to my SkyDrive, Enjoy WCF Data Services with Reflection Provider.



Happy Coding.

Regards,
Jaliya