All posts in ASP.NET

A caching solution is always necessary if you want to build scalable applications which will handle lots of users and heavy data access without requiring more hardware resources. I’ve already talked about caching when using Entity Framework, but is there a way to use caching in Linq to SQL and what are the problems one might face.

Capture To explore caching in Linq to SQL I’ve built a small web site that had a single page. This page used Linq To SQL to access the AdventureWorks sample Database which you can download from Codeplex.

The very simple Linq To SQL model that I’ve created had three entities, a Peson and its Phones and Email addresses.

 

The site default page contained a repeater that was databound to the first Person that his last name begun with the letter M, fetched by a call to the GetPerson method.

public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      DataBind();
    }
  }

  public override void DataBind()
  {
    base.DataBind();

    employeesRepeater.DataSource = new List<Person>() { GetPerson("M") };
    employeesRepeater.DataBind();

  }

  protected Person GetPerson(string lastNameStartsWith)
  {
    using (AdventureworksDataContext context = new AdventureworksDataContext())
    {
      return context.Persons.Where(p => p.LastName.StartsWith(lastNameStartsWith)).FirstOrDefault();
    }
  }
}

This works, but imagine tens of thousand of users accessing this page at the same time asking for the same thing. This would result in thousand of SQL commands reaching SQL server. And in this case the SQL command is trivial but what would happen if the query was more complex? The answer is that this page would become significantly slower as more users would try to access it.

To overcome this, I’ve introduced a simple Caching mechanism to the page utilizing the System.Web.Caching.Cache Object of the Page and changed the GetPerson method to use this caching mechanism.

protected Person GetPerson(string lastNameStartsWith)
{
  using (AdventureworksDataContext context = new AdventureworksDataContext())
  {
    return CacheGet<Person>(
      () => context.Persons.Where(p => p.LastName.StartsWith(lastNameStartsWith)).FirstOrDefault(),
      string.Format("Person_LastName_StartsWith:{0}", lastNameStartsWith));
  }
}

protected T CacheGet<T>(Func<T> loader, string cacheKey) where T : class
{
  var cachedObject = this.Cache[cacheKey] as T;
  if (cachedObject == null)
  {
    cachedObject = loader.Invoke();
  }
  return cachedObject;
}

	

 

Now only the first call to the GetPerson method results into a query being made to the SQL Server, all resulting calls will be served by the Cache (that is of course if the cache key aka last name character matches) and its going to be equally fast for either one user or million of users. So what’s the problem? Anyone care to guess?

To be continued…


So it’s time for me to reveal what I’ve been up to for the past 6 months. As some of you may already know, I moved to a new start up company about six months ago, called Metadata (I know the site isn’t quite ready yet, but we focused on the product instead of the site), where I was assigned the task of designing and building a solution for Content Management. The company business strategy required a product that would satisfy the needs of every possible client ranging from small companies that need simple websites to large news portal organizations with real publishing needs and companies with e-commerce needs.

So let me talk a lit bit about what we’ve build so far and begin by letting you in on the 5 pillars we used to design the system:

  • Ease of use. Build a system that would increase its users productivity by reducing the time needed to operate it.
  • Configurable. Build a system that could be easily configured to feet every customer’s specific needs.
  • Modular. Build a system with the core CMS functionality and extend it by using various modules.
  • Secure. Build a secure system.
  • Performance. Build a system that is fast.

We’ve decided to follow the content – presentation separation principle. That way the content editor doesn’t have to worry about how its content is going to look on the site (at least in most of the cases), the content can be easily reused, the whole site can change by simply changing the appearance (themes, templates) and the content can be used in other types of applications (PDA’s, WPF, Silverlight… 😉 and so on) as well and not just websites.

MetaCMS and MetaCMS built websites use the latest Microsoft technologies. We’ve used LinqToSql as our ORM, .Net Framework 3.5 sp1 for its MVC Routing Feature, ASP.NET Ajax extensions, Silverlight, and Microsoft SQL 2008.

ArchitectureA MetaCMS website doesn’t have any physical pages (aka aspx files) instead a Runtime engine is responsible to built page instances using available templates and controls and apply themes. That way the whole page structure and appearance can change at runtime. Controls. Using a Parameter system that we’ve built each control is associated with specific content and is responsible to present it to the page instance that it’s placed. But other mini applications – modules can also be placed on an instance of a page.

Except from the runtime presentation engine, MetaCMS offers a number of services as well. So in it we’ve built an advance caching mechanism capable of automatic Cache Item invalidation Synchronization between web farm nodes, Logging service that can be configured with different types of logging sinks, Auditing, Messaging service that allows communication with a user using various platforms (email, Newsletters, sms etc.) and many more.

MetaCMS

Also during the past five months we’ve developed a number of modules that add more functionality to the MetaCMS core some of which are:

  • Polls
  • Comments/Opinions
  • Publications
  • StockMarket
  • Events
  • EShop
  • Business Directory

This Friday, the first site, built using our platform went live. www.progreece.gr is an e-commerce solution built in co-operation with www.in.gr and is dedicated on selling organic food but that’s not the only one we’ve been working on… you’ll just have to wait a bit longer to find out more…


There are many occasions (at least so I’ve discovered) were you need to re-use user controls that you’ve created. A solution in those cases would be to convert your user control to a custom control, compile it into an assembly which is then going to be shared to all projects needed. The problem is, that you can’t easily convert a user control to a custom control cause you have to write all that render html methods to render exactly as the user control looks like.

