Wednesday, October 16, 2013

Best Practices for ORM

Although everyone puts their complete effort to develop the best software, sometimes bad things happen. But to be on safer side we can take some precautionary measures. Well, today I want to share some of the best practices which needs to be followed while dealing with any ORMs (Object Relational Models). Most of these are common among many of the relational databases.
  1. Exception handling: One should handle only those exceptions which need to be handled, and let all others pass through. For example, if you want to handle a connection issue you should do this by catching the specific exceptions, but don't use a catch all on Exceptions with the clause "just in case" because it may lead to high performance issues.
  2. USING: As many of us know, that USING automatically handles dispose functionality. But this statement doesn’t work all the time, especially in the case of database connections. If you want to try it out, then run some overnight processes using windows services. Although USING handles it most of the time it is always a good practice to close database handles/connections manually.
  3. Prefer compiled LINQ queries: Compiling a query once and executing it several times can lead to high performance gains. Such queries are useful, especially when the same query needs to be accomplished numerous times for innumerable parameters. The syntax for that is: MORE...

Wednesday, September 25, 2013

New Features in Vistual Studio 2013 Release Candidate

It has been great movement, great work done by VS team for making easy to use Visual Studio. There has been lot of new features in Visual Studio 2013 preview, which has some very cool features in XAML, intellisence, goto definition and many more. But in RC, most of these features are extended. Today I'll try to cover as much as possible about all these new features. So, let's start by taking them one-by-one.

1) Intellisence in XAML - Earlier versions of VS were pretty cool but I hope, whoever working in WPF would have surely miss intellisence feature. But in RC, that is no more a limitation. So, if we let editor know where the data is, it will help us out like a friend and will make our work easier. VS2013 rc supports intellisence for DataBinding and Resources. Let's have a look of this feature in below screen shot:


Intellisence for DataBinding




  



Intellisence for Resources




2) Go To Definition in XAML - This new feature in available in VS2013 RC on right click of relevant object or by pressing F12 key in XAML. If you are pressing F12 on built in control, then it will pop-ups an Object Browser. This feature works for any valid user defined objects as well as for User controls. Below screen shot will give you better insight of the feature.









3) Insert snippet - Another cool feature is "Insert snippet...". If you will right click on XAML file, you will get this new feature in context menu. So, if you have any snippet, you can insert it or you can use any built-in snippets.
4) Intellisence matching - Let's say instead of typing DockPanel, you typed ockPanel. Well, from now onwards this will be taken care by VS2013. It will autmatically detect ockPanel as DockPanel. You can also type by lowercase and it will be automatically rectified by editor. It's not 100% matching, but yeah, it's still very helpful. Isn't it cool? Let's have a look at below figure for better understanding.












5) Refactoring - VS2013 RC also provide us the refactoring capabilities. Let's say, you want to change your Stackpanel to some other container. In this case, as soon as you will change starting or ending node, the other one will automatically get changed. This is one of the awesome feature, which reduces lot of typing.












6) Type Script - Another major enhancement is done in the area of Type Script (TS). Hope most of you are aware of TS. When you build your application, TS code automatically gets converted to JavaScript code. As TS is very easy to write, one can write code in TS and later get it converted on application build. To check out this enhancement, you can create a web application and get this Type Script option on right click of Scripts folder or in 'Add New Item' dialog. This feature was available in VS2012, but instead of integrated version, it asks you to download from Nuget or from http://www.typescriptlang.org/. But in VS2013 RC, one need not to download any external tool because this is built with the product itself as shown in below figure. The good thing is, VS2013 supports, intellisence for type script too.












7) Peek Definition - This is the new capability, which was added in Visual Studio 2013 preview. It allows you to view the other source file from your opened source code file. Let's understand this in another way and forget about this Peek Definition feature. With current editions of Visual Studio, if one want to see the information about particular method or class or any object from our current file, then only two options are available as either by using F12 or by using Go to Definition. Now problem here is, if the file is very huge then there are very high chances that user will loose the context or might forget where he/she was. So, Visual Studio team came up with a very brilliant idea of showing the object's information in the current window itself as shown below. Let's say developer wants to see the information about Employee class, then -















