Object Reference .NET

ref this.Object

BlogEngine Flaws

clock July 27, 2008 21:46 by author Naz
Get Blog Engine I'm part of the small open source team for dashCommerce an .NET shopping cart system so please don't get me wrong this article isn't about dissing the hard work and time developers have put into BlogEngine it is just to highlight some of the the design flaws I've encountered which we should help to improve on in future versions. I will still continue to use blogengine as it's still the best out there but it can be better...

This site is currently distributed across multiple web servers sometimes referred to as “Cloud Hosting”. Many large popular websites work of this kind of architecture to keep up with demand of resources required to process user requests. I’ve read somewhere Facebook currently has around 200 distributed web servers for hosting their site at various parts of the world.

BlogEngine runs of a singleton caching model where it caches posts and comments into an static object and then reads and writes that object to either an XML or Database when required. A common problem with this design is each server does not have direct access to the in memory object. Due to this when a comment is posted on server 1 gets saved to database, server 2 doesn't know that there has been change so you wont see the update until server 2 updates itself. However there are some work-rounds the simplest is just using the standard .NET caching model instead of singleton caching as you can specify an expiration times and also set-up things like cache dependency enabling your servers can get back to sync.

Recently I’ve been having problems as the site has gotten more popular I’ve had comments and even posts disappearing randomly, sometimes re-appearing you might have experienced this yourself either on my site or in your own so after divulging into the code to see what was happening found something I thought was shocking. When you add a comment, or Rating, Approving and Remove Comments this is what BlogEngine DbBlogProvider actually does...

  1. UPDATE The post
  2. DELETE All Tags for that post
  3. INSERT All Tags for that post
  4. DELETE All Posts Categories
  5. INSERT All Posts Categories
  6. DELETE All existing comments for that post
  7. INSERT All comments back in including the new comment
  8. DELETE All post notifications
  9. INSERT All post notifications

Remember all this when all it needed to do is INSERT into Comments table the new Comment!

From what I can see (I could be wrong) it is doing it without an SqlTransaction so if something where to go wrong it wont rollback changes. This can also cause concurrancy issues say if two people are adding comments at the same time i'm not sure if it will lock it. So instead of just getting delayed updates my web servers are actually in constant battle with each other overwriting each others changes when they save content every time someone post's a comment or rates an article.

I hacked a workaround so Add Comment just does what it needs to by adding new Abstract Methods to BlogProvider but this meant I had to write the same methods for XMLProvider but XML's and relational databases are completely different so I just stubbed them out.

I hope the developers take note about this for future versions there might have been a reason for this the only thing I can think of is they tried to reduce the number of methods a BlogProvider requires.

kick it on DotNetKicks.com


Lightbox images for dashCommerce

clock July 1, 2008 21:20 by author Naz
lightbox

As of revision 142 of dashCommerce i've added a new feature on product pages to now display images using lightboxes.

As it did not make the 3.0.1 release i've made available the patch to allow you to add it if you wish to do so.

It's based on the jQuery lightbox library.

Demo -> Test Site

Patch -> Lightbox_Patch.patch (83.97 kb)

Files -->  dashCommerce_Lightbox.zip (37.13 kb)

Check in note.

"ADDED: Lightbox for product images.
1. You can now view images if js is disabled it just opens them in a new window makes it more accessible.
2. If you have large images it wont break the design on the product page like it did before.
3. You can cycle through all the images in the product using next and back links and it also displays the title for each one.
4. Don't need tiny thumbnails any more as it uses the same size one as the category page.
5. It looks really cool ;)

TODO: Remove tiny thumbnails.
"

Please report any bugs or issues here


enum to friendly string extension method

clock June 3, 2008 12:05 by author Naz
We use enums quite extensively in our application as they are great for representing integral values in a strongly typed way using symbolic names eg.

if (Status == UserStatus.NotLoggedInAYear)
    ArchiveRecord();

is a lot more clearer and meaningful making it easier to maintain than

if (Status == 3)
    ArchiveRecord()

The enum names are not very friendly to the user and I recently read a some good articles creating user friendly strings for enum values and Enum description values which tried to solve this problem.

I think i've found a better solution to this problem using extension methods.

public enum RemovalType
{
    None,
    ManAndVan,
    FullRemoval,
    FullRemovalsAndPacking
}
 
public static class RemovalTypeEnum
{
    /// <summary>
    /// Returns a friendly enum name
    /// </summary>
    public static string ToFriendlyString(this RemovalType removalType)
    {
        switch (removalType)
        {
            case RemovalType.ManAndVan:
                return "Man & Van Service";
            case RemovalType.FullRemoval:
                return "Standerd removals service";
            case RemovalType.FullRemovalsAndPacking:
                return "Full removals and packing service";
            default:
                return removalType.ToString();
        }
    }
}

