Fremus.co.za

Demistifying Life and Web Development

Archive for February, 2011

Why devs must support their own machines

One of my recent annoyances comes from IT support providing a developer, like me, with a laptop pre-installed with all the corporate mumbo jumbo. And in my case they do the installation using ghost images or ghost installations, so the OS is not really configured for the hardware of your particular machine, which in some cases results in messages appearing on-screen relating to graphics drivers crashing. Thats my opinion anyway. IT support also generally also install whatever anti-virus whether its any good or not, which in most cases slows the machine down. I have also had software installed on my machine that affects localhost on IIS for some reason, which I cannot use for development when I am connected to the network, but when I disconnect I can. I have no idea which piece of software is causing this, and I can think of one particular piece of software that could be causing this. Personally I would prefer installing Windows 7 from the start at some point, with the appropriate and correct device drivers.

In my opinion devs should be allowed to take total ownership of their dev machine which includes OS maintenance and installation. In this way they are totally accountable for any problems that may occur and you will find that most devs need to know what the OS can do and what not, so its best for them to take ownership. My current machine has 8GB of RAM and has a core i7 processor but it does not feel that quick in some cases, but maybe thats just me.

  • Share/Bookmark
posted by fr3dr1k in General and have No Comments

HOWTO: Check all items, and get all checked items

I often find myself re-using code, in the bad way (copying and pasting) without really remembering how it was done, and this is particularly true for JavaScript. I could easily say that I do most of my JavaScript with jQuery, so here is a function that enables all checkboxes with a given name:

var general = {
    selectAllEntities: function (chkBoxName, checked) {
        $('input[name=' + chkBoxName + ']').each(function () {
            $(this).attr("checked", checked);
        });
    },
    getAllCheckedEntities: function (chkBoxName) {
        var entityValues = [];
        $('input[name=' + chkBoxName + ']').each(function () {
            if ($(this).attr("checked")) {
                entityValues.push($(this).val());
            }
        });
        return entityValues;
    }
};

jQuery makes it easier with its $ selector, so if I had to write this without jQuery the code might be significantly longer.

  • Share/Bookmark
Tags:
posted by fr3dr1k in jQuery and have No Comments

Paging for ASP.NET MVC

I bought Steve Sanderson’s book Pro ASP.NET MVC about 6 months ago and I must admit its a real winner for the single reason that the code and explanations are clear and to the point, and he actually gives you code you can use easily. In chapter 4 he builds a paging helper class which is really cool and works well when you bind the model to your view but the way he implements it could be made better by creating a class that accepts a generic type and I also want to use this method for Ajax/jQuery. The code in his book for the PagingInfo class looks like this:

public class PagingInfo
{
	public int TotalItems { get; set; }
	public int ItemsPerPage { get; set; }
	public int CurrentPage { get; set; }
	public int TotalPages
	{
		get
		{
			return (int)Math.Ceiling(decimal)TotalItems / ItemsPerPage;
		};
	}
}

In the controller in his book he then creates a class called, ProductsListViewModel, which has two properties, IList Products and PagingInfo PagingInfo:

public class ProductsListViewModel
{
	public IList
 Products { get; set; }
	public PagingInfo PagingInfo { get; set; }
}

On a side note I must say I agree in part with Jeffrey Richter in his book C# via CLR that properties are a weird mix of a method and not a method and sometimes it does feel as if what you are doing in a property might work in a method also. But I digress. In his controller he has a action result that binds the ProductsListViewModel to the view:

public ViewResult List([DefaultValue(1)] int page)
{
	var productsToShow = productsRepository.Products;
	var viewModel = new ProductsListViewModel
	{
		Products = productsToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList();
		PagingInfo = new PagingInfo {
			CurrentPage = page,
			ItemsPerPage = PageSize,
			TotalItems = productsToShow.Count()
		}
	}
	return View(viewModel);
}

Before I continue there are a few things to note about the controller method and the first is that I have not shown the productsRepository and its implementation and the second thing to note is that PageSize is a public variable declared in the controller class.

The code works great, and I have used it now in several places and I really like it because it feels as if the PagingInfo class provides an abstraction that the entity you are paging does not know about. This makes it feel light and easy to implement. I do however think it might be useful to create a class that accepts a generic type instead. So what I have done is create a class called EntityViewModel, and it looks a bit like this:

public class EntityViewModel
{
	private List listOfEntities;
	private PagingInfo pagingInfo;

	public List ListOfEntities
	{
		get { return listOfEntities; }
		set { this.listOfEntities = value; }
	}
	public PagingInfo PagingInfo
	{
		get { return pagingInfo; }
		set { this.pagingInfo = value; }
	}
}