PublishWebSite An alternative that I find pretty handy in cases like that, is moving the user controls that I want to be able to reuse in a separate website. By doing that I’m able to build and test my controls separately from any project that is going to use them. When I want to use the shared controls on a website I use the publish website option from the context menu and choose the use fixed naming and single page assemblies option from the dialog that popups.

FixedNaming

This way I get a single dll for each web user control in the controls web site, with a fixed name. Next I register those dlls (copy them on the bin folder) on the consuming site. I then place a placeholder where I want my control to be loaded and from the code behind I load the control and add it to my placeholder’s controls collection. The code looks something like that

   1: try
   2: {
   3:     UserControl dynamicControl =
   4:         Activator.CreateInstance("App_Web_mycustomcontrol.ascx.cdcab7d2", 
   5:         "ASP.mycustomcontrol_ascx").Unwrap() as UserControl;
   6:     controlPlaceHolder.Controls.Add(dynamicControl);
   7:  
   8: }
   9: catch (Exception ex)
  10: {
  11:     Debug.WriteLine(ex.Message);
  12: }

As you can see the compiler generates a class that inherits from your code behind *.cs file and that contains all the code to render the html contained in the control. That’s the class you need to instantiate. By default the class name is the control’s name with all chars lower case with the extension included. If you’re not sure about the class name or get a runtime exception saying that can’t create the type specified just use reflector on your generated assembly to verify that you’ve entered the names correctly, you’ll find the right class under the ASP namespace.

I’m attaching a sample solution to check how this works and play around with it. Hope this will help you solve some of the problems you might face …


Most of the people I meet have this idea that they have to either buy or find an open source third party component, or (even worse) use flash, when it comes to drawing graphs for a web application or site. So I’m guessing that most people don’t know that the Data Visualization Group within the SQL Server Reporting Services Team has created stand-alone ASP.NET and Windows Forms Chart Controls for the .NET Framework 3.5 and Visual Studio 2008, similar to what has been already available as built-in chart functionality in SQL Server Reporting Services 2008.

Free ASP.NET Chart Controls

There are several links to get the “full package” for these free chart controls:

  • First, you can download the ASP.NET and Windows Forms Chart Controls for .NET Framework 3.5 SP1 here.
  • Then download the Language Pack for the Free Microsoft Chart Controls here.
  • Download the Microsoft Chart Controls Add-on for Microsoft Visual Studio 2008 here. This package provides Visual Studio toolbox integration and IntelliSense for the ASP.NET and Windows Forms Chart controls.
  • Last but not least, download the documentation and API for the free ASP.NET and Winform Chart Controls here.

You can also check-out some samples for the Microsoft Chart Controls as well as discuss them in the forums.


Protected: Building the Web.Next Now

Categories: ASP.NET, Great Sites, MS Application Blocks, Silverlight, Web
Enter your password to view comments.

This content is password protected. To view it please enter your password below:


2013-05-17 23:13:48 Read more

Protected: .Net Framework 3.5 breaks ASP.NET 2.0 web sites!

Categories: ASP.NET, Visual Studio, Web
Enter your password to view comments.

This content is password protected. To view it please enter your password below:


Protected: Building Facebook applications

Categories: ASP.NET, C#, Great Sites, Popfly, Silverlight, Web
Enter your password to view comments.

This content is password protected. To view it please enter your password below:


I rarely have the time to read a 10 page article or watch a full length Webcast, I usually prefer nuggets when I want to discover new technologies and explore hot developer topics since they only last 10-15 minutes.

So if you’re into Silverlight here’s a list of nuggets so that you can catch up:

Silverlight Streaming : Silverlight Streaming is a hosted service at http://streaming.live.com/ which provides developers and content creators the ability to host Silverlight content on …

Silverlight – Linking Silverlight and ASP.NET Projects : With Silverlight Tools for Visual Studio 2008 it is possible to develop Silverlight applications as a separate project and embed that project within an ASP.NET site. This gives clean separation between …

Silverlight – Debugging between Mac and Windows : An unusual nugget in that it was recorded on a MacBook Pro. It demonstrates the concept of debugging a Silverlight application running on Safari from a Visual Studio (on Windows) developer machine. This …

Silverlight – Using Ink : A brief introduction to using an InkPresenter element to receive ink based input from users. First, we introduce the InkPresenter element in XAML and follow-up with the code (C#) to capture and process …

Silverlight – Accessing HTML from managed code : Silverlight enables developers to access the host HTML from managed code. This is an extremely powerful feature which can be used to create seamless experiences between the HTML and Silverlight UI. …

Silverlight 1.1 Programming – Full Screen : In this nugget we show you how to make a Silverlight application break outside the ‘chrome’ of the browser and take up the complete screen. This is very useful for applications which require more desktop …

MSDN Roadshow 2007 Silverlight Demo : This nugget is a screen cast of the Silverlight demo delivered during the rich client session on the MSDN Roadshow 2007.


As always this time of year I begun thinking on all the things I want to read, listen, view and code during my summer vacation free time, even though I never get the chance to do all the things I plan to ;-).

So I’ve already bought my PSP extra memory (2GB) and plan to fill it with podcasts, webcasts and videos so I can watch on my free time. Technologies and products that I’m especially interested this summer include : Silverlight, WPF, WCF, Visual Studio 2008, Astoria, Jasper, Linq, .Net 3.5, C# 3.0 etc.

I’ve already begun searching for those but I would be grateful if you could give me a hand by proposing stuff you think I should definitely have a look at.