i need some suggestion for working on 3 tier architecture, recently i completed one of my clients site, with 3 tier architecture, in that i was transforming the data directly in html from sql server 2008 and fetching the records in .net, .net was just doing the rendering task, so one of my friend said, it could reduce the performance drastically, but i never got any complaint from client regarding the same, as the records i am transforming in html from sql server 2008, is not more than max 10, and now for the second site, i want to take more precaution while rendering data on the site, now this time i have planned to again use 3 tier, but now instead of transforming data to html in sql server i am pulling the records from sql server 2008, to my datalogic layer, and in bussinesslogic layer, i am transforming it to HTML.. so can anyone suggest which is the best way to go about, the previous tranforming in HTML in SQL SERVER or in .NET
By Transforming in HTML i mean:
ALTER PROCEDURE [dbo].[SP_TVT_Article_Get]
@ID INT
AS
BEGIN
DECLARE @HTML NVARCHAR(MAX)
SET @HTML = '';
SELECT @HTML = '
<div style="margin-top:10px;" class="craig"><span style='+(SELECT CASE ISNULL(Author, '') WHEN '' THEN '"display:none; float:left"' ELSE '"display:block; float:left"' END)+'> ' + ISNULL(C.Author, '' )+ ' /</span> ' + DATENAME(DW, C.PublishDate) + ' ' + CONVERT(VARCHAR(15), CAST(C.PublishDate AS TIME), 100) +'</div>
<div class="sony_title">'+C.Title+'</div>' +
CASE
WHEN C.SecondaryTitle IS NOT NULL THEN
'<div class="tokiyo">' + C.SecondaryTitle + '</div>
'ELSE
'<div class="tokiyo">TOKYO: Six broadcasters & theaters in at least 22 will carry 3D coverage.</div>'
END +
'<div class="imgtxt_para01"> <img src="'+isnull(C.Thumbnail,'')+'" style="'+(SELECT CASE ISNULL(C.Thumbnail,'') WHEN '' THEN '"display:none"' ELSE '"display:block"' END)+'" />
'+ CAST(C.Article AS NVARCHAR(MAX)) +'</div>'
FROM CrossArticle_Article C
WHERE Id = @ID
SELECT @HTML
END
and this time which i am planning to do is like this..
My DataLogic Layer, will look like this:
public void GetArticle(int PortalID, int ArticleID, out DateTime PublishDate, out string Article, out string Author, out string Title)
{
object _getArticle = "";
Article = Author = Title = null;
PublishDate = DateTime.Now;
using (SqlCommand comGetArticle = new SqlCommand("sp_EQEM_GetArticle", ConnectDatabase))
{
comGetArticle.CommandType = CommandType.StoredProcedure;
comGetArticle.Parameters.AddRange(new SqlParameter[]{new SqlParameter("@PortalID", PortalID),
new SqlParameter("@ArticleID", ArticleID)});
ConnectDatabase.Open();
using (SqlDataReader reader = comGetArticle.ExecuteReader())
{
while (reader.Read())
{
Title = reader.GetValue(0).ToString();
Author = reader.GetValue(1).ToString();
PublishDate = Convert.ToDateTime(reader.GetValue(2));
Article = reader.GetValue(3).ToString();
}
}
ConnectDatabase.Close();
}
}
and bussinesslogic layer will look like this:
public object GetArticle()
{
string Title, Author, Article;
DateTime PublishDate;
connection.GetArticle(PortalID, ArticleID, out PublishDate, out Article, out Author, out Title);
StringBuilder str = new StringBuilder();
str.Append("<div class='aricle_para'>");
str.Append("<h1>" + Title + "</h1>");
str.Append("<span style='float:left; display:" + (String.IsNullOrEmpty(Author) ? "none" : "block") + "'>By " + Author + " | </span><span> " + PublishDate.DayOfWeek.ToString().Substring(0, 3) + ", " + PublishDate.ToString("dd MMM yyyy") + "</span><br />");
str.Append(HttpContext.Current.Server.HtmlDecode(Article));
str.Append("</div>");
return str.ToString();
}
and then i will be calling the bussinesslayer function GetArticle to the page, where i want to render article…
A “tier” in an n-tier architecture is so called because it is functionally distinct from other parts of the application; in fact, it’s more correct to say it’s isolated from even the question of whether it’s used in one or n applications.
“Layers” commonly refer to distinct groups of functionality within one tier.
“3-tier” applications came about because the pattern of using Data Access, Business Logic and Presentation in three distinct tiers is a quick fit for many scenarios, but it’s by no means prescriptive that you need 3 tiers. The point is primarily that you have tiers at all.
In order to qualify as a tier, broadly speaking its responsibilities must not bleed in to another. By having a stored procedure in your database that returns HTML, you are bleeding presentation markup up into your business logic tier (of your chosen 3-tier approach) and then further outwards into your presentation logic.
You will find everything far simpler to test, and make performant, by returning only data from your stored procedures. Your business logic can process this data, choosing what (if any) filters, modifications and workflows to apply, and add metadata to that data before passing it outwards to your presentation tier that is solely responsible for placing that data inside of your HTML markup. In this way, your presentation tier is kept distinct, and you may then add layers to it that can generate markup for mobile applications, or JSON for RESTful services responses, etc. – all without the stored procedures caring.
Your friend’s comments about performance optimisations were the tip of the iceberg, but may have referred to a natural consequence of keeping this separation: you would not be concatenating strings in your stored procedure to generate markup that is then further modified in your business logic.