Tuesday, February 17, 2009

Research : Silverlight Data, WCF as a Service and few others

Like I mentioned in one of my previous posts, I was thinking on how to develop a report development suite using Silverlight and WCF as the backbone. In that process I had several questions for which I had been digging around the internet to see if I could get some answers. The intention of this post is to log my findings. Please note that I do not own the content posted here and I appreciate all the effort that the authors have put into writing these articles.

1. How would I bind unknown objects to the Silverlight Datagrid (or any grid for that matter)?

Problem: My plan is to take in queries that generates reports and run them against a database (SQL or Oracle). The returned results should bind to the datagrid. So I am not sure on what columns would be returned from the result set and more over I do not know what type of class should I be creating in advance so as to support all the possible results returned. Some times it might just return EmployeeName, EmployeeId while some other time it might just return EmployeeName, EmployeeSalary, EmployeeHikePercent. So the question is on how to generate dynamic typed objects in C#.

Solution: One idea is to generate IEnumerable<IDictionary> as described in Vladimir’s blog.

How-to-bind-Silverlight-DataGrid-from-IEnumerable-of-IDictionary

2. I would like to get a general idea on how to go ahead developing business/enterprise class applications in Silverlight. Is there a reference implementation or a tutorial?

Problem: Doing simple stuff in Silverlight is fairly easy but developing enterprise line-of-business applications using Silverlight would require decent experience with the new programming model that silverlight brings in. So learning from other’s experience would be great.

Solution: Look at Chris Andersons’ wonderful series on developing LOB applications in Silverlight.

Building-a-Framework-for-Silverlight-Line-Of-Business-Applications.aspx

3. Now let us think deep. Let us assume that somehow I am able to send my data using WCF service. The problem would be – what the most performance efficient way to implement paging or infinite scrolling?

Problem: you have the datasource and the most common requirement is to implement Paging functionality in the grid. So how would I go about doing this. Are there any clever methods?

Solution: Look at the following links. They are all very useful.

scrolling-through-large-resultset-with-silverlight-2-beta1-and-linq-to-sql.aspx

stealth-paging-datagrid.aspx

4. How about support for large datasets returned from the service?

I know with my 0.01 cent worth knowledge on WCF, it is too early for me to think on these lines but anyway the problem that I foresee is that if WCF service sends large dataset to the Silverlight client, the client application eventually runs out of memory. So how would I handle it.

So far, I just found this - http://weblogs.asp.net/cibrax/archive/2008/06/10/streaming-large-content-with-wcf-and-deferred-execution.aspx

5. More on large datasets, sorting, grouping on paged datagrids.

Problem: Let us assume that we have loaded only the first page of data in the datagrid and when we sort, the grid only sorts that particular page. But the client expects to see the whole data sorted and return only the first page. How would I achieve this with Silverlight datagrid and WCF as a data providing service?

Solution: On this one, I am yet to find proper solutions. May be I am not looking right. so if anyone reading this blog has prior idea on how to  do this, I would be glad if you can share your experience or idea (need not share code as such, I am just looking for a proper pattern to do this).

6. The users viewing the report might want to apply certain filters. How would I go ahead and provide such a functionality?

Problem: Let us assume we displayed certain report. Now it would be nice if the user can view only those records which meets his criteria. So we provide the user with some filtering capabilities. The user would add a new filter expression which would be something like “ColumnX = 100”. So the filter should be evaluated and be applied on the dataset and display only those records which satisfy the filters specified.

Solution: One way is to use dynamic linq (http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx) which allows string LINQ expressions. So the user would be writing LINQ expressions which would be evaluated runtime and the result would be returned.

7. What if the user wishes to have more control, then he could write a filter in languages like IronRuby, IronPython and Managed JScript.

Problem: Consider the same scenario as above but with a more complex scenario. The user wishes to execute some script on the result returned, save the script as a filter and apply it on the report as he wishes to. So how would we provide such a capability ?

Solution: The idea would be to use DLR platform which provides scripting ability using DLR languages like IronRuby, IronPython and Managed JScript. For a basic understanding on how to enable your applications to have scripting ability, you might want to look at the following articles which discuss the subject in more detail.

http://blogs.microsoft.co.il/blogs/shayf/archive/2009/02/01/make-your-application-extendable-using-the-dlr.aspx

http://ironruby.rubyforge.org/wiki/wiki.pl?ExecutingIronRubyFromCSharp

http://www.ironruby.net/Documentation/CLR_Interop

http://msdn.microsoft.com/en-us/magazine/cc163284.aspx

My next immediate task is to be able to come up with a detailed article on how to make a simple extendable grid using WPF datagrid and IronRuby as scripting platform. Hopefully I should be done with it today and get a more detailed understanding on how things work.

