3.5 Social Networking

Andrew Siemer

Mentioned 7

Each chapter starts by showing the user experience we are going to create in the chapter. It then has a section discussing the major design decisions we face in building these features, and finally presents the 'solution' including the overall architecture and highlighting the most important code elements. The book presents a lot of real-world code, with comprehensive explanation and discussion. The full social network application is available for download from the Packt website, for you to examine, study, and modify. This book is written for ASP.NET and C# developers who want to build an enterprise-grade Social Network, either for their own business purposes or as a contract job for another company. The book assumes you have prior experience of developing web applications using ASP.NET 3.5, C# 3.0, SQL Server 2005/2008, and Visual Studio .NET 2008; it focuses on topics that will be of interest to existing developers not on providing step-by-step examples for each detail.

More on

Mentioned in questions and answers.

What are the best practices in using Lucene.Net? or where can I find a good usage sample?

We frequently use Lucene.NET when the data is huge and needs to have super fast response times for reading. We generally stick the data in that we need to search as well as the key to allow us to map our results back to the database table that has the remaining details. This then allows us to search for a user (in our case) checking for their past participation. This is not just a username search but a search that iterates over various details trying to find if there are any other instances of that user (albeit in a different form). An example of this, we look for the users ID (from one system), their ID from another system, perhaps an ID from a suppliers system, a flash cookie GUID, a sites cookie GUID, etc. And as we find one identifier we look for other instances of that identifier for other instances of users. This allow us to dedup the users entry into one of many systems (as their participation in any system is only allowed once per 24 hours). In SQL this alogrithm (which I was vague about) would take forever! In Lucene.NET it takes less than a second. Lucene has many more search possibilities than SQL Server does. The thing that it sucks at is writing to or updating your index. This is usually done as a job...all at once. However, if you need to write to the index updating it in real time you need to write some clever code to insure that it is written to in a locked fashion (think queueing with singleton) or your code will overlap and explode!

I cover the usage of Lucene.NET in my book (ASP.NET Social Networking) and you can find lots of help here.

For a .NET component that will be used in both web applications and rich client applications, there seem to be two obvious options for caching: System.Web.Caching or the Ent. Lib. Caching Block.

  • What do you use?
  • Why?


Is this safe to use outside of web apps? I've seen mixed information, but I think the answer is maybe-kind-of-not-really.

I don't expect to use one of its highlights, SqlCacheDependency, but the addition of CacheItemUpdateCallback in .NET 3.5 seems like a Really Good Thing.

Enterprise Library Caching Application Block

  • other blocks are already in use so the dependency already exists
  • cache persistence isn't necessary; regenerating the cache on restart is OK

Some cache items should always be available, but be refreshed periodically. For these items, getting a callback after an item has been removed is not very convenient. It looks like a client will have to just sleep and poll until the cache item is repopulated.

Memcached for Win32 + .NET client

What are the pros and cons when you don't need a distributed cache?

These are the items that I consider for the topic of Caching:

MemCached Win32 Velocity .net Cache Enterprise Library Caching Application Block

MemCached Win32: Up until recently I have used MemCached Win32. This is a akin to a web farm (many servers serving the same content for high availability) but it is a cache farm. This means that you can install it locally on your web server initially if you don't have the resources to go bigger. Then as you go down the road you can scale horizontally (more servers) or vertically (more hardware). This is a product that was ported from the original MemCached to work on Windows. This product has been used extensively in very high traffic sites.

Velocity: This is Microsofts answer to products such as MemCached. MemCached has been out for quite some time, Velocity is in CTP mode. I must say that from what I have read so far this product will certainly turn my head once it is out. But I can't bring myself to run big production projects on a CTP product with zero track record. I have started playing with it though as once it gains momentum MemCached won't even compare for those locked in the windows world!

.NET Cache: There is no reason to discount the standard .NET Cache. It is built in and ready to use for free and with no (major) set up required. It offers flexibility by way of offering mechanisms for storing items in local memory, a SINGLE state server, or a centralized database. Where Velocity steps in is when you need more than a single state server (cache in memory) and don't want to use a slow database for holding your cache.

Enterprise Application Block: I stay away from all of the Enterprise Application Blocks. They are heavy frameworks that give more than I generally require! As long as you remember to wrap everything that touches code that is not your own and follow simple rules for coding, stick to any of the other methods over this one! (just my opinion of course - MySpace leverages as much as they can out of Enterprise Application Blocks!)

You don't have to choose up front! I generally create a cache wrapper that I communicate with in my code for methods such as Get, Set, Exists, Remove, ListKeys, etc. This then points to an underlying level of cache abstraction that can point to MemCached, Velocity, or .NET cache. I use StructureMap (or choose another IoC container) to inject which form of cache I want to use for a given environment. In my local dev box I might use .NET cache in the session. In production I generally use MemCached Win 32. But regardless of how it is set up you can easily swap things around to try each system out to see what works best for you. You just need to make sure that you application knows as little as possible about how things are cached! Once this layer of abstraction is in place you can then do things such as run a compression algorithm (gzip) for all the data that is going in and out of cache which would allow you to store 10 times the amount of data in cache. - transparently.