RemovalType removalType = RemovalType.ManAndVan;
removalType.ToFriendlyString()

This implementation creates an extension method on the actual enum "RemovalType" which then allows you to call a ToFriendlyString() on the instance of all enums of that type I think this a more fluid implementation and also means better performance compared to the other implementations as it doesn't need to use reflection.

You will notive i've decided to leave the default return value to call ToString() on the enum value which means you only need to implement the values that need to be made friendlier.

You can also call it easily inside databinded controls in the aspx page.

<%# ((RemovalType)Eval("ServiceType")).ToFriendlyString() %>


kick it on DotNetKicks.com


We made it to asp.net front page!

clock May 23, 2008 08:00 by author Naz
Daves article on Implementing Generic Caching hit asp.net front page as article of the day for a week!



SCOPE_IDENTITY() return the id from the database on insert

clock May 22, 2008 20:15 by author Naz
As a .NET programmer most of my time is spent coding in C# and I try to avoid writing SQL where possible, which is great as we have dba's that can help with the more complicated queries. Why is it so complicated to write a loop without turning the database upside down? I'm not saying it's not possible it's just the language can me annoying at times.

Anyways, Recently I had to write an insert stored procedure and needed to return the ID of the row I was inserting. While writing my usual bad SQL I came across a fascinating function I’ve never used before, SCOPE_IDENTITY().
Here's short example of how you can use the SCOPE_IDENTITY() function while doing an SQL INSERT to return the identity value of the row just inserted.

In this example I’m inserting a new customer record using a stored procedure and have declared a output parameter which I'd like to return to my application.

CREATE PROCEDURE [dbo].[Customer_Insert]
    @Name VARCHAR(255),
    @Email VARCHAR(255),
    @Phone VARCHAR(255),
    @CustomerID INT OUTPUT
AS
BEGIN
    INSERT INTO dbo.Customer ([Name], Email, Phone)
    VALUES (@Name,@Email,@Phone)
 
    SET @CustomerID = SELECT CustomerID 
              FROM dbo.Customer 
                  WHERE [Name] = @Name 
              AND Email = @Email 
              AND Phone = @Phone
END


What I’ve done wrong here is after inserting run a select to try and SET the @CustomerID. This is a bit risky as it could return more than 1 record, or it return an of completely different record.

SQL's SCOPE_IDENTITY() function can return the last identity value inserted into an identity column in the same scope e.g. a stored procedure, trigger, function, or batch. Alternativly you can also use the @@IDENTITY function to return the last identity value inserted into an identity column regardless of the scope.

Now lets improve my stored procedure using SCOPE_IDENTITY().

CREATE PROCEDURE [dbo].[Customer_Insert]
    @Name VARCHAR(255),
    @Email VARCHAR(255),
    @Phone VARCHAR(255),
    @CustomerID INT OUTPUT
AS
BEGIN
    INSERT INTO dbo.Customer ([Name], Email, Phone)
    VALUES (@Name,@Email,@Phone)
 
    SET @CustomerID = CAST(SCOPE_IDENTITY() AS INT)
END

I've casted the value returned by SCOPE_IDENTITY() to make sure the confirms to the return parametor @CustomerID INT OUTPUT.

kick it on DotNetKicks.com


the LinqToSQL is a lie

clock May 8, 2008 21:18 by author Naz

LinqToSql is amazing! I've been working with it for the past week and loving every moment. However i've noticed somethig weird, it lies to me about the SQL it's generating. Here is my example...

Updated with better example

IEnumerable<QuoteLINQ> quote = from q in db.QuoteLINQs
                               where q.QuoteID.Equals(10)
                               select q;
 
lvQuoteDetails.DataSource = quote;
lvQuoteDetails.DataBind();


<asp:ListView ID="lvQuoteDetails" runat="server">
    <LayoutTemplate>
        <h2>Booking Details</h2>
        <asp:PlaceHolder runat="server" id="itemPlaceholder" />
    </LayoutTemplate>
    <ItemTemplate>
        Booking ID: <%# Eval("QuoteID") %><br />
        Booking Date: <%# Eval("BookingDate") %><br />
        Customer ID: <%# Eval("CustomerLINQ.CustomerID") %><br />
    </ItemTemplate>
</asp:ListView>

Linq allows you to see while you're debugging the SQL it's generating by hovering over the object. For the above code it tells me this is the generated SQL

