Tuesday, 19 October 2010

Experimenting with MVC and the Entity Framework – Part 1

If you’ve got an hour – watch this http://channel9.msdn.com/posts/matthijs/ASPNET-MVC-2-Basics-Introduction-by-Scott-Hanselman/

Introduction

For some time now many of my techy colleagues have been pestering me to have a look at MVC and some of the Object Relational technologies emerging from Microsoft.

I have been telling them I’m not interested.

This was a bit of a lie, I am interested but I’m also old. - old enough to have seen some of these magic bullets before.

[more]

I was once involved in a project which all of a sudden had to adopt an MVC framework. In this case it was Apache Struts. Now this is now a pretty mature product but at the time it was new, buggy and the support was very limited. In fact, from what I remember, just about everything in the technology stack was in flux. EJB 2.0 had just come out, the Web Logic server was new, we were using some shaky open source LDAP server etc.

I hasten to add I was not the architect on the project else I would have stuck with the C++ on Sybase that was working for another year or two. Luckily the dot-com bust happened, the bank shed all its contractors and I went next door to another bank for more money on a more stable platform – those were the days.

Well – more stable in terms of the UI technology. However, the other bank had a corporate policy to adopt an Object Relational tool. I believe the Object Relational mapping later became a java standard.

This worked well for development. We built a great business object model for netting trades. Both me (as technical lead) and the architect were heavily into OO. We mapped it to the database schema and it sailed through test.

Then we hit production – firstly we had performance problems. The database was accessed using parameterised queries generated by the tool. The business process of handling a trade was in a transaction that spanned the incoming message Q and the database.

The performance was abysmal and the system kept on deadlocking.

Still, a few indexes and some hand cranking into stored procedures fixed this.

The second issue was harder. Every so often the Object cache in the third party tool would get out of sync with the database. A value displayed to the user would get “stuck” – although fortunately the underlying data in the database was OK.

We flew a support engineer from Canada to try and resolve it. Sat him down until a problem occurred, attached the java command line debugger to the process and gave him the keyboard so he could take diagnostics. At this point he said “I’ve never actually been involved with any coding before!”

Also – this bank later went on to make a few investment mistakes and collapsed creating a global banking crisis. This was not my fault or the fault of MVC.

So, when informed “there’s a great new MVC framework in town and it works great with a new Object Relational mapping technology that means you don’t need to know SQL anymore” my enthusiasm was not exactly high.

Aims

  • to assess the suitability of MVC and the Entity Framework to the Enterprise
  • to investigate these new technologies
  • to produce a working application
  • to have a bit of fun

The project

Some people say I moan a lot. I have been compared to Victor Meldrew – a character from a UK sitcom. To an extent, I agree, like Victor I get flabbergasted by the stupid things that seem to go wrong.

So – here’s the concept – a website called either Victor Meldrew or ImNeverGoingBackThere where people can say what went wrong with places they went to.

Probably stick to simple stuff to start with and then see if people use it

Basic Use Cases.

Display a list of recent gripes.

*The user is displayed a list of recent gripes. Each gripe consists of a venue type, venue name, venue location.

Search for gripes

Comment on a gripe.

Add a gripe.

Tools

Although I have a full licence for Visual Studio P.E. 20xx and SQL server developer 20xx – I decided to use the free versions to make the project useable by all.  I did not use 2010 as this is not supported on XP.  Also, I’ve stopped lugging my laptop around and wanted the whole thing on a memory stick.

So tools are –

Visual Web Developer 2008 Express Edition available free from Microsoft.  I did not use VS 2010 as s

SQL Server 2008 Express.

Windows XP SP2 and Windows 7

I will probably have to deploy on IIS6, Windows server 2003 and SQL Server 2005 as this is what’s installed in the production environment I have in mind.

Approach

I like to start all technical projects with an initial schema design, then build basic object layer on top and implement use cases to fill out the details.

I intend to blog each of these iterations

Initial Schema

Just to finish this blog with a bit of techie stuff, here is the initial schema.

clip_image002

Notes on the schema.

By convention we always create a “non natural primary key”.

Or, in English, create an integer column for the primary key which is an identity – i.e. created by the database. This column is names in<Table Name>Id

This is very good practice as:

  1. The keys created are very good indexes as they are numbers.
  2. Its easy to see from a table definition the foreign key relationships e.g. when I look at tblComment I can see instantly that intVenueId refers to tblVenue. This also makes writing stored procedures much quicker.
  3. A change in business logic or requirements will not suddenly make my key non unique.
  4. The schema is more amenable to change (or refactoring as we like to call it now). We can move any of the other columns during design/development without worrying about the PK.
  5. Generating the number in SQL as an identity means the number is guaranteed unique.
  6. Identity is efficient to generate.

