Tuesday, February 12, 2008

IConfigurationSectionHandler is deprecated on .NET 2.0

I just heard a bad news while reading MCTS Book and MSDN.

It said that IConfugurationSectionHandler is deprecated on .Net 2.0.

We need to inherits from ConfigurationSection instead of implementing IConfigurationSectionHandler.

Somehow it is easier using ConfigurationSection but it is less customization.
  1. We can not have CDATA or text Element anymore. All value need to be placed on attribute.
    Otherwise it will throw this exception
    "The configuration section cannot contain a CDATA or text element."

    If implementing IConfigurationSectionHandler we can easily using XmlElement Declaration . [XmlElement("ElementName")]

  2. for multiple element values , We need to write ConfigurationElementCollection.

    public class ValueCollection : ConfigurationElementCollection
    {
    protected override ConfigurationElement CreateNewElement()
    {
    return new ValueElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
    return ((ValueElement)element).Value;
    }
    }

    public class ValueElement : ConfigurationElement
    {
    [ConfigurationProperty("value")]
    public string Value
    {
    get { return (String)this["value"]; }
    set { this["value"] = value; }
    }
    }

    [ConfigurationProperty("domains")]
    public ValueCollection Domains
    {
    get { return (ValueCollection)this["domains"]; }
    }

    Or you can use default configuration collection such as NameValueConfigurationCollection or KeyValueConfigurationCollection


    But If using IConfigurationSectionHandler we can just use XmlElementAttribute
    [XmlElement("ExtractColumn")]
    public ExtractColumn[] ExtractColumns;

====================
However The nice thing of using ConfigurationSection is
  1. Standard .Net Configuration
  2. Easier to validate and better error Messages
    [ConfigurationProperty("myAttrib1", DefaultValue = "test", IsRequired = true)]
    [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]

Sunday, February 10, 2008

MVC Framework for ASP.NET is coming

From my previous blog, I just read that Microsoft will provide MVC Framework for AsP.Net Developer.

Here is the good link for MVC Framework
http://weblogs.asp.net/scottgu/archive/2007/10/14/asp-net-mvc-framework.aspx

Wednesday, February 6, 2008

MVC VS MVP vs Passive View

As you know MVC is a Model View Controller Pattern. which focus on the controller to update the view trigger by event when model changed/ updated.

MVP is the variation of MVC where controller can hang together with View (Called Presenter).
This makes the UI more interactive. such as in Asp.net control, user can interact with the model interactively using the presenter.

Passive view is also the variation of both. It will enhance the testability. It does not have dependencies with View and Controller and all user responses are handled by the controller

Free sorting with CSS Friendly and Object Datasource

I just find a good article where you can implement sorting and paging easily using object data source.

Object data source will not do binding everytime postback anymore and It instantiate inside your page control.
http://damianpedwards.spaces.live.com/blog/cns!A079DE667E1958B3!567.entry#post



All the presentation is implemented using CSS Friendly.
http://damianpedwards.spaces.live.com/blog/cns!A079DE667E1958B3!562.entry

Tuesday, February 5, 2008

Trigger Javascript inside Ajax Partial Postback called

To run / trigger a javascript after ajax partial postback, We need to register our script in our code behind.

Here is the code :
//To find your script manager which belong to master pages
ScriptManager sm = Master.FindControl("ScriptManager1") as ScriptManager;

ScriptManager.RegisterStartupScript(this, this.GetType(), "title script", string.Format("javascript:window.open(\"{0}\",\"_blank\");", "kajaxnet.blogspot.com"), true);

Monday, February 4, 2008

Dig into Data Source

Data Source (Object Data Source and Sql Data Source) in .net 2.0 includes free sorting and paging. Here is a very good example the benefit of using data source.
http://damianpedwards.spaces.live.com/blog/cns!A079DE667E1958B3!562.entry

Using Data Source we can use CSS friendly to structure and display our sorting and paging nicely.


However there is overhead when you implementing AJAX type of work.
  1. DataSource will trigger your select method, everytime, it is postback.
    If you use Data binding using data table / other data source , you can control manually so that it only bind when you need to refresh your data or there is data changes.

  2. Object Data Source is isolated from your page control. because It create a new instance of data source every time it postback. so you can not share your data into your page control. If you want to share you need to share it using session/ cache. View state/ class variable can not be shared.

Dynamically sorting your data easily using Linq & Lamda

Here is 1 line of code to sort data dynamically based on sorting parameter A

YourProducts.OrderByDescending(p => p.GetType().GetProperty(A.GetValue(c,null))

It will get your property value of Product class dynamically.

Here are some example to use lamda expression in C# 3.0:

x => x + 1 // Implicitly typed, expression body

x => { return x + 1; } // Implicitly typed, statement body

(int x) => x + 1 // Explicitly typed, expression body

(int x) => { return x + 1; } // Explicitly typed, statement body

(x, y) => x * y // Multiple parameters

() => Console.WriteLine() // No parameters