I mentioned earlier today that there still are a couple of articles on implementing N-Tier approach in ASP.NET which could be good (as opposed to junk published on 15seconds.com.) So, at least a first part of Imar Spaanjaars' article is very good. He is using very readable style. His approach features well structured business objects, and custom DAL objects. It certainly could be useful!
Friday, February 29, 2008
Thursday, February 28, 2008
N-Tier with ASP.NET
1. Abstract:
For a last couple of weeks I've been looking for a good example of implementing N-tier approach for ASP.NET application. Our company is switching its Intranet from plain old ASP to a modern ASP.NET (3.5) application. It's been proposed to follow a modular N-tier approach.
2. Big Ball of Mud:
I was googling for N-Tier ASP.NET application. All I found was unbelievable bad. It looks like that all those authors from http://www.15seconds.com just heard buzzwords like N-Tier, O/RM, loose coupling, but never understood them. Instead they went ahead and implemented their little poor-designed, overcomplicated examples where all the layers are tied together in a Big Ball of Mud (BBM) where each layer directly uses ADO.NET and heavily depends on datasets with millions of useless methods; - and they immediately published their articles. BBM examples:
Designing N-Tiered Data Access Layer Using Datasets
N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005
3. I still hope:
I'm still looking for a good example of N-Tier approach with ASP.NET and ADO.NET. I still hope that one of the following articles might be good:
Building Layered Web Applications with Microsoft ASP.NET 2.0
Implementing a Generic Data Access Layer in ADO.NET
Architecting LINQ To SQL Applications
4. Foundations of Programming (N-Tier, DDD, DI, NHibernate):
I was happy to find Foundations of Programming series of articles written by Karl Seguin at the very beginning of my exercises. Despite its theoretical name, this series shows fairly clear and practical approach of designing modular C# [Web] application with separated business layer (Problem Domain), Data access layer (DAL), and presentation layer.
There are simple C# examples. You implement business (domain) objects like a Car, a Model, an Upgrade; type-safe collections of those objects are implemented as generic lists - List<Car>, List<Model>, List<Upgrade>.
Domain objects are almost persistent-agnostic: they do not know in details how to save themselves to a database or retrieve themselves from a database. All they really should know is how to create an instance of class which implements IDataAccess interface and then to call one of its GetCar(), GetAllModels(), SaveCar(), etc. overloaded methods.
Domain objects are completely presentation-agnostic. That's a task of presentation layer to databind to business objects and collections. As Karl said, "You'll also be happy to know that ASP.NET and WinForms deal with domain-centric code just as well as with data-centric classes. You can databind to any .NET collection, use sessions and caches like you normally do, and anything else you're used to doing."
Then we can create or own DAL methods or we can use one of O/RM tools like NHibernate.
Everybody agrees that NHibernate (Hibernate for .NET) is very good. It's one of the most known and widely used O/RM tools. It's free and well-documented. For me - it's a way to go.
We also can use Dependency Injection tools like StructureMap to test pieces of code.
5. Our possible design outlines:
So, a picture is pretty clear. We need
1) To implement set of classes which represent our business logic. There would be
public class Employee {}
public class Project {}
public class WorkActivityReport {}
public class Contract : WorkActivityReport {}
public class CorporateTask : WorkActivityReport {}
etc.
2) To implement collection classes which also represent business logic. I would encapsulate collections into static container/factory classes. There would be
public static class ContractList
{
// private static List<Contract> m_contracts;
public static List<Contract> getContractList(int projectId, int sortOrder) {}
}
It makes perfect sense to delegate part of business classes' functionality to "Factory" classes. So, it could be a
public static class ContractManger
{
public bool saveContract(Contract thisContract) {}
public List<Contract> getContractList(ContractType thisContractType) {}
}
With Factory classes we might not need custom collection classes, but just use generic collections instead. All calls to DAL layer methods will be performed by Factory classes' methods. It makes sense. I even used this approach in my little poor VBScript classes (ASP). Imar Spaanjaars recommends it too.
3) To implement DAL. (I vote for O/RM or, at least, custom DAL classes.)
4) To implement ASP.NET presentation layer using standard technique - master pages, code-behind, sessions, cache, etc.
I've been through implementing business logic before. In our current classic ASP application I implemented Roles Rights Management System (RRM) using VBScript classes. While VBScript is a bad tool, it still was a real pleasure to program with classes, to make a set of methods representing Roles/Rights business logic and to bind system to a normalized MS Access database. It's surprisingly easy to do, if you understand that OOP business layer comes first. It should not be database-driven design. It should be Domain Driven Design. You just create classes, properties, and methods for every entity your business needs. Everything else comes after it.
Wednesday, February 13, 2008
A Conversation with Anders Hejlsberg
In this eight-part interview, Anders Hejlsberg, the lead designer of the C# programming language, discusses many design choices of the C# language and the .NET framework.
Foundations of Programming, by Karl Seguin
Foundations of Programming - an excellent series of articles about better programming algorithms, practices, and tools from .NET programmer's perspective. Written by Karl Seguin, a Microsoft ASP.NET MVP.
Foundations of Programming - ZIP/PDF
Part 1 - Introduction
Part 2 - Domain Domain Domain
Part 3 - Persistence
Part 4 - Dependency Injection
Part 5 – Unit Testing
Part 6 - NHibernate
Part 7 - ActiveRecord
Very interesting and helpful information about Object-Relational Mapping (ORM) concept and existing tools; about Dependency Injection (DI) concept and existing tools. This stuff is really important, because it shows what is a good way to separate application layers (UI, Business == Domain, Data Access). What Thiru Thangarathinam showed in his "N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005" article seems to be far from optimal, because in his example there is a tight coupling between layers. One layer explicitly created an object of another layer, explicitly calls methods of another layers, everything is tied to TableAdapters, etc. (It looks like there are a lot of programmers who are trying to write articles just after they learned a new technique, without real understanding of design issues.)
Karl shows, among other things, how to use Interfaces to decouple layers and particular database providers.
There are different aspects described in that article. Unit Testing is an interesting technology, but I don't think we will use it right away. NHibernate is a powerful Object-Relation Mapper (ORM) which sits between database and business layer (Domain layer) and maps relational data to your objects in a standard and flexible way, allowing to decouple database from domain. MS is releasing Entity Framework (EF) as a part of Astoria. It's a MS way of doing ORM. So, we might look at it as well.
But there is one part in that article talking about Dependency Injection and using StructureMap tool. It seems to be most relevant for my development. It basically allows to use stubs (mocks) for pieces of code which are not yet developed. So, when you need to work on domain layer (business classes), but don't have data-source layer (ORM) yet, you can substitute calls to data-source layer by calls to some mock classes. To do it you only need to change one line in StructureMap's XML configuration file.
It also makes it easier to switch from one implementation of data-source layer to another if necessary, without changing a single line of code in domain (business) layer!
The Code Wiki by Karl Seguin - a website designed to help coders become better developers. At the core of this websites sits The Code Wiki application, built, from the ground up, as a learning tool.
The Code Wiki book (PDF) - this book is about introducing and exploring alternatives to how you design, write and test your code on a day to day basis.
Tuesday, February 12, 2008
Should Microsoft Buy Yahoo? - by Bruce Eckel
An interesting article. Sounds reasonable to me...
(Bruce Eckel is one of the best known C++/Java programmers and wrote excellent programming books.)
Tuesday, January 08, 2008
Ode To Code
I'd like to share with you this link. Outstanding programming resource, clean easy-to-read style. Bravo Scott! I already ordered your Programming Windows Workflow Foundation book.
Friday, January 04, 2008
Why do I hate MS SharePoint?
To me, what is really bad in SharePoint - in general - is that it mixes together data, business logic, and presentation. Everything is in the list. The good standard multi-tier design (database layer – business layer – presentation layer) was developed and used over many years. Data is supposed to be stored in database, not in a list. But SharePoint does not easily allow plugging an external database. You are not supposed to work with its underlying cryptic SQL database either. SharePoint does not want to be a presentation layer. It wants to be an all-in-one machine. I never saw good all-in-one machines. They consume an enormous amount of resources but are never flexible enough to suite concrete needs.
Personally, I'm not very happy with ASP.NET View State and ASP controls which try to blur a boundary between server-side scripts and client-side controls either. I'm not entirely happy with applying well-working Windows desktop event model to very different world of Web applications either. Are you a big fan of Big Ball of Mud?
I always thought that it is generally healthier to separate client logic and server logic. Ajax (which I like and use for a long time) does not try by itself to blur that distinction between server and client.
Apparently, it’s not only my concern: some people at Microsoft think in that direction as well. Otherwise, why they're now introducing in ASP.NET a long-known and proven MVC design pattern?
But, besides these philosophical design considerations, our team stumbled against major problems in our attempts to switch company's intranet from old classic ASP to SharePoint. After months of learning, after participating in SharePoint Connections 2007 conference we finally – and to my big relief – decided to develop in ASP.NET 3.5 instead. We are pretty sure that MOSS security model (based on groups and similar to Windows itself) is not flexible enough and does not allow us to implement our company's business rules in a natural way.
I don't agree with an author of article mentioned below when he says that our company business rules are unique. I see them as quite normal and natural. It's enough to say, that they lie perfectly to a good relational database design for Roles and Rights we use right now and going to continue to use with ASP.NET version of our intranet.
Here's why we decided that SharePoint is not our tool
Somewhat related articles:
Why SharePoint Portal Server is Terrible
Things you should know about SharePoint ahead of time
SharePoint 2007: Pointedly unskinnable
The Long Path to Content Deployment
Missing MOSS 2007 Functionality
Wednesday, December 19, 2007
Anders Hejlsberg, Herb Sutter, Erik Meijer, Brian Beckman: Software Composability and the Future of Languages
For sleepy programmers to refresh their minds: An interesting discussion about future of programming languages on the outstanding Channel 9 site.
Thursday, December 13, 2007
ASP.NET 3.5 Extensions -> ASP.NET MVC
I always thought and continue to think that ASP.NET model which merges together browser-based UI and server-based business logic in one class, trying to present distributed web page / Web server application as if it would be a desktop application is completely wrong. Traditional ASP / PHP, etc. are much better in this regard.
Also, for a long time I think that event-based model is very hard to manage. I remember a big struggle against MS Access front-end in a big multi-user Access / SQL Server protective gear distribution system we developed in Centech Group, Inc. I worked as a contractor there and developed major part of that application. As usual, it wasn't programmer's decision of what tools to use. Personally, I would use VB 6 as a front-end, not MS Access.
In VB applications I never used bound controls with their hard-to-predict events. It was much easier to manually write code to connect to a database, get data, and then save it back. Events are dangerous: you are never sure what fires first, what triggers another event when you respond to a first one, etc. But, as I said, it was quite easy to turn them off in VB front-end and to use similar approach in a classic ASP. In MS Access, however, you cannot turn off event handlers. A significant portion of development time was spent on workarounds eliminating harmful chains of events.
And now they are back... ASP.NET, SharePoint... old problems.
Apparently, people at Microsoft understand it as well. Otherwise, why they're now introducing in ASP.NET a long-known and proven MVC design pattern? Sure, they would present it as a big Microsoft's achievement, despite the fact other parties use it for a while...
Thursday, December 06, 2007
Upper Volta?
Noun 1. Upper Volta - a desperately poor landlocked country in western Africa; was formerly Upper Volta under French rule but gained independence in 1960
TheFreeDictionary.com
West German chancellor Helmut Schmidt liked to describe the Soviet Union of the 1980s as "Upper Volta with missiles", according to David Halberstam in an article published in Vanity Fair magazine dated August 2007. The phrase "Upper Volta with rockets" was also used to describe the Soviet Union (in quotes, but with no attribution) in a survey on the Soviet economy in The Economist on April 9, 1988. According to Perry Anderson, London Review of Books, Vol 29, No.2, 25 January 2007, the phrase was popular among foreign diplomats in the 1970s. He suggests the modern Russia might be "Saudi Arabia with Rockets".
Wikipedia
December 5th, 2007
Microsoft is making available a first, publicly available test build of a developer toolset that allows programmers to write Web applications using existing .Net-based tools and languages.
Microsoft delivers a test build of its 'Volta' cloud-programming toolsetThe toolset — code-named "Volta" (and previously code-named "Tesla") — is the brainchild of Erik Meijer, a SQL Server architect whose new title is principal architect of Volta. Meijer has been talking up for the past couple of years the concept of "democratizing the cloud" via the programing of multi-tier, distributed applications...
You architect and build your application as a .NET client application, assigning the portions of the application that run on the server tier and client tier late in the development process. You can target either web browsers or the CLR as clients and Volta handles the complexities of tier-splitting. The compiler creates cross-browser JavaScript for the client tier, web services for the server tier, and all communication, serialization, synchronization, security, and other boilerplate code to tie the tiers together. In effect, Volta offers a best-effort experience in multiple environments without requiring tailoring of the application.
Thursday, November 15, 2007
Anders Hejlsberg - a main source of our troubles
Anders Hejlsberg - that's a name every programmer should know. He created Turbo Pascal, Delphi - which both were far ahead of anything Microsoft could produce. Lately, MS managed to buy this guy. It was he, who invented J# - without any success, but then created C#. Now he presents a new technology called LINQ which will be included into VS Studio 2008 (scheduled to be released this November). http://blogs.msdn.com/charlie/archive/2007/01/26/anders-hejlsberg-on-linq-and-functional-programming.aspx
Monday, November 12, 2007
SharePoint 2007
Hi everybody! I've just came back from a SharePoint Connections 2007 conference in Las Vegas. Alsthough I still don't like that, I decided to share with you this info. If you are interested in SharePoint development, this guy's blog should be of most interest for you: Todd Baginski.
http://www.sharepointblogs.com/members/Vladimir-Kelman.aspx
Tuesday, October 30, 2007
What means OLED?
OLED means Organic light emitting diode and is emerging as a leading next-generation technology for electronic displays and lighting. OLED can be used as display technology or as light source.
Thursday, October 26, 2006
Incorrect behavior of window.onblur event under IE and a workaround
<html>
<header>
<script>
/*
In Internet Explorer window.onblur event is implemented incorrectly (as opposed to Firefox/Mozilla browsers). It is wrongly fired when focus is switched between HTML elements *inside* a window.
Suppose you're trying to automatically close a popup window when focus is switched to another (main) window (a common task). As a result of the described bug, clicking on any element *inside* a popup window or trying to select something there will also close a popup.
http://codingforums.com/showthread.php?p=500127
Below is a workaround.
*/
var active_element;
var bIsMSIE;
function initiateSelfClosing() {
if (navigator.appName == "Microsoft Internet Explorer") {
active_element = document.activeElement;
document.onfocusout = closeWnd;
bIsMSIE = true;
}
else { window.onblur = closeWnd; }
}
function closeWnd() {
if (window.opener != null) {
if (bIsMSIE && (active_element != document.activeElement)) {
active_element = document.activeElement;
}
else {
window.close();
}
}
}
</script>
</header>
<body onload="initiateSelfClosing()">
Your stuff
</body>
</html>
Friday, September 29, 2006
jQuery and an example: Dynamic Stylesheet Switcher
I've just found a fantastic JavaScript library called jQuery.
jQuery is a Javascript library that takes this motto to heart: Writing Javascript code should be fun.
Here's a cool example of using it: Dynamic Stylesheet Switcher.
Levels of JavaScript Knowledge
Dean Edwards rules! Look at the comment #77, though :)
I understand level 5, but I don't know yet why
var button = document.getElementById("hello");
button.addEventListener("click", function(event) {
hello(WORLD);
}, false);
is better than
var button = document.getElementById("hello");
button.onclick = function() {
hello(WORLD);
};
Could someone explain it?
Sunday, August 27, 2006
ASP.NET 2.0: Cross Page Postbacks
Most regular Web pages written by professional developers in ASP, PHP or other conventional languages use HTTP POST operation for submitting data from one page to another, often hidden page which manages data submission and normally updates a database. It was considered to be a bad practice to POST to the same page. First of all, it leads to inability to use browser's Reload/Refresh button (you would get "Page expired" message) unless some special manipulations with cache are performed. Also, it potentially leads to multiple modifications of a database in case Reload was performed.
ASP.NET introduced a paradigm of POSTing to a same page as a main method. It used internal tricks to deal with "Page expired" and multiple data updates. In my opinion, it was teaching newbie developers, who didn't realize what happens under the hood to a bad technique. (Not a first time MS is doing that, though).
It looks like obvious considerations started to overturn strange ideas of MS Web designers - thus "Cross Page Postbacks" are introduced in ASP.NET 2.0. Most of experienced Web developers who are not inclined to a weird Microsoft's idea of transforming page-based architecture of World Wide Web to Windows desktop applications behavior used it all the time in most Web languages...
Tuesday, August 22, 2006
ASP: Server.Execute vs. server-side includes
I suddenly realized on advantage Server.Execute() command has over server-side includes in a classic ASP: Server.Execute() is a part of normal execution flow and thus works as a conditional include. There is no normal way to include files conditionally with <!--#include file [or virtual]="_our_file.asp"-->, because those includes are treated with something like ASP preprocessor. On the other hand, local variables declared in calling file are available inside include and are not available in executed file - that's one reason I never used it.
VB.NET vs. C#
1) Is it right that anything which could be done with C# could be (as easy and elegant) done in VB.NET?
2) What programmers are easier to find on a market, VB.NET or C#? Which out-of-school (university) programmers are easier to find, VB.NET or C#?
3) Which programmers tend to get more money, C# or VB.NET?