Here you might have noticed that being in the same window, user is able to view the source code of another object via an inline window. Infact, you can go further down in a nested way and can go back and forth among those nested windows also.

Another best thing is, user can also do code modification in these inline window and similarly, there are little other optimizations are done around the coded window.

8) CodeLens capability - If your Visual Studio 2013 is connected to Team Foundation Server (TFS) then you will get lot of additional features which can help developers as well as reviewers to ease their task. So, let's look at the source code in VS2013, you will find a small header at the top as shown below:





In above screenshot, I have a function named BasicEmployeeViewModel which has 5 references, 3 unit test cases. Now if you will click on references, it will show you all the references as:










By clicking on any of the references above, you can jump on to that particular part of code.
Now let's quickly jump on to another screenshot for unit test case:






By clicking on Run All, developer can run all the unit test cases at once. So, whenever you are making a change to your code, at the same time, you can run your unit test cases also.

The third part of codelens is available, only and only when your Visual Studio is configured with TFS. This part of codelens says as who has last modified this method. Let's see below screen shot:





Here developer can identify the person, who has last touched this method. This feature is also available at class level also. And in above figure, one can also get to know about the number of changes and what all the changes are done as shown below:






One more fabulous thing is, if you want to connect to any of the authors listed above, then you can click on that icon (orange square) and can communicate with him or her. If require, you can chat or e-mail to that concerned person. Definitely, here you will need a Lync integration ;)

9) Addition of Charts in TFS- There are few enhancements done in web interface of TFS. Below is the screen shot of web interface 2013 RC.















As part of release candidate, a new Chart capability is added on Queries tab as shown below:













This is a web based chart and one can select the chart of the choice from the given list as Pie chart, stack chart, etc. Now on click of queries, you will get the list of all the assigned items. As of now, only one item is assigned to my name, so only 1 is listed here as:










Now, when you will click on Charts, you will get the corresponding chart with all the items assigned to you. You can also select the group items for which you want to create a chart and it seems to be very flexible. Below shown is the Pie chart -
FIGURE
10) Addition of Features in TFS - One more feature is added into Backlog management as below:





Feature can be think of as one higher level abstraction. So, let's say you have one or more features, backlogs, then you can sort them by categories and can do lot more than this.










11) Others - 
Apart from all above. there were lot of major enhancements done in Office365 to host LoB apps in much more easier way. To meet this goal, a new template is added for building business apps having lot of plumbing done. This is not a 100% business app, but we can say, it's a good start.

Hope above list of features gave you a gist of Visual Studio 2013. So, let's wait for the release of VISUAL STUDIO 2013 :)


Thursday, September 12, 2013

Visual Studio 2013 RC is available for download

Visual Studio 2013 Release Candidate is available for download now. This release includes new tools for line of business applications, Web applications, games, mobile devices, services and of course applications targeting our latest platform Windows 8.1. With the Visual Studio 2013 Release Candidate (RC) and Windows 8.1 RTM for MSDN and TechNet subscribers, developers now have everything they need to start building and testing  the next generation applications for the Windows Platform. More...

Wednesday, September 4, 2013

WPF Error: IOException - Cannot locate resource

Recently I worked on a WPF application which supports localization using LocBAML tool. I created a sample application, having a very simple UI, consist of a button inside a window. Here I am assuming that most of you are aware on how to generate satellite assemblies. So, quickly coming to the point, when I build my application, build was successful. But when I launched, my application crashed with reason stated as IOException :(


















After spending almost an hour, I came to know that it was due to Culture settings. Let's have a look at my code first:











Apart from this above code, I also updated my project file (.csproj) for development language so that my satellite assembly contains the neutral language resources. Following line was added in the project file:
<UICulture>en-US</UICulture>

Apart from this, I did nothing special in my app. After hitting my head on msdn, I got an idea on why culture changes was causing an exception.

Well, no need to get panic. Solution is pretty simple. Let's move ahead and  open your assembly.info.cs file and uncomment below line:
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

After this minor change, we are good to go.
By uncommenting this line, we are telling runtime that culture information needs to be read from satellite assembly.

Hope, this tip will save your time :)


