Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries for August, 2010

Optional Parameters in C# 4.0

Some members of the C# community were discussing optional parameters and the implications for future versions when optional parameter values change.  In short, you need to realize that changing the value of optional parameters in a public API is a change that is observable at client code. The ramifications vary greatly, from “no big” to “stop the world”. I’ll give a brief explanation of how the feature works, and what you need to watch for and how to separate reasonable caution from irrational fear from using a feature.

While most modern programming languages provide some way of declaring optional function parameters, C# doesn’t provide a way of directly doing so, despite the fact that VB.NET and .NET’s attribute system both support this functionality. This is a subject of some debate currently in the C# community. The C# development team’s position seems to boil down to the following: When provided, this feature is usually nothing more than dressing up method overloading with a little syntactic sugar.

When you get right down to it, their position makes some sense. A function with an optional parameter is in reality two different functions: one that assumes some default behavior if the optional parameter is omitted, and another that performs more specific behavior based on the value of the optional parameter if provided. But that doesn’t change the fact that using overloaded methods to provide optional parameter support feels a little clunky. It works, but you always wind up writing more code, and you pollute your object interface with the extra method signatures required to support all of your optional parameters. Let’s look at some alternatives.

Displaying Markers on the Chart via Microsoft Chart Controls

01 private void BindData() {
02
03 var exams = new List<Exam>()
04 {
05 new Exam() { Name = "Exam 1", Point = 10 },
06 new Exam() { Name = "Exam 2", Point = 12 },
07 new Exam() { Name = "Exam 3", Point = 15 },
08 new Exam() { Name = "Exam 4", Point = 2 }
09 };
10
11 var series = ExamsChart.Series["ExamSeries"];
12
13 foreach (var exam in exams) {
14
15 var point = new DataPoint();
16 point.SetValueXY(exam.Name, exam.Point);
17
18 point.Label = exam.Point.ToString();
19 series.Points.Add(point);
20 }
21
22 ExamsChart.DataSource = exams;
23 ExamsChart.DataBind();
24 }
Microsoft Chart

Microsoft Chart

FAQ in BlogEngine.NET

Can BlogEngine.NET be installed within an existing website?

Yes. Install it in its own folder and configure the directory it resides in as an application in IIS.

Some hosting providers may not allow the level of trust used in BlogEngine.NET by default. If you receive an error similar to:

  • “Parser Error Message: It is an error to use a section registered as allowDefinition=’MachineToApplication’ beyond application level.”,

You can try one of the following to resolve the issue:

  • Comment out the “trust” line in web.config
  • Ask your hosting provider if they can configure the directory where BlogEngine.NET is installed as a virtual directory.
  • Ask your hosting provider to verify that the directory has been configured as an application in IIS.

How do I update the “About the Author” section of the blog?

In the default Standard theme, edit the content of this section by clicking “edit” in on the side bar of your blog.

An alternative way to display About the Author information is to create a ‘Page’ in the control panel. The Title of the Page can be About the Author. Enter information about the author in the WYSIWYG editor. Once the page has been created, you can add a link to the About page on your blog. This can be achieved by adding a Page List widget, or by adding a TextBox widget with a hyperlink to the About page, or by editing your theme file (site.master) and adding a hyperlink to the About page.

Is BlogEngine.NET open source and completely free?

Yes. BlogEngine.NET is built by passionate developers who have too much spare time, just to make an open source blog engine to give away absolutely free.

Is my mother able to use it?

Yes. We have gone to great lengths to make BlogEngine.NET as easy as possible to use &ndash; both from an end user as well as a developer or theme designer’s point of view.

What are the demands for the web server?

The only thing needed to run BlogEngine.NET is a web server that support ASP.NET 2.0 and write permissions on the App_Data folder.

What database is it running on?

None. BlogEngine.NET uses XML to store all posts, pages etc. by default. However, if you prefer to use a database, BlogEngine.NET includes a “DbBlogProvider” that allows you to store data in databases which support standard SQL — MS SQL Server, MySql, SQLite and Vista DB among many others. Configuration changes necessary to store data in a SQL Server database can be found. If there isn’t a data provider already available, you can easily write your own provider. We have enginereed our framework to make this very easy and simple to do.

How can I switch where data is stored (XML to Database or vice versa)?

If you’re just starting off with BlogEngine.NET, all of your data will be stored in XML files in your App_Data folder. Some web hosts such as GoDaddy who have an automatic BlogEngine.NET setup option, might setup your blog so data is stored in a database instead. If your blog is new, you don’t yet have any data, and you want to switch from XML storage to Database storage

Entity Framework 4

Some of the big improvements in EF4 include:

  • POCO Support: You can now define entities without requiring base classes or data persistence attributes.
  • Lazy Loading Support: You can now load sub-objects of a model on demand instead of loading them up front.
  • N-Tier Support and Self-Tracking Entities: Handle scenarios where entities flow across tiers or stateless web calls.
  • Better SQL Generation and SPROC support: EF4 executes better SQL, and includes better integration with SPROCs
  • Automatic Pluralization Support: EF4 includes automatic pluralization support of tables (e.g. Categories->Category).
  • Improved Testability: EF4’s object context can now be more easily faked using interfaces.
  • Improved LINQ Operator Support: EF4 now offers full support for LINQ operators.

EF4 enables you to:

  • Develop without ever having to open a designer or define an XML mapping file
  • Define your model objects by simply writing “plain old classes” with no base classes required
  • Use a “convention over configuration” approach that enables database persistence without explicitly configuring anything
  • Optionally override the convention-based persistence and use a fluent code API to fully customize the persistence mapping