I cover .NET Cache, MemCached Win32, StructureMap, and the appropriate abstractions in my book if you are interested!

ASP.NET 3.5 Social Networking ( ) Andrew Siemer

Update Changed the link that lists sites using memcached. Thank you David for noticing that it was broken!

In an application similar to StackOverflow that I am building, I am trying to decide what relationship my Questions, Answers and Comments tables should have.

I could have Questions and Answers both be represented by a single table Posts.

That would allow Comments to have a single foreign key to Posts.

But if Questions and Answers are separate tables, what relationships should Comments have to each of these?

UPDATE: Although the chosen answer recommends a Class Table Inheritance approach and this seems like the best approach in database terms, this option is not supported by the Rails ORM. So, in Rails my models will have to use Single Table Inheritance and will probably look like this:

class Post < ActiveRecord::Base  

class Question < Post  
  has_many :answers, :foreign_key => :parent_id  
  has_many :comments, :foreign_key => :parent_id  

class Answer < Post  
  belongs_to :question, :foreign_key => :parent_id  
  has_many :comments, :foreign_key => :parent_id  

class Comment < Post  
  belongs_to :question, :foreign_key => :parent_id  
  belongs_to :answer, :foreign_key => :parent_id  

class CreatePosts < ActiveRecord::Migration  
    def self.up  
      create_table :posts do |t|  
        t.string :type 
        t.string :author   
        t.text :content  
        t.integer :parent_id   

    def self.down  
      drop_table :posts  
CREATE TABLE "posts" (
  "type" varchar(255),  
  "author" varchar(255),  
  "content" text,  
  "parent_id" integer,  
  "created_at" datetime, 
  "updated_at" datetime

In the social networks that I build I do something a bit different. If you think about it a comment could be attached to just about any Entity in a site. This could be a blog post, a forum thread or post, an article, someones picture, a persons profile, a vendor of a service, etc. For this reason I create a SystemObjects table which holds the object type (table reference). For the most part I create records for the Entities of my system that will accept comments...but these map directly to my tables. The SystemObjects table houses the SystemObjectID, and a friendly name for future reference (a lookup table).

With this in place I then create a Comments table which has the SystemObjectID reference to tell me what table to go look in. Then I also house the SystemObjectRecordID which tells me which PK of the referenced table I am interested in (along with all the standard comment data).

I use this notion of the SystemObject table for many other generic far reaching concepts in my sites. Think about Tags, Ratings, Comments, and any other dangling fruit that might be attached across your site and aggregated up for quick use.

Read more about this in my book ASP.NET 3.5 Social Networking.

I am developing a social networking website using 3.5 and sql server 2008. I need reference links, sites or reference books which help on knowing what are the best practices, code patterns and best third party tools to integrate and work on. Any suggestions from you people will help me greatly. T Thanks

I found this book a great read and should help:

Searching here I found that this question was already asked, but I think my situation is worse!-(

I'm a total novice with Internet repositories development. A book I'm reading contains an interesting sample project - a social network in ASP.Net. The author put the code as a CodePlex project. I tried to "get" the version through VisualSVN (subversion) but failed. So what I did was download the code as a zip file (i.e. disconnected from the project's repository), created my own local repository, and started modifying the code.

Now the CodePlex's respository started becoming active. What I'd like to do is to continously get the changes from the CodePlex repository to my local one, and occasionally commit some of my changes back to the CodePlex repository.

Is it doable?

What I actually did, at least for now and as long as the traffic for the CodePlex project is low:

I downloaded the current CodePlex 22875 release (as a zip). Luckily, before I started playing with the project, I'd saved the original 16202 zip file locally on my computer (but I think I could also have gotten it from CodePlex).

Now I have two folders on my computer: 22875 and 16202. What I did was use Total Commander to compare the two trees and find out which files were modified. And I manually integrated the few changes into my code.

I think Beyond Compare would have been even better with the integration, but I'm not yet familiar with it so I stuck with TC.

To go the other direction I think I'd have to check out the current CodePlex trunk, integrate my changes manually, and commit the changes.

What can I say - it sucks. But my volume of changes is such that a local repository makes much more sense than working with the CodePlex one. And as CodePlex uses SVN, I enjoy working locally with SVN, I've just shelled $49 for VisualSVN, and I'm afraid of the git Unix hacker :-)

I am looking for a blog/forum type webpage for ASP.NET. I am using the MVC project so I need a page that I can use the content of my Master page and my Site.css style sheet. Maybe a control or application extension. Also a link to a page where there are more of these controls would be nice.

Admittedly this is an unsolicited recommendation, but for social networking in I found this book useful - ASP.NET 3.5 Social Networking

As a fun project I wanted to build a social networking site for internal use inside our company. Most of the public networks are blocked by the proxy so i can't use the public api of facebook or other networking sites. I would like to hear from people who have built such sites before.

Thanks Bharani

I just got done writing my book ASP.NET 3.5 Social Networking in which I detail step by step how to go about building (technically) a social networking site. In it I cover the ideas of stickiness and community, contrast some of the popular sites, go over some lesser known sites, and then dig into the details of each feature that most of the common community sites have (friends, messaging, tagging, blogging, articles, profiles, accounts, security, navigation, etc.). Take a look at the book on amazon!