Saturday, August 31, 2013

Silly but useful stuff - Part 5 (Web)

Reduce the data sent across the network 
Reducing the amount of data sent across the network can improve application performance significantly. Compressing CSS and JavaScript is possible using bundling and minification. This will reduce the number of server requests and the amount of code sent across the wire.

Saturday, August 3, 2013

Silly but useful stuff - Part 4 (Database)

As a developer you may or may not need to go into the database and write queries or design tables and indexes, or help determine configuration of your SQL Server systems. But if you do, these tips should help to make that a more pain free process. 

a) Since the code generated from the ORM can frequently be ad hoc, ensure that the SQL Server instance has ‘Optimize for Ad Hoc’ enabled. This will store a plan stub in memory the first time a query is passed, rather than storing a full plan. This can help with memory management.

b) Ensure your variables and parameters are the same data types as the columns. An implicit or explicit conversion can lead to table scans and slow performance.

c) You get exactly one clustered index on a table. Ensure you have it in the right place. First choice is the most frequently accessed column, which may or may not be the primary key. Second choice is a column that structures the storage in a way that helps performance. This is a must for partitioning data.

d) Performance is enhanced when indexes are placed on columns used in WHERE, JOIN, ORDER BY, GROUP, and TOP. Always test to ensure that the index does help performance.


Hope above tips were helpful :)

Wednesday, May 29, 2013

WPF: TemplateBinding with ControlTemplate

Today I'll try to write bit on TemplateBinding and how to use it inside a ControlTemplate. TemplateBinding is a type of binding used mainly for template scenarios. Here I am not going to write more on it's theoretical aspect as what is TemplateBinding, when to use, blah blah blah, as lot of content is readily available on net.  So, let's start quickly onto coding part:

First of all, let's create a new project using WPF template and place a button in it as below:
Now, what I am going to do is, I am going to replace this content template for this button. So, in order to do this, open up the Button tag and add Button.Template markup tag with a new ControlTemplate as:












Now as soon as you will add ControlTemplate tag, you will notice that the content of the button is gone and button is shown as a transparent rectangle. This is happened because here I told WPF to replace the default ControlTemplate with the one, which I defined. But at this point, our ControlTemplate is blank, so there is no visualization and we can see only a transparent rectangle.

Now go ahead and customize our ControlTemplate by putting Ellipse inside it as:








Now we can see that, we get a visualization for a button in form of ellipse. At this point of time, it works OK, but there are scenarios where this struct breaks down.

For example, Let's increase the height of button, from 35 to 105 as:








In above image, you will notice that button height is increased but the ellipse size is still the same, which is a bad UI design. And the reason this is happening is, inside a ControlTemplate, the height of an ellipse is hard coding. So, no matter, whatever height is set at parent (i.e. Button), it will not get inherited to child control (i.e. Ellipse).

So, now we have to fix this problem by using a special type of binding called TemplateBinding inside ControlTemplate. So, instead of hard coding the height, we will use TemplateBinding as shown below:









By setting the TargetType property of ControlTemplate, we are telling Ellipse that, any property of  Button can be set to ellipse. Now, whatever the height of button will be, it will automatically be the height of ellipse also. Isn't it interesting?

Moving forward, let's do something more interesting with Fill property of ellipse.














In above snippet, I am trying to set the Fill property of an ellipse using TemplateBinding. But now problem here is, a button doesn't have a Fill property. So, there is no one-to-one mapping for Fill property. Now, what to do ?

No need to worry that much because button does has a Background property as:









