My presentation on AOP in .NET with PostSharp

I was invited by Adil Mughal, user group lead of Emerging .NET Developers to speak on “Aspect Oriented Programming in .NET with PostSharp” at Microsoft Innovation Center, FAST-National University, Karachi.

I started the talk by discussing how we code traditionally in OOP that results in boiler-plate and highly coupled code, then I introduced Aspect Oriented Programming, defined Cross-cutting concerns and that AOP results in true separation of concerns by transforming and encapsulating cross-cutting concerns into Aspects.

I introduced PostSharp and how well it integrates into Visual Studio 2010 then showed demos. I also explained Joint-point and Advices concepts in AOP, then answered some questions.

I totally enjoyed doing this talk, thanks to all those who attended.

Download the code samples, slides below and find some links to further learning resources.

Caching Aspect demo
Exception-handling Aspect demo

Learning Resources

http://www.sharpcrafters.com/postsharp/documentation/screencasts
http://www.sharpcrafters.com/postsharp/documentation#blogposts
http://www.infoq.com/presentations/Advanced-AOP
http://dimecasts.net/Casts/ByTag/PostSharp
5 Ways That Postsharp Can SOLIDify Your Code series


Update
Event summary has been posted on Emerging .NET Developers group page

ASP.NET 4.5 Preview–Improvements in the core services

I have blogged about some of the new features in ASP.NET 4.5 WebForms, during Microsoft’s Build conference the first preview of .NET 4.5 was made available for download, besides WebForms there also have been significant improvements in the ASP.NET Core, including:

Note that this is still a developer preview release which means it is not even beta so features may be added or removed.

I’m excited to see enhancements like these coming to ASP.NET 4.5 in the near future.

Related links:

Profiling ASP.NET website in Visual Studio with Performance Wizard

Those who use Visual Studio 2010 may know that it now ships with a performance analysis tool that instruments your application in various ways.

You can run from Analyze > Launch Performance Wizard

Launch_perf

 

 

 

 

If you are profiling an ASP.NET website project then you must open it from File > Open Website > Local IIS

local_iis

 

Though there is one problem, if you do not have the right options enabled in Windows you might get an exception that reads

Error
VSP 7008: ASP.net exception: "The website metabase contains unexpected information or you do not have permission to access the metabase.  You must be a member of the Administrators group on the local computer to access the IIS metabase. Therefore, you cannot create or open a local IIS Web site.  If you have Read, Write, and Modify Permissions for the folder where the files are located, you can create a file system web site that points to the folder in order to proceed."

On this Visual Studio Profiler team blog post I found the right options that I needed to enable.

To check this in Windows 7:

  1. Open ‘Control Panel\Programs\Programs and Features’ (or run ‘appwiz.cpl’).
  2. Choose ‘Turn Windows features on or off’.
  3. In the ‘Internet Information Services’ section, make sure that the following options are selected.

After selecting the options re-launch the profiler, Visual Studio builds the solution, launches a browser window, browse some of the pages of your website while Visual studio records in the background, close the browser or hit Stop in visual studio to get the report.

Hope it helps you, let me know.

New features and improvements in ASP.NET vNext

I watched Damien Edward’s excellent presentation and learned about the new features that will be available to us in the vNext of ASP.NET Webforms, if you like to get the full details I encourage you to watch the video. Here I have summarized the main features.

Strongly typed Data control and Model Binding support

Using these you will be able to bind strongly typed models to your datacontrols and get full intellisense for them.

Image(31)[5]

Image(1)[5]

You can also tell the controls to get their data from a method


Image(32)[4]

In your codebehind file you define your method like this, this works great with Entity Framework 4.1

Image(3)[4]

Add Querystring value with method parameter

Another improvement that is coming up is the support of attributes in method arguments as in the following code.

It is looking under the querystring collection for the key minProductsCount, converting it to an integer if found or leaving null because it is nullable, saving a lot lines of code that we write today.

Image(4)[4]

Image(5)[4]

Take the above value from a control and other collections

There will be support for other attributes as well, suppose you want to get the value from a control of the same name, a Form Collection, Cookie, ViewState even from your custom attribute and so on

Image(6)[4] Image(7)[4]
Image(8)[4] Image(9)[4]
Image(10)[4]  

Change parameter id

By default it uses the parameter name but if your control’s id is different then your parameter name, you can specify the id like this

Image(11)[4]

Master/Detail scenario in GridView

Have another grid view bound to the master gridview, define the select method on the detail gridview, then tell the method to pull the selected id from the master gridview

Image(15)[4]

Image(16)[4]

The results look like this

Image(17)[4]

Update support

Image(18)[4]

Image(33)[4]

Because there is no info of the Product from the gridview due to the databinding expressions, the Products property is null so instead of binding Product info to the gridview and getting it back on postback, we’ll have the following method

TryUpdateModel(category)

Now the product collection still exists, just update this model with the info from the gridview and save

Image(20)[4]

Adding Validation using Data Annotations

Image(21)[4] Image(22)[4]
Save only if the model is in valid state or reload the category

Image(23)[4]

Adding custom model exception on Db.SaveChanges();

For example if you have a unique key constraint on your table then you will push the exception from Entity Framework into the Model state to be able to show the error on page in gridview, like this.

Image(24)[4]

Image(25)[4]

Support to bind the Gridview to Dynamic is coming up

Image(26)[4]

Async in WebForms vNext

New methods and keywords are coming up for improving asynchronous communications

Image(28)[4]

The call will return immediately and the results will be filled in later

Chaining three async method calls together then returning the results, using PageAsyncTask

Image(29)[4]

The result of those three files loaded looks like this

Image(30)[4]

There’s more

Unobtrusive built in validators – no inline javascript, AntiXSS Encoding, Html5 updates and runtime combination and minification of Javascript and Css files is coming up

Summary

  • Webform is not dead
  • Improved data binding via Strongly typed data controls and model binders
  • Easier Async programming coming to ASP.NET

..and much more

How and why I changed my coworker’s code

The other day I set myself a goal to change a coworkers code, which to be honest did the job but ranked low on my acceptability scale because it did not match the coding guidelines, in addition I was required to use and build on top of that, I decided fix that so it matches the rest of the application code.

Though the intention was to improve the code, I made clear to the team that I have set aside few hours initially to see where I’m going and if I can’t start to fix it, I’ll leave it as it is, because it worked.

I had a vision of the resulting code should look like. I started top down, fixing the outer functions first then went to nested loops and more loops, to make sure I knew what I was going to change I got clarification about that piece of code from the coworker who originally wrote it until I fixed everything.

The goal was to not only improve the code without breaking anything but also motivate developers to think outside the box and not be afraid to experiment.