8. Microsoft’s Silverlight Datagrid is very basic. Can I have a better grid that is free to use?

Problem: Silverlight datagrid does not provide functionality like grouping data, display summary items straight out of the box and requires more work.

Solution: Use DevExpress AgDataGrid the free silverlight datagrid control which has nice features like summary rows, sorting, grouping and many others that the Silverlight datagrid lacks.

9. So where else can you think of using scripting functionality on the reporting suite.

One idea is to auto-generate Ruby classes for a query based on the result returned, load them using DLR and generate data source out of it. But it might not actually be a great idea since I have no clue on how well it performs or how well it would act as a Data Transfer Object.

Custom Summary Rows : AgDatagrid allows us to display custom summary items which is very flexible. So we can extend the same flexibility to the report developer. He would write a script using one of the DLR languages which would be executed to get the custom summary value. This would make the application more flexible and depending on how well the developers are with scripting the possibilities are endless. Combined with filters functionality, this would turn out to be a great application to use – powerful and yet simple for basic reports.

Custom Event Handlers: Let us say the report has an event ReportDataFetched. Reports developer can specify a custom script which handles how the data would be filtered before actually rendering the data. He might want to filter out some data without even showing the user. Similarly such other events could be applied using scripting. For example, when we provide scheduling functionality we might have an event that says “ExecutionCompleteDataReady” which can execute a SendEmail script that the user would provide us.

With proper optimization of the script such that the end user does not feel bugged, DLR would be a great platform to make your applications extensible. You just need to figure out ways to work with it.

10. Let us say everything is going well with the reporting suite and finally I would like to implement the following – Charts, Dynamic Charts, Exporting data to Excel-PDF-CSV, Exporting charts as images. How would I be doing all this?

Silverlight, at this moment, does not allow data to be printed at the client. So the conversion should be done on the service. For this, I found a codeplex project called Exporter (Exporter). In order to generate charts in Excel documents, one could use the help of this MSDN article. Some discussion on this subject is available here.

As of the charts, Silverlight charts looks like a great suite that could be used. Another option is to use Visifire controls but they are not free for commercial use.I received a comment from Chirag, Product Manager at Visifire stating that Visifire is indeed free for commercial applications if it conforms to the GPL. As a more important note, Silverlight Toolkit is a very good suite that one could use in many ways.

If we move ahead on how to export Silverlight control rendered as an image, as a starting place, I would look at Silverlight Contrib project. This project provides a Silverlight implementation of XamlWriter which can be used to get the XAML for the current silverlight element and then render it to a PNG. (Use this article)

I am glad on how this blog has turned out to be. It really helped me to think in the proper direction. Now in the next article I would focus on making extendible grid and demo the filter functionality with the help of DLR, I guess I would pick Ruby (I need to learn on how to use it, though).

Sunday, February 15, 2009

C# 3.0 & LINQ : Auto-implemented Properties

As my quest to understand the finer details of LINQ which I would present to my team at Imageright, I thought it would be a great idea to start from scratch and look at each functionality of C# language which enables LINQ. In that process, I start with Auto-implemented Properties.

Defining auto-implemented property

   1: public sealed class Person{


   2:  


   3:        public string FirstName { get; set; }


   4:  


   5: }




The above class Person, defines an auto-implemented properties. you see can see both setter and getter defined. We will dig more into these in a while before which we should look at the compiler generated code. I used Reflector to decompile the source and this is what it looks like.





   1: public sealed class Person


   2: {


   3:     // Fields


   4:     [CompilerGenerated]


   5:     private string <FirstName>k__BackingField;


   6:  


   7:     // Properties


   8:     public string FirstName


   9:     {


  10:         [CompilerGenerated]


  11:         get


  12:         {


  13:             return this.<FirstName>k__BackingField;


  14:         }


  15:         [CompilerGenerated]


  16:         set


  17:         {


  18:             this.<FirstName>k__BackingField = value;


  19:         }


  20:     }


  21: }


  22:  





Clearly, nothing exceptionally new has been added to the IL and the compiler just generates C# 2.0 like properties from the above property – thus aptly named auto-implemented properties. Notice that a hidden backing field is used to support the property.

CompilerGeneratedAttribute[System.Runtime.CompilerServices] is applied to any element that is not user written and is automatically generated by the compiler. This attribute can be used to determine if an element is generated by the compiler.



Read-Only Properties





   1: //read-only property


   2: public readonly string LastName { get; set; }




The above shown way is INCORRECT. The compiler does not allow readonly keyword on the auto-prop. The way you achieve read-only is as shown below.





   1: //read-only property


   2: public string PersonCode { get; private set; }




