The Generic Application
This is the final part in a 4 part series where I describe an attempt to implement some generic principles I thought up at the cottage this past summer.
In part 1 I discussed XML serialization and its uses.
In part 2 I discussed the creation of a generic data layer that utilizes the XML serialization techniques.
Part 3 describes the creation and usefulness of a generic business layer that utilizes the generic data layer.
This final article describes how to bring all these techniques together in a real world example, and I hope I can convince you to at least investigate these techniques further, as I have found the following advantages (so far):
- Extremely fast coding
- Extremely small amount of code required
- Highly standardized
- Allows focus on where it really counts – OO Principles & Design
- Use this extra time to enhance the user interface with some cool web 2.0 crap
Retrieval
Here is a screen shot, as an example of the complete code behind to retrieve a list of entities (ContentEntry in this case), sort them, then iterate through them displaying the first 5 entries. You should recognize this as the source code for the “Most Recent” article control on the left.
If you are really interested in a copy paste example, then here is the entire code behind just in case you thought there was some other work being done:
using System; using System.Data; using System.Configuration; using System.Collections;
using System.Collections.Generic; using System.Web; using System.Web.Security;
using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; using TwentySeven.Blogger.Services;
public partial class Controls_MostRecent : System.Web.UI.UserControl
{ #region Events /// <summary> /// Handles the Load event of the Page control.
/// </summary> /// <param name="sender">The source of the event.</param> ///
<param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
protected void Page_Load(object sender, EventArgs e) { UpdateView(); }
#endregion #region Methods /// <summary> /// Updates the view. ///
</summary> private void UpdateView()
{ //Get the 5 most recent articles GenericBusiness<ContentEntry> biz =
new GenericBusiness<ContentEntry>(); ContentEntryDateComparer byDate =
new ContentEntryDateComparer(); biz.Items.Sort(byDate); HtmlGenericControl li; int i = 1;
foreach (ContentEntry entry in biz.Items)
{ //This makes only the top 5 get listed if (i > 5) break; if (entry.Published)
{ li = new HtmlGenericControl("li"); li.InnerHtml =
"<a href='" + Navigator.ContentView(entry.Id) + "' title='" + entry.Title + "'>" + entry.Title +
" (" + entry.CreateDate.ToShortDateString() + ")</a>";
mostRecentList.Controls.Add(li); i++; } } } #endregion }
Notice the ease of the creation of the generic business layer, then how simple it is to apply
- A sort: biz.Items.Sort(byDate);
- A strong typed iteration: foreach (ContentEntry entry in biz.Items)
Inserting, Updating, Persitance and all that CRAP, er… I mean CRUD
Typical crud is just as easy. Here is another example of either the insert or the update of a record:
Note the insert and update of the entities are single line items, and the persistance is basically junior developer proof!
Weaknesses
Well, as with any new attempt to have some fun and discover a new (to me at least) concept, there are some weaknesses to this type of strategy:
- Table joins are not done easily. There are some work arounds I have found but it is not always pretty.
- The current scheme forces validation to be done in either the entity itself, or in the UI, both of which make me throw up in my mouth a little bit – perhaps an enhancement would be a GenericValidation scheme, now there is a thought…
- Systems with large amounts of records are going to be faster with a traditional database due to indexing, however this is pretty damn fast right now since if you look closely at the generic data and business layers, there is heavy use of caching.
- As noted in the previous point, due to the large about of caching used by these techniques, memory mangement MAY become an issue, however I would offer the bit of advice of not solving an issue until something actually IS an issue.
Conclusions
Take it or leave it, I love this technique for simple apps like the site you are using right now – it is exclusively developed using these generic techniques. I love that it allows me to literally start defining and creating my entities (classes), and then immediately start implementing the user interface and being PRODUCTIVE. I recently picked up PHP and wrote a PHP OO code generator called PHPPooper (pronounced puh-pooper if you will, since is shits PHP code like crazy) The thing I was struck by most using PHP was simply how fast and easy it was to be productive almost from the first hour of coding. I find .Net in general although powerful for enterprise level applications, sometimes takes some time to get really valuable. These generic techniques have almost reinvigorated me in .Net.
Please use the link below to send any comments, I will work hard to post and answer them as required! p>



Feedback