Friday, September 9, 2011

Dependency Injection (DI) – How and Why

There are two ways to implement dependency injection (DI) – through the class's constructor or its property setter. In class's constructor or the setter, instead of passing in a concrete class type parameter, use an interface. This way, you can later pass in different class objects that implement this same interface. The decoupling between the class constructor / setter and a concrete type provides the needed flexibility and scalability.

With this design, your classes are more unit-testable. You can create a mock class to implement the same interface as the real one, and pass in the mock object to the constructor or the setter. In the mock class, instead of connecting to a physical database server, you can set sample values as you wish. However, this would not eliminate the necessities to do your integration testing by connecting to the real database later.

Overall, the usage of DI will make your class handle less and less dependencies with concrete types, thus make it much more scalable.

understanding NULL Pattern

It is easy to miss a NULL check in our code especially when the code is not fully tested. Will it be nice to call one object's property or method without checking if the object is NULL or not without throwing NullReferenceException? The answer is to implement NULL pattern.

How?

Creat an interface for your real and null-check classes. In the real class, do real implementations. In the other null-check class, put in same signatures as the real one, but with no code, or return some default values. The callers would interact with the interfaces only. If the object is null, by calling its method, the null-check class will just do nothing or return default values. As a result, your code would have lot less IF tests for NULL references, and look more elegant and compact. 

However, doing null pattern will create more work initially. 

Here is the useful reference where I learned all about this - http://sourcemaking.com/design_patterns/null_object

Friday, August 5, 2011

understanding AJAX timeout settings for .Net application

Why bother? If you use the default Ajax timeout setting, which is 110 seconds (or 90 seconds for .Net framework 1.0/1.1), users may get the timeout error, especially when they are using the web application with a slow network connection.

In order to set the AJAX timeout settings, you would need to check three things in your code

1) add below code in the web.config file - for example to set it to 10 minutes (600 seconds).

- <httpRuntime executionTimeout="600" maxRequestLength="40960"/>

2) this above executionTimeout setting only applies if you set the "debug" attribute in the <Compilation> tag to "False" in the web.config. Here is the reference

- http://msdn.microsoft.com/en-us/library/e1f13641.aspx

3) you would also need to set AsyncPostBackTimeout in the aspx page (or the site.master page) to match with the setting in the web.config file.

- <asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="600">
...
</asp:ScriptManager>

This is one type of hidden bugs, if your developer's or QC's network speed is faster than your end-users. :)

Of course, you would also need to watch out the timeout settings for your website and web services, if any.

Thursday, June 30, 2011

SSRS, ReportViewer, RDLC, and using object(s) as data source(s)

Lately, worked on a project to create some reports. Chose to use SSRS RDLC (local processing report) and decided to explore the usage of objects as data source. I am very pleased with what RDLC has to offer. It is super easy to bind objects (dlls) to reports. With RDLC, you no longer need to publish reports separately to a report server. Instead, reports become part of  application deployment. This certainly cuts down the deployment cycle time. ReportViewer control, ReportDataSource and ReportParameter classes are the main tools or libraries to interact with.  For one single RDLC, you can work with multiple data sources and parameters. Parameters can be any data types. String array is used to pass values to a parameter for multiple selections. When pass in datetime as a parameter, you don't need to do string convertion, as long as the string is well-formatted. It is trivia, but makes developer's life easier.

There are many benefits of using objects as report's data source. You can re-use your existing data service classes intended for presentation layer to retrieve the same data. You can use nested objects for your report and sub-reports. LINQ becomes available to allow you further filter or join data. The amount of code in SQL stored procedures can be significantly reduced. Code is more unit testable. However, it took me a while to figure out how to update the data sources after made changes to my objects. You would need to re-compile the dll, and then go inside of the RDLC to refresh each data source. The top level refresh button in the Report Data View panel is not working.

SSRS is my favorite reporting tool comparing to others. I have years experiences in using other reporting tools, like Crystal, SAP BEx, Visual Composer. The built-in functions in SSRS are extensive and easy to use. I could calculate anything anyway I wanted and where I wanted. There are always some tricks you can find to get the job done nicely ranging from formatting, sorting to aggregations either at group or detailed level. These reports have the same built-in UI comes with the ReportViewer to allow export data in many format - PDF, Excel, Word...