The compiler generated code seems to add “private’ before the setter, just like the way we did.





   1: public string PersonCode


   2:   {


   3:       [CompilerGenerated]


   4:       get


   5:       {


   6:           return this.<PersonCode>k__BackingField;


   7:       }


   8:       private [CompilerGenerated]


   9:       set


  10:       {


  11:           this.<PersonCode>k__BackingField = value;


  12:       }


  13:   }




As a side-note, auto-implemented properties can also be static.





   1: //statics are allowed


   2: public static string ClassName{


   3:             get;


   4:             set;


   5: }




Write-Only Properties



Well, this might not make sense to use (in practical scenarios), but write-only properties are also allowed. When one tries to read a write-only property, the compiler generates the following error.



Error    1    The property or indexer 'LinqBasics.Person.SecretCode' cannot be used in this context because the get accessor is inaccessible



Shown below is a write-only property.





   1: public string SecretCode { private get; set; }






Also note that, you can re-order the get and set [does not matter, though].





   1: public string Ext { private set; get; }


   2: //order of set and get does not matter




What are not allowed?



The accessibility modifier for the accessor should be more restrictive than the property or the indexer.



The following property is invalid.





   1: private string ExtensionWide { private get; set; }


   2: //private get is not more restrictive than private applied on the property


   3: //thus it is not a valid declaration




Similarly, a private property cannot have a public accessor.



You cannot specify accessibility modifiers for both accessors of a property or indexer.



The following is invalid.





   1: public string InvalidProperty { private get; private set; }


   2: //modifiers on both accessors not allowed.




Indexers cannot be defined using auto-implemented properties.



If you do so, you end up with compiler error





   1: //indexer property cannot be defined using short-hand notation


   2: public string this[int index]


   3: {


   4:             get; 


   5:             set;


   6: }


   7: /*Errors you would notice


   8: 'LinqBasics.Person.this[int].get' must declare a body because it is not marked 


   9: abstract, extern, or partial


  10: */




Automatically implemented properties must defined both get and set accessors.



The following is not valid.





   1: public string LastNameX { get; } 


   2: //missing set accessor, thus invalid.




Normal properties can be initialized, while auto-implemented properties cannot be initialized.



See the code below.





   1: //Normal properties can be initialized.


   2: private string _lastName = "Vangapandu";


   3: public string LastName{ get { return _lastName; }}


   4:  


   5: //Auto-implemented properties cannot be initialized.


   6: public string ZipCode { get; set; } = "30605";




The documentation also talks about creating immutable classes using auto-implemented properties. So, now lets make the Person class immutable (we make all the properties as read-only and privatize the constructor. also we provide a factory method for instantiation.)





   1: public sealed class Person


   2:     {


   3:         public string FirstName { get; private set; }


   4:         public string LastName { get; private set; }


   5:  


   6:         private Person()


   7:         {


   8:         }


   9:  


  10:         public static Person CreatePerson(string firstname, string lastname)


  11:         {


  12:             Person p = new Person();


  13:             p.FirstName = firstname;


  14:             p.LastName = lastname;


  15:             return p;


  16:         }


  17:     }




I will add more information later if required. I hope this article is as informative as I wanted it to be.

Tuesday, February 10, 2009

Long hiatus and queued up work … new ideas…

Well, its been a long time since I blogged something and it was just that I thought I had a legitimate reason for not to blog. I am attending this training at my work which is kick-ass (when it comes to learning) and wierd when it comes to knowledge being graded with quiz and exams. Anyway, I love the learning part and the exams – not much. But it is my full time job and I got to learn what they throw at me, which is fun though. Anyway, that training itself keeps me busy most of the day and most of the week. The time that I am left with, I spend it watching tv (did i mention I am actually following 7 different TV series and I am a huge fan of HOUSE). Anyway, I have once again began thinking about developing a reporting engine using Silverlight and WCF. We use LogiXML internally and it does a pretty good job with its flexible approach when it comes to integrating reports with existing software systems, but personally (this does not represent the opinion of my employer in any way) I feel it is too complex and we use only 10% of what the engine provides. Moreover it is darn-slow since it uses a lot of Datasets and writes whole bunch of XML files. I wish to revamp this, as a challenge to come up with a great Silverlight-based report front-end driven by WCF on the server. Apart from this pet project, I am working with a friend on a super cool application which is still in its infancy but hopefully things would be improved as I graduate from the training camp.

Lets talk about the reporting suite