{SELECT [t0].[QuoteID], [t0].[CustomerID], [t0].[BookingDate], [t0].[ServiceTypeID], [t0].[Comments], 
[t0].[Referer], [t0].[QuoteTypeID], [t0].[GUID]
FROM [dbo].[Quote] AS [t0]
WHERE [t0].[QuoteID] = @p0
}

Now most people will think everything's hunky dorey it's just a simple un-taxing query, but hold on... now how is it possible that in my Quote object I now have access to the customer object with all the customer details in it??

The LinqToSQL is a lie

So the generated Linq to SQL is a lie! Now my simple query as not so simple at all. What Linq actually does is fire of extra queries on request of the data. If you run SQL profiler you can see exactly what it's running. You need to be carefull accessing internal objects as Linq will go right ahead and get the information you request in another query, it's a lot safer to get everything in the initial Linq query go explicitly.

Atleast the cake is not a lie.

the cake is not a lie

kick it on DotNetKicks.com


Why can't you have handlers with multiple paths in web.config?

clock May 6, 2008 17:29 by author Naz
I have created IIS7 asset handler for my application which deals with images and stylesheets by serving them from a CMS database. For some reason the new IIS7 handlers section doesn't allow multiple paths for a handler, so you have to map each path like so..

<handlers>
    <add name="AssetHandlerGif" path="*.gif" verb="GET" type="CMS.AssetHandler" />
    <add name="AssetHandlerJpg" path="*.jpg" verb="GET" type="CMS.AssetHandler" />
    <add name="AssetHandlerPng" path="*.png" verb="GET" type="CMS.AssetHandler" />
    <add name="AssetHandlerCss" path="*.css" verb="GET" type="CMS.AssetHandler" />
<handlers>

This seems a bit odd since with the older httpHandler you were do this in one line

<httpHandlers> 
    <add verb="GET" path="*.jpg,*.gif,*.png,*.css" type="CMS.AssetHandler" validate="false"/>
</httpHandlers>

So can someone tell my why this does not work..am i doing something wrong here?

<handlers>
    <add name="AssetHandler" path="*.jpg,*.gif,*.png,*.css" verb="GET" type="CMS.AssetHandler" />
<handlers>

kick it on DotNetKicks.com


Justice for Barrister Rizwan Hussain

clock April 24, 2008 01:32 by author Naz
Bangladesh Emergency Floot Appeal
Bangladesh Emergency Flood Appeal - Baroness Uddin and Rizwan Hussain

Rizwan Hussain (pictured above) is well known figure in the Bangladeshi community for is humanitarian aid work and featuring in regular Bengali TV fundraising events, was recently was beaten and tortured at Zia International Airport in Dhaka, Bangladesh earlier this week while trying to help an elderly lady with ticket problems. 

The scary thing is this guy was in the flight when I was returning from my holiday from Bangladesh last week but I was unable to recognise him because he looked severely beaten and was on a wheelchair. I hope this never happens to anyone else an erg you to sign this petition currently with over 8,000 signatures so we can stop this happening again to someone else in the future.

QUOTE: 

"As most of you might have heard already, barrister Rizwan Hussain was beaten and tortured at Zia International Airport in Dhaka, Bangladesh earlier this week. He has returned to the UK on Thursday afternoon, albeit, sadly with a broken arm and leg.

His crime was to help an elderly lady with ticket problems and was brutally punished by 5 soliders for 1 hour until he signed a declaration claiming that he was illegally traficking humans and that he falsely claimed to be a Barrister. When he pleaded with them for water, they threw it over the floor and told him to drink it off there. This act of injustice cannot go by without repercussions!.."

UPDATE 27 May: This site is a site of web developers to learn and share knowledge and I wrote this article a few weeks ago with mixed emotions of anger and sadness over the horric events that took place in Zia airport against Rizwan Hussain. I'm sure many of you feel the same as I do and as of yet we have not come to a definete conclusion. Please help us support the "Justice for Barrister Rizwan Hussain and sign this petition which now has 13,000 signatures! and feel free to post a comment bellow.



dashCommerce 3.0 RC1

clock April 19, 2008 17:57 by author Naz
 

dashCommerce previously known as the e-Commerce Starter Kit is an ASP.NET Open Source e-Commerce Application which I actively contribute to fixing bugs and creating patches has just reached RC1 status.

Chris Cyvas the lead developer has a blog post here talking about the release.

kick it on DotNetKicks.com


Back from holiday

clock April 18, 2008 02:36 by author Naz

Hey everyone just got back from my holiday in Bangladesh had a great time there ended up extending my holiday.

I see someones hacked the site already (Beeshup?)

Anyways here's a pic of me and some students on thier first school trip ever!




Search

Sign in