Overall, this alternative way of delivering canned reports makes development and deployment life-cycle relatively simpler comparing to the traditional server-based SSRS reports.

SSRS, ReportViewer, RDLC, and using object(s) as data source(s)

Lately, worked on a project to create some reports. Chose to use SSRS RDLC (local processing report) and decided to explore the usage of objects as data source. I am very pleased with what RDLC has to offer. It is super easy to bind objects (dlls) to reports. With RDLC, you no longer need to publish reports separately to a report server. Instead, reports become part of  application deployment. This certainly cuts down the deployment cycle time. ReportViewer control, ReportDataSource and ReportParameter classes are the main tools or libraries to interact with.  For one single RDLC, you can work with multiple data sources and parameters. Parameters can be any data types. String array is used to pass values to a parameter for multiple selections. When pass in datetime as a parameter, you don't need to do string convertion, as long as the string is well-formatted. It is trivia, but makes developer's life easier.

There are many benefits of using objects as report's data source. You can re-use your existing data service classes intended for presentation layer to retrieve the same data. You can use nested objects for your report and sub-reports. LINQ becomes available to allow you further filter or join data. The amount of code in SQL stored procedures can be significantly reduced. Code is more unit testable. However, it took me a while to figure out how to update the data sources after made changes to my objects. You would need to re-compile the dll, and then go inside of the RDLC to refresh each data source. The top level refresh button in the Report Data View panel is not working.

SSRS is my favorite reporting tool comparing to others. I have years experiences in using other reporting tools, like Crystal, SAP BEx, Visual Composer. The built-in functions in SSRS are extensive and easy to use. I could calculate anything anyway I wanted and where I wanted. There are always some tricks you can find to get the job done nicely ranging from formatting, sorting to aggregations either at group or detailed level. These reports have the same built-in UI comes with the ReportViewer to allow export data in many format - PDF, Excel, Word...

Overall, this alternative way of delivering canned reports makes development and deployment life-cycle relatively simpler comparing to the traditional server-based SSRS reports.

Wednesday, March 2, 2011

my MVC 3 journey

Went to Seattle last week to attend a very cool workshop. Exposed to many new terms, technologies and tools used in a demo project. Summarize some of them as below:

1) Razor (cshtml) - Wondering why we no longer saw any aspx files in that project, but only cshtml files. Well, welcome to the new Razor world. Did more reading about Razor afterwards. It looks very neat and compact. Here is one of the best blogs about Razor for beginners - Introducing "Razor" – a new view engine for ASP.NET from Scott Gu.

2) EF Code First - the coolest thing about this is that - you don't need to create database or tables beforehand. When you run your application first time, if it cannot find the database and the tables, it will create them based on your classe model definitions. Like a magic! Of course, you would need to wire the db connection in the web.config file. Further more, if you make changes to any classes (add / remove properties), you can also wire the application to allow drop and recreate the database in the Application_Start() event using "DbDatabase.SetInitializer". For sure, this only makes sense in the testing / development environment, where losing data is not an issue. One more tip, you can override default data types by using System.Data.Entity.ModelConfiguration, and method OnModelCreating().

3) Validation - The validations in MVC 3 look much simplier and less code comparing to what we are using right now. Microsoft takes care of both the client-side and server-side (in case the JavaScript is disabled in user's browser) validations for you. All you need to do is to add the decorations above your class properties like this - [Required(ErrorMessage = "This field XXXXX is required")]. Availabel built-in validations include "Required", "StringLength", "Range"... Cool!

4) QUnit - how can you do unit test if all the logics are moved into JavaScript. Qunit or Jasmine are used in the demo. Having tools like these is an absolute step-up in the JavaScript world.

Finally, here is the link to talk about all the related technlogies included in this project Announcing release of ASP.NET MVC 3, IIS Express, SQL CE 4, Web Farm Framework, Orchard, WebMatrix , again from Scott Gu.

This is one of the best workshops. Feeling smarter now. ;)