Search

C# Preprocessor

Did you know that C# actually does have a preprocessor? Probably the most common C# preprocessor directive that you see is the #region directive, and that doesn't even have any impact on the code. Now the preprocessor for C# is nowhere near as powerful as the ones for C and C++ (for instance, no macros), but it does let you do a couple of handy things.
C# actually has almost all the standard preprocessor directives - it just happens to be that the functionality of some of them (specifically the #define directive) is quite reduced. The one notable directive that is missing is #include - and it makes sense that C# wouldn't have it, because C# gets the same sort of functionality from the using statements (although there is the fact that #include refers to files and using refers to assemblies - so they are definitely not equivalent). So let's start at the beginning: #define and #undef. The directive #define gives you the ability to define a symbol, and #undef lets you un-define it. For instance:
#define MY_SYMBOL
/* Do Stuff */
#undef MY_SYMBOL
However, while you can define a symbol, you cannot assign a value to it (which is where that major difference from C/C++ comes into play):
#define MY_SYMBOL 42

//Error: Single-line comment or end-of-line expected
So what good is defining a symbol when you can't actually give it a value? Because in C/C++, giving it a value was really the whole point. Well, now that you can't give it a value, the only place to use them are in the #if and #elif directives:
#define SYMBOL_A
#define SYMBOL_B

using System;
public class Foo
{
  static void Main() 
  {
#if (SYMBOL_A && !SYMBOL_B)
    Console.WriteLine("SYMBOL_A!!");
#elif (!SYMBOL_A && SYMBOL_B)
    Console.WriteLine("SYMBOL_B!!");
#elif (SYMBOL_A && SYMBOL_B)
    Console.WriteLine("SYMBOL_A and SYMBOL_B!!");
#else
    Console.WriteLine("Neither!!");
#endif
  }
}
That code sample pretty much covers all the craziness that you can do with the conditional directives. This particular code block would end up printing out "SYMBOL_A and SYMBOL_B!!", because both symbols were defined. If, say, I had thrown an #undef in there, we might get something else:
#define SYMBOL_A
#define SYMBOL_B

#undef SYMBOL_A

using System;
public class Foo
{
  static void Main() 
  {
#if (SYMBOL_A && !SYMBOL_B)
    Console.WriteLine("SYMBOL_A!!");
#elif (!SYMBOL_A && SYMBOL_B)
    Console.WriteLine("SYMBOL_B!!");
#elif (SYMBOL_A && SYMBOL_B)
    Console.WriteLine("SYMBOL_A and SYMBOL_B!!");
#else
    Console.WriteLine("Neither!!");
#endif
  }
}
Now, this code block would end up printing "SYMBOL_B!!".
But now on to my favorite two directives: #error and #warning. They essentially allow you to inject compile errors and warnings into the code. For example:
using System;

public class Foo
{
  static void Main() 
  {
#error My Best Error Ever!!
#warning A Little Tiny Warning
  }
}

//Error: #error: 'My Best Error Ever!!'
//Warning: #warning: 'A Little Tiny Warning'
Simple, and yet quite effective. I use them as almost a to-do list - whenever I'm leaving a section of code that I know is wrong, I'll leave some #error or #warning directives so that the compiler will remind me to come back later and fix it. Next up: #pragma. The #pragma directive is kind of a catch-all in C/C++, and it is the same here in C#. But there is one #pragma varient that is probably useful to know: #pragma warning.
using System;

public class Foo
{
  static void Main() 
  {
    int foo;
    Console.WriteLine("Hi");
  }
}

//Warning: The variable 'foo' is declared but never used
Generally, the code above would throw the warning that you see there. But say you want to ignore that warning - you can use the #pragma warning directive to get rid of it:
using System;

public class Foo
{
  static void Main() 
  {
#pragma warning disable 0168
    int foo;
#pragma warning restore 0168
    Console.WriteLine("Hi");
  }
}
Essentially, what that does is disable the warning number CS0168 between the disable and restore directives. You should be careful when using this directive, because if you don't restore the warning, it will be disabled for the rest of the file. This #pragma warningdirective is actually quite a bit more powerful than displayed here, and if you'd like to learn more, you should read about it at MSDN. Well, that concludes a nice overview of C#'s preprocessor directives. We did not cover everything, because some things (like #line) could probably get a whole tutorial on their own. Also, if you would like to know more about what can be done with the #pragmadirective, you can check out this MSDN page (there is a lot of stuff). As always, feel free to leave any questions or comments below.

What is WCF RIA service?

WCF RIA service is a framework to develop n-tier application for Rich Internet Application (RIA). It is mainly used in RIA applications like Silverlight, AJAX client, etc. It solves the major problem while developing business application like decoupling the resource access, application logic and presentation layer. WCF RIA service was introduced in Silverlight 4 with .net framework 4, and it can be developed using visual studio2010.

Main problem developer are facing while developing the n-tier RIA application will be coordinating the application logic between middle tier and presentation tier. This problem will be solved by using WCF RIA service, it will synchronize the code between middle and presentation tier.