1. Reports can be created simply by adding a query or a method from a .NET plugin.
2. Drill-Down reports can be developed by simply mapping the reports using a column pulled from the parent report.
3. Reports can be styled using standard silverlight styles.
4. Overall look would initially be fixed and as the application evolves, a designer can be used to restructure the components. Fine-grain design might not be supported yet, since it is complex to implement.
5. Reports can have input-data which should be easy to set up.
6. The reporting engine would support scheduling reports off the shelf.
7. Events would be raised on the server and plugins would be invoked for those events passing appropriate data. For example, BeforeReportLoads event would be fired as the report is being loaded and this would invoke IAsReportLoaded.ReportLoading(ReportInputData) would be called. So we can create a plugin which would be injected into the server and which provides implementation for the interface and in the implemented method, we could save the ReportInputData to a database. This is what we call “saving the user favorites”.
8. The report viewer would support generating graphs/charts on-the-fly and in the future, one would be able to save the chart parameters as a favorite and apply when required.
9. The report grid would be fancy one with support for grouping, sorting, filters and should be one like the Intersft Webgrid.

Cut the crap and talk about technical details

I may be wrong but I think the application should be using PRISM since it supports dynamic loading of XAP files (modules) which makes the application faster to load and better to use. Remember, not all users would use the entire application at once which makes dynamic injection of modules a great catch. PRISM also automatically makes my application to have a great design (I know it is not true but at least it makes me know what is the right way to do some things, if not all).

What else?

I am using FeedDaemon and let me tell you this – it is a great tool for avid blog reader like me and you. http://www.newsgator.com is the place that you should visit and download FeedDaemon and use it, you would not repent. By the way, I figured out how to make datagrid (in WPF) display drill-down data. (drill-down reports using WPF datagrid, I would post more on this later).

Monday, January 19, 2009

Speed up OS boot process using all the processors you got

Well this is a trick that most of your know already. Anyway for those who does not know this and for those who actually have a Dual Core and Quad Core machines, you could use all these processors on Windows right from the Boot process. Here is how you do it.

Go to Windows Run (Windows + R) and type in msconfig.

image

Now in the application launched, go to the Boot tab.

image

Click on the advanced options and you should see something like this.

image

My machine has two cores so I could check the Number of Processors and set the number to 2.

Hopefully this helps you.

Wednesday, January 14, 2009

Aero Glass inside Window

Taken from http://blogs.msdn.com/adam_nathan/archive/2006/05/04/589686.aspx

Modified a little such that you can extend GlassWindow instead of Window and your XAML changes from <Window to <local:GlassWindow

   1: public class GlassWindow : Window


   2:    {


   3:        protected override void OnSourceInitialized(EventArgs e)


   4:        {


   5:            base.OnSourceInitialized(e);


   6:            GlassHelper.ExtendGlassFrame(this, new Thickness(-1));


   7:        }


   8:    }


   9:    public class GlassHelper


  10:    {


  11:        public static bool ExtendGlassFrame(Window window, Thickness margin)


  12:        {


  13:            if (!DwmIsCompositionEnabled())


  14:                return false;


  15:  


  16:            IntPtr hwnd = new WindowInteropHelper(window).Handle;


  17:            if (hwnd == IntPtr.Zero)


  18:                throw new InvalidOperationException("The Window must be shown before extending glass.");


  19:  


  20:            //Set the background to transparent from both the WPF and Win32 perspectives


  21:            //window.Background = Brushes.Transparent;


  22:            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;


  23:  


  24:            MARGINS margins = new MARGINS(margin);


  25:            DwmExtendFrameIntoClientArea(hwnd, ref margins);


  26:            return true;


  27:        }


  28:  


  29:        [DllImport("dwmapi.dll", PreserveSig = false)]


  30:        static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);


  31:  


  32:        [DllImport("dwmapi.dll", PreserveSig = false)]


  33:        static extern bool DwmIsCompositionEnabled();


  34:    }


  35:    struct MARGINS


  36:    {


  37:        public MARGINS(Thickness t)


  38:        {


  39:            Left = (int)t.Left;


  40:            Right = (int)t.Right;


  41:            Top = (int)t.Top;


  42:            Bottom = (int)t.Bottom;


  43:        }


  44:        public int Left;


  45:        public int Right;


  46:        public int Top;


  47:        public int Bottom;


  48:    }




usage





   1: /// <summary>


   2:    /// Interaction logic for Window1.xaml


   3:    /// </summary>


   4:    public partial class Window1 : GlassWindow


   5:    {


   6:        public Window1()


   7:        {


   8:            InitializeComponent();


   9:        }


  10:    }




XAML changes to





   1: <local:GlassWindow x:Class="TryUserapp.Window1"


   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"


   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"


   4:     Title="Window1" Height="600" Width="600"


   5:     xmlns:local="clr-namespace:TryUserapp" WindowStyle="None"


   6:     >


   7:     <local:GlassWindow.Background>


   8:         <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">            


   9:             <GradientStop Color="#CC101010" Offset="1"/>            


  10:             <GradientStop Color="#AA101010" Offset="0.01"/>            


  11:         </LinearGradientBrush>


  12:     </local:GlassWindow.Background>


  13:     <!-- -->


  14: </local:GlassWindow>