In above image, you might have notice that as soon as ellipse's Fill property is set to Background, ellipse becomes transparent as button's background. Now if we set button's Background property to Red, the same will be applied to ellipse too.












So, one can understand that how much magic we can do with TemplateBinding.
Now, let's work little bit on code cleanup.

ControlTemplate inside resource dictionary:
For better code readability, we will move out our ControlTemplate code and put it inside a resource dictionary as:











So, now we can see as earlier that whatever visual property for button is set, all get applied to ellipse also.

Hope this article was useful and gave you the basic idea on how to use TemplateBinding.


Sunday, May 26, 2013

ReLive TechEd

This weekend, I got a chance to attend an event 'ReLive TechEd' organised by very talented folks of B.Net and Microsoft community. This event goes for close to 5 hours and gives us the glance of latest Microsoft technologies. The most effective thing was that all the sessions were given by the professionals who actually presented in Microsoft TechEd 2013. This year I missed the TechEd, but I am very happy and thankful to B.Net team, who organized it back and that is also totally free of cost :) 

Friday, May 17, 2013

Windows Phone App Design Principles

Hope most of you might have heard DOUGLAS MARTIN's quote:
         "Questions about whether design is  necessary or affordable are quite beside the point: Design is inevitable. The alternative to good design is bad design, not no design at all."

And  VICTOR PAPANEK says: 
       "Design is the conscious effort to impose meaningful order"

So, if designing is that much important then one has to be very much stern about it. Going forward, today I am pointing out some design principles for Windows Phone App. So, let's gear up.

  • Clutter free UI - Information should be well organized and user should not get congestion like feel. Make best use of typography
  • Focus should be on content and it should be in a way that it attracts the user's attention from the beginning itself
  • Try to keep only the most pertinent content on the screen because when it comes to WP design, content matters more than a chrome
  • Try to provide seamless UX from dedicated device buttons
  • Tiles should be soulful and alive, in order to achieve the better responsiveness
  • Accentuate more on enhanced touch and gesture information
  • Try to keep the smart screen updated with only the latest and germane information
  • Use proper icon/text to introduce your app
  • Always create UI mockups, before proceeding towards actual implementation
  • Once mockups are ready, never hesitate to do usability testing
  • Provide app uniformity by providing similar meaning to similar images, color, symbols, etc
  • Do spend time in branding your app - it includes logo, color, copyright info, etc.
Hope above tips were useful :)

Wednesday, May 15, 2013

Windows Phone App' s Tip - Image consideration


Images play a very significant role in any Windows Phone application. So, one should be very careful while dealing with images. As of now, Windows Phone supports only two image formats named JPG/JPEG and PNG. Now before concluding on which image format to choose, let's get into bit more depth of it.

PNG format images are non-lossy and need very little CPU effort to display because those are pixel perfect. But at the same time, huge PNG images take too much longer to read from storage and ultimately lead to slower display.
On the other hand, JPEG format images are lossy, smaller to store  and based on the compression level much more complicated decoding algorithm is required to display them.

Another point regarding image is about opacity and transparency - All the images that use transparency should be stored as PNG format because JPEG doesn’t support transparency and JPEG format should be used for all the images that are fully opaque.

Now when coming to Windows Phone apps, performance matters a lot...Isn’t it?
Well, now some best tips to make our Windows Phone app much more performant:
  • Choosing proper image format: Use JPEG for anything large and PNG for anything small, i.e. use PNG for small icons. If image transparency is not a concern then it is always recommended to use JPEG.
  • Compiling images with Build Action=Content: Whenever a new image is added to the project, the default "Build Action" is set to Resource. It is always recommended to change this "Build Action" property to Content as it will reduce the size of our DLL, resulting in speedy image and app loading.

Note: Resources are included in an assembly whereas content is included in deployment package.
  • Image size: One of the limitations of Windows Phone is the limited screen resolution. So one has to consider proper image size, in order to gain better performance

 I'll keep updating this post as and when I'll get to know about more interesting facts. I would be very happy, if you can add more on to it.