WCF RIA service will allow developer to write the set of service code and this server code will be available to the client side without manually duplicate that programming logic. RIA service client will be updated with business rule and entity present at the server side, when your recompile your project solution.

WCF RIA service will generate the code at the client side related to the service and domain entities declared at the server side.

RIA service exposes the data from server side to client side using Domain service, RIA service framework implements the each domain service as WCF service to access the data as business entity.

  1. WCF RIA Domain Service

  2. Problems solved in RIA Service

  3. Query/Update process in RIA

  4. How to create Silverlight-WCF RIA service

fig: WCF RIA Serive architecture
WCF RIA Serive architecture

 

Domain Service

Domain services are WCF services that expose the business logic of a WCF RIA Services application. Domain service contains set of business related data operation and it is exposed as WCF service.

Below diagram explains integration of the RIA service with WCF

The DomainService class is the base class for all classes that serve as domain services.

  • DomainServiceHost is the hosting class for domain service; internally
  • DomainServiceHost uses the WCF ServiceHost class to host the application.

A domain service class must be marked with the EnableClientAccessAttribute attribute to make the service available to the client project. The EnableClientAccessAttributeattribute is automatically applied to a domain service when you select the Enable client access check box in the Add New Domain Service Class dialog box. When the EnableClientAccessAttribute attribute is applied to a domain service, RIA Services generates the corresponding classes for the client project.

 
//Example:
[EnableClientAccess()]
      public class EmployeeDomainService : DomainService
      {
          private EmployeeData data = EmployeeData.Instance;   
          public IEnumerable < Employee> GetEmployees()
          {
              return data.EmployeeList;
          }
      }   


DomainContext class at the client side is used to consume the Domain service by using DomainClient object. DomainContext class available inside the name space "System.ServiceModel.DomainServices.Client"

fig: WCF RIA Domain Serive architecture
WCF RIA Domain Serive

 


Problem solved in RIA



  1. To have best performance of the RIA application, app logic need to be available in client and server side. This problem is solved by auto generating the code at the client side while recompiling the project.
  2. Asynchronous call – Asynch service call are supported in Domain service by using WCF infrastructure
  3. Handling large data and data across different tier – Large amount of data can be access and filter using IQueryable object. Since entity objects used in domain service are serializable and so it can be access across different layer
  4. Security/Access control – ASP.Net membership frameworks are integrated with RIA service to provide security systems to RIA service
  5. Validation – Entity can be validated based using adding attribute to the class members

//Example:
public class Member
    {
        [Key]
        public int MemberId { get; set; }   
   
        public string Fname { get; set; }
   
        [Required]
        public string Lname { get; set; }
   
        public DateTime JoinDate { get; set; }
   
        [Range(30,90, ErrorMessage="sorry, you are either too young or too old for our club!")]
        public int Age { get; set; }
    }

 


 


Querying/Updating data in RIA Service


The below diagram are self explanatory to discuss about the querying or updating the data using RIA service

fig: WCF RIA to Query data


fig: WCF RIA to update data




 


How to Create WCF RIA Service


Download:
Silverlight_WCF_RIA_Service.zip

Let us understand more about the WCF RIA service by creating Silverlight client application which read and updated the Employee details from WCF RIA Service.

Step 1:Start the Visual Studio 2010 and click File -> New-> Project. Enter the project name and click “Create”

Project creation

Step 2:Select “Enable WCF RIA Services”. This will make your Silverlight application to user WCF RIA service

Select RIA service

Step 3:Create “Data” folder and add DataModel” class as shown below. This is the data class which will return list of Employee and update the employee list

Data Model class:


   1:  public class Employee
   2:      {
   3:          [Key]
   4:          public int EmpId { get; set; }
   5:          public string Fname { get; set; }
   6:          public string Lname { get; set; }
   7:          public DateTime JoinDate { get; set; }
   8:          public int Age { get; set; }
   9:      }
  10:   
  11:   
  12:      public partial class EmployeeData
  13:      {
  14:          private static readonly EmployeeData _instance = new EmployeeData();
  15:          private EmployeeData() { }
  16:          public static EmployeeData Instance
  17:          {
  18:              get
  19:              {
  20:                  return _instance;
  21:              }
  22:          }
  23:   
  24:   
  25:          private List < Employee > empList = new List < Employee>()
  26:          {
  27:              new Employee() { EmpId  = 1, Fname = "Sam", Lname = "kumar", 
  28:                              JoinDate=new DateTime(2010,7, 21), Age=30},
  29:              new Employee() { EmpId = 2, Fname = "Ram", Lname = "kumar", 
  30:                              JoinDate=new DateTime(2009,6,8), Age=35},    
  31:              new Employee() { EmpId = 3, Fname = "Sasi", Lname = "M", 
  32:                              JoinDate=new DateTime(2008,3,5), Age=39},  
  33:              new Employee() { EmpId = 4, Fname = "Praveen", Lname = "KR", 
  34:                              JoinDate=new DateTime(2010, 5,1), Age=56},
  35:              new Employee() { EmpId = 5, Fname = "Sathish", Lname = "V", 
  36:                              JoinDate = new DateTime(2006,12,15), Age=72},  
  37:              new Employee() { EmpId = 6, Fname = "Rosh", Lname = "A", 
  38:                              JoinDate=new DateTime(2009,2,2), Age=25}
  39:          };
  40:   
  41:          public IEnumerable< Employee > EmployeeList
  42:          {
  43:              get
  44:              {
  45:                  return empList;
  46:              }
  47:          }
  48:   
  49:   
  50:          public void Update(Employee updEmployee)
  51:          {
  52:              Employee existing = empList.Find(p => p.EmpId == updEmployee.EmpId);
  53:              if (existing == null)
  54:                  throw new KeyNotFoundException("Specified Employee cannot be found");
  55:   
  56:              existing.Fname = updEmployee.Fname;
  57:              existing.Lname = updEmployee.Lname;
  58:              existing.JoinDate = updEmployee.JoinDate;
  59:              existing.Age = updEmployee.Age;
  60:          }
  61:      }