I am sure there are other reasons too.

Put the PK, foreign key and all other constraints in from the start.

So many times I’ve worked on projects where “they’ll put in the constraints at the end”. What when you’re up against the deadlines and neck deep in testing and refactoring as opposed to when your doing design!

Code, technologies, users, operating systems all come and go – data stays around for years, sometimes decades. It’s going to get dirty but the more you can make the database stop this the longer it’ll take.

Create some string user defined types.

I usually define 3 user define types in SQL so I don’t need to worry about mismatches in string lengths between my stored procedures and tables.

ShortString – nvarchar(10)

MediumString nvarchar(100)

LongString – nvarchar(255)

I do this using -

CREATE TYPE ShortString FROM nvarchar(10)

Etc.

Standard columns for audit.

I also add in at least three columns for audit purposes.

I might also add in a couple of other columns on each table

intSessionId – the users login session.

intRequestId - a number for the page request

These two can be used when I want to see what data was updated by a particular user for a particular session/login and during a particular change request.

Normally I would use SQL management studio to create the database and initial schema but this is not in my toolset – so I used VS. Unsurprisingly it was pretty much the same.

In part 2 I Display some data!

Friday, 1 January 2010

Windows 8 and Dell Duo–welcome to the revolution.

The revolution

Here’s why I think Microsoft have a winner.  I have held back from buying a proper tablet for the last few years because I’m always carrying a work laptop.  I know I’d end up lumping a laptop and a tablet around and then, eventually, dump the tablet.

Now users can have it all.  Windows 8 will end up on all work devices and hey presto – they can just carry that around and use it on the train, bus and at home for Facebook, twitter and reading the FT.  The same device for business and consumer.

And when it comes to buying for the home – they’ll just opt for what they know – but pure tablet –Windows RT.

image

The fancy new start menu – the heart of Windows 8.  I can run tablet apps like the lovely FT App and Visual Studio on the same device.

image

Tim Berners-Lee (we are not worthy) on the beautiful FT Metro App.  He tells us we should be using HTML5 Web Apps.  I think the FT did!

The hardware

Dell Inspiron duo Laptop Computer

A few years ago, I blogged about my Dell Duo, it has always been an OK notebook but in tablet mode let down by a complete lack of usability.  Basically standard windows apps just haven’t got big enough buttons to use as tablet apps.

The upgrade

Then, a couple of weeks ago, I decided to upgrade to Windows 8 pro RTM.   So, I downloaded my SDN copy, burnt a dvd and, fingers crossed, upgraded.  This is the first time I’ve upgraded a windows – I normally only change OS when I change machine or, occasionally, a rebuild.

Took about 5 hours to upgrade, which was worrying, but it worked.

The result

At first I thought windows 8 was just windows 7 with a big start menu.  But then I started to use some of the “Metro” apps in tablet mode.  It’s brilliant.  My Duo now works great in tablet mode, nice UI and Microsoft have really thought through the whole gestures and menu placement.

image

Metro app

OK, I have a couple of niggles, my Bluetooth doesn’t work anymore and my little Duo seems to struggle with performance sometimes – particularly with networking.  Not sure if it’s underpowered for Windows 8 or there are still some gremlins in there.

Windows 8 professional supports Metro apps, which are available only from the windows store – which are like AppStore apps like you run on iPads, iPods and iPhones and Market apps on your android.

There are not millions of Apps yet, barely hundreds, but expect me (@stuartm9999) and @exciledderryman to sort this.

and

Windows 8 professional also allows you to run all your traditional windows apps.

But that’s not where the store ends – Microsoft are also going to release a Windows 8 RT version (for ARM processers).  This version will only run Metro apps.  With all the windows “legacy crap” dumped expect this to run great as a pure tablet.

Microsoft are due to release there own hardware – the Windows Surface with Pro and RT option and other hardware vendors like Samsung withe their Ativ have also committed to Windows 8 RT .

Sunday, 22 November 2009

XPath Checker should have been blogged

Some of my colleagues are working “offsite” at the moment which gives us an excuse to have a working lunch in town.  We have found a particularly good burger joint.

Having finished our lunch, the discussion turned to parsing html files to get betting odds – as it does.  My colleague mentioned he had spent a few hours working out some complex XPath expressions.

Whilst creating Selenium test suites one of our testers came across XPath Checker for Firefox.  This tool will adds an option on the context (right mouse button) menu of Firefox to display the XPath of the element.