This will mean that when I create a controller method I can do this instead:

public ViewResult List([DefaultValue(1)] int page)
{
	var productsToShow = productsRepository.Products;
	var viewModel = new EntityViewModel
	{
		ListOfEntities = productsToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList();
		PagingInfo = new PagingInfo {
			CurrentPage = page,
			ItemsPerPage = PageSize,
			TotalItems = productsToShow.Count()
		}
	}
	return View(viewModel);
}

Which to me seems a bit more flexible. It doesnt end there though because I want to consume the controller method using jQuery and jQuery templates and for that I need to change the controller method slightly:

public JsonResult List([DefaultValue(1)] int page)
{
	var productsToShow = productsRepository.Products;
	var viewModel = new EntityViewModel
	{
		ListOfEntities = productsToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList();
		PagingInfo = new PagingInfo {
			CurrentPage = page,
			ItemsPerPage = PageSize,
			TotalItems = productsToShow.Count()
		}
	}
	return Json(viewModel);
}

Now I can get a JSON result, AND it has paging which is totally awesome!

  • Share/Bookmark
posted by fr3dr1k in ASP.NET MVC,jQuery and have No Comments

When Stored Procedures are a bad thing…

Stored procedures are remains one of a DBA’s main weaponry, there is no doubt in that, but in my opinion there are a few cases where stored procedures are a bad thing.

The first reason is that they lock you down into vendor-specific syntax which ultimately means that your system can only run on a vendor-specific RDBMS. This is fine if you are never planning on creating a shippable product, but if you do intend to create a shippable product the data layer in your product should not be dependent on a specific RDBMS. I am sure there are nuances that separate SQL Server Stored Procedure syntax from Oracle syntax, which means that if you intend to make a product work on say both SQL Server and Oracle you might have to rewrite parts of your stored procedures to be compatible with the specific vendor.

The second reason stored procedures are bad is because DBA’s try to control too much, they tend to sometimes dabble too much and exert too much control, which leads to application inflexibility. I have seen instances where the DBA returned HTML from a stored procedure, which is a total WTF moment. What happens if your data needs to be consumed in a non-web environment, like Windows Forms or WPF? What do you do with the HTML? The point is that the RDBMS is responsible for data integrity and data integrity alone, and not for returning HTML (its the role of the client consuming the data). I always had the impression that some DBAs would even run the web server using SQL Server. I have also seen DBAs place domain specific code in stored procedures. In other words they let the SQL handle business rules, which is inflexible again and it muddies the water in terms of what the code using the data must do.

The third reason stored procedures are bad is that they can become really cumbersome to maintain, in which case it makes better sense to rather have your programming language deal with data access and not have your business rules in stored procedures.

The fourth reason stored procedures are bad is because there is a preconceived notion or idea that they are more secure, which again is a load of crap. The only thing that makes it seem more secure is because the roles for a database have not been properly assigned and that you are trying to be “more secure” by adding T-SQL to apply user-specific rules. I have seen this approach fail where data gets leaked to users that are not suppose to see it because the code in the stored procedure was dodgy, not because the security was inherently bad. You can lockdown roles and their permissions in your database to a reasonable extent.

The fifth reason stored procedures are a bad thing is because they don’t really outperform ADO.NET code.

  • Share/Bookmark
posted by fr3dr1k in SQL Server 2005,SQL Server 2008 and have No Comments

What are extension methods?

I was looking for questions that a Senior .NET developer could expect and one of the questions I would ask is to explain what extension methods are. If I am not mistaken ASP.NET MVC 2.0 uses extension methods to create HTML Helpers, which would make perfect sense since ASP.NET MVC might not have all the functionality baked in, that you may require.

Extension methods essentially provide a developer with a mechanism to extend the base functionality of a particular class. So lets say you want ToString() for a class to return a date in a particular format, then you could write an extension method that transforms the ToString() representation of that object into the DateTime format you desire. Creating an extension method requires that you create a static class with a static method, like:

    static class Extensions
    {
        public static string ToString(this Concept concept, DateTime startDate)
        {
            return startDate.ToString("dddd MMMM yyyy");
        }
    }

Where concept is an arbitrary class I created. If I create an instance of Concept and call ToString() I get the overload:

            Concept concept = new Concept();
            Console.WriteLine(concept.ToString(new DateTime()));
  • Share/Bookmark
Tags: ,
posted by fr3dr1k in C# and have No Comments
Get Adobe Flash playerPlugin by wpburn.com wordpress themes