Step 4:To expose the Employee related operation to the client side, Create domain service class. By right click project file and select Add new item.

Create Domain Service

Step 5:Add code to return the Employee list

Domain Service class:


   1:  // TODO: Create methods containing your application logic.
   2:      [EnableClientAccess()]
   3:      public class EmployeeDomainService : DomainService
   4:      {
   5:          //Create instance of the Data access layer
   6:          private EmployeeData data = EmployeeData.Instance;
   7:   
   8:          public IEnumerable< Employee> GetEmployee()
   9:          {
  10:              return data.EmployeeList ;
  11:          }
  12:   
  13:          public void UpdateEmployee(Employee emp)
  14:          {
  15:              data.Update(emp);
  16:          }
  17:      }



Step 6:Compile the solution – After compilation RIA service will generate the application logic at the client side using DomainContext object. Enable show all files option for the solution and view the auto generated code.

Auto generated code

Step 7:View the DomainContext class are created at the client side.

Domain Context class at client:


   1:   /// 
   2:      /// The DomainContext corresponding to the 'EmployeeDomainService' DomainService.
   3:      /// 
   4:      public sealed partial class EmployeeDomainContext : DomainContext
   5:      {
   6:          
   7:          #region Extensibility Method Definitions
   8:   
   9:          /// 
  10:          /// This method is invoked from the constructor once initialization is complete and
  11:          /// can be used for further object setup.
  12:          /// 
  13:          partial void OnCreated();
  14:   
  15:          #endregion
  16:          
  17:          
  18:          /// 
  19:          /// Initializes a new instance of the < see cref="EmployeeDomainContext"/> class.
  20:          /// 
  21:          public EmployeeDomainContext() : 
  22:                  this(new WebDomainClient< IEmployeeDomainServiceContract>(new 
  23:                                Uri("MyFirstRIAApplication-Web-EmployeeDomainService.svc", 
  24:                                                          UriKind.Relative)))
  25:          {
  26:          }
  27:   
  28:          ........
  29:          ........



Step 8:Add DataGrid to Main.xaml file to display the employee details query from DataModel and add two buttons to update and reject the data changed from client side.


Main.xaml

 < Grid x:Name="LayoutRoot" Background="White">
        < StackPanel Orientation="Vertical" HorizontalAlignment="Left"  >
        < sdk:DataGrid x:Name="EmployeeGrid" AutoGenerateColumns="True"  
                        RowEditEnded="EmployeeGrid_RowEditEnded" />
            < Button Content="Accept" Height="23" Name="btnAccept" 
                        Width="75" Margin="5" Click="btnAccept_Click"  />
            < Button Content="Reject" Height="23" Name="btnReject" 
                        Width="75" Margin="5" Click="btnReject_Click"/>
        </StackPanel>
    </Grid>


    

Main.xaml.vb

   1:  public partial class MainPage : UserControl
   2:      {
   3:          //create instance of Doman context class
   4:          EmployeeDomainContext ctx = new EmployeeDomainContext();
   5:   
   6:          public MainPage()
   7:          {
   8:              InitializeComponent();
   9:              //Load query data , Read data from DAL layer to UI
  10:              EntityQuery< Employee> query = ctx.GetEmployeeQuery();
  11:              LoadOperation< Employee> lo = ctx.Load< Employee>(query);
  12:              EmployeeGrid.ItemsSource = lo.Entities;
  13:          }
  14:   
  15:          private void EmployeeGrid_RowEditEnded(object sender, 
  16:                                  DataGridRowEditEndedEventArgs e)
  17:          {
  18:   
  19:          }
  20:   
  21:          private void btnAccept_Click(object sender, RoutedEventArgs e)
  22:          {
  23:              //Update the DAL with user changes
  24:              ctx.SubmitChanges();
  25:          }
  26:   
  27:          private void btnReject_Click(object sender, RoutedEventArgs e)
  28:          {
  29:              //Roll back the user changes
  30:              ctx.RejectChanges();
  31:          }
  32:      }



Step 9:Run the application and see the output as shown below

RIA service output