Why isn’t that on your blog – he asked.  Would have saved me hours!

Tuesday, 28 July 2009

Report viewer over Https

This morning we had a long time customer ring to say they could not export reports to excel.  We are using the .net report viewer.  We tested the report ourselves and found no issues.

The customer was getting “cannot download reportviewer.axd from …”

A quick google found this - http://forums.asp.net/p/1415187/3316287.aspx#3316287

I’ll repeat the instructions here – which worked a treat:

1. In Internet Explorer, select "Internet Options..." from the "Tools" menu.
2. Click on the "Advanced" tab.
3. Scroll down to the "Security" section.
4. Un-tick the "Do not save encrypted pages to disk" check box.
5. Click the "OK" button.

I simply read these over the phone and the problem went away.

One to watch out for.

She had recently received a new computer.

Tuesday, 14 July 2009

Where Microsoft succeed.

I always try and remain technology neutral.  When asked what technologies I would use I always say there are 2 choices:

  • a J2EE with Linux or major vendor Unix on SQL Server, DB2 or Oracle.
  • .Net stack on windows with SQL Server

and which to use depends largely on what skills you have.

OK – I know that there are many technologies around that in individual cases are probably a lot more suitable to address a particular problem – but I am involved in Enterprise development which means the technologies used need to be

  • Well supported both today and in the future
  • Have a large pool of developers available
  • Are highly saleable

Recently I lend a hand to a team developing some reports using a product called JasperReports.  I found the product itself to be great and in many ways more feature rich than Reporting Services that I normally use.

But then I started to look for help.  It’s not that the help wasn’t there – it was – I just had to look much harder than on a Microsoft product.  And it just isn’t so easy to use.

And that is where Microsoft do well.  Often there products are not technically as good or they lag behind their competitors – but the tools and the documentation are usually better.

Compare Oracle to SQL Server.  It’s been a long time since I used Oracle – version 8 I think it was, but I remember how hard it was to even connect to a database.  The documentation was terrible.  The tools were limited.  From what I remember, the ESQL samples did not even compile properly – they had lots of typos in them!

Now, at the time, Oracle was probably a technically superior product, but it was much harder to use.  SQL server has probably now caught up on the technology but, IMO, it was its use ability that made it a success.

I spent 10 years developing c++ and java on various UNIX systems, and yes, the tools are very good, but they are initially no where near as easy to use a Visual Studio and required the developer to have a “bag of tricks” up their sleeves.

Thursday, 19 February 2009

Securing Reporting Services Web Services.

 

Many web based applications use reporting services to facilitate report production. We have used them extensively in many of our internal and external applications. The main drivers for deployment have been:
  • to produce PDF and other format documents
  • they are part of SQL Server
  • they have a very simple and effective deployment model
Although placing reporting services behind a second level of firewalls will secure it from external attack a mechanism is required to authenticate the client – otherwise internal users will be able to run any reports. To this end we would normally create a domain or local NT account and grant that account read access to the applications. These credentials can then be used to authenticate when using web services to execute reports.
However, when using the Microsoft ReportViewer web control it is not at first clear how to set the network credentials to call reporting services.
In order to set the credentials you must first create a class that implements the IReportServerCredentials interface as follows –
[Serializable]
    public class RsCredentials : IReportServerCredentials
    {
        #region IReportServerCredentials Members
 
        public bool GetFormsCredentials(out Cookie authCookie, out string userName, out string password, out string authority)
        {
            authCookie = new Cookie();
            userName = password = authority = null;
            return false;
        }
 
        public System.Security.Principal.WindowsIdentity ImpersonationUser
        {
            get { return null; }
        }
 
        public ICredentials NetworkCredentials
        {
            get
            {
                if (ConfigurationManager.AppSettings["rptUser"] != null)
                {
                    return new NetworkCredential(
                            ConfigurationManager.AppSettings["rptUser"],
                            ConfigurationManager.AppSettings["rptPassword"]);
                }
                else
                {
                    return CredentialCache.DefaultNetworkCredentials;
                }
            }
          
        }
 
        #endregion
    }
 
And set the report viewer report server to use these credentials.
 
m_rptViewer.ServerReport.ReportServerCredentials = new RsCredentials();
 
In this particular implementation we look for a username and password in the web.config or we use the current Network Credentials of the process.
The current network credentials can be used if you are running if your asp.net worker process is running with account credentials that have access to the reports. Most internet setups do not usually use or allow domain authentication in the web servers so in practice this mechanism is rarely used.