What’s Good About Microsoft’s .NET, C#, and ASP.NET? Andy Gravell, May 2009 Introduction • No system or platform is perfect, but Microsoft .NET has some impressive features – for use in our teaching and research – for commercial software development • The observations in this talk are drawn from – personal experience – comments from colleagues and students – articles and white papers on the web Disclosure • Studied Mathematics, then Computer Science • Worked as a programmer for seven years – mostly at IBM’s Hursley lab • Persuaded ECS to teach Theory of Computing – but my current classes are in the area of ebusiness and web development • As a member of DSSE, my research area was Formal Methods – but am more interested in Agile Methods now The Evil Empire? • When I was a student, IBM were considered the evil empire • Now Microsoft have been found guilty of anticompetitive behaviour, some colleagues are reluctant to consider any of their software • In fact, both IBM and Microsoft have been successful in developing a number of popular and useful technologies – technology transfer & commercialisation Contributions From Industry IBM • Computer architecture • Operating systems – OS/360, virtual machines Microsoft • Web services (with IBM) • “Visual” programming – visual basic, visual studio • High level languages • Programming languages – Fortran (and PL/I) – powershell, C#, F#, ... • Relational databases • Formal methods – VDM, Z • Open source (eg Linux) • ODBC • Software analysis tools – security/crash/driver – spec# Why .NET? • At the end of the 1990s, a faction in Microsoft argued in favour of focussing on an Internet O/S instead of Windows* – M/S has continued to develop Windows, of course – but also introduced .NET version 1 in 2002 • Possibly a consequence of Sun’s 1997 lawsuit alleging Microsoft’s extended and incompatible version of Java had breached their contract *Breaking Windows, David Banks, Free Press 2001 What is .NET? • Like Java, a multi-target platform – .NET programs can run on desktop and mobile Windows machines, and in the browser – also on Linux/BSD/ OS X thanks to the mono project • Unlike Java, there are ISO standards* for – Common language infrastructure (run-time) – C# programming language • Unlike Java, .NET supports multiple languages – C#, VB, C++, Ada, Cobol, F#, JScript, Python, Ruby, ... *ISO/IEC 23270 and 23271 .NET is Popular in Industry • The technical skills most cited in job adverts* are: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. SQL 13335 total ads .NET 12683 SQL Server 11327 Windows 11075 C# 10053 Java 8220 Oracle 7446 ASP.NET 6981 UNIX 6527 XML 6402 *IT Jobs Watch UK £40K average salary £40K £39K £37K £42K £46K £47K £37K £46K £39K What is ASP.NET? • A framework for web applications – framework = library + skeleton main program • Typically used together with Visual Studio – or Visual Web Developer Express Edition (free) • Without these, web development is difficult: – multi-skilled (text & graphical content, code) – multi-language • XHTML, CSS, JavaScript, OOPL, XML, SQL – increasing demands on user experience Web Controls • ASP.NET web pages (aka web forms) are made up of a number of ASP.NET web controls – around 100 controls are provided, grouped into – standard, data, validation, navigation, login, AJAX • Can drag and drop these from the toolbox, or code them via web form markup or OO code • Each control has properties which allow you to configure its appearance and behaviour • You can also code your own, or buy extras ASP.NET Page Life Cycle • When a page/form is first loaded its markup is parsed and an object created for each control or standard HTML element • Each object is initialised and its initial properties are set according to the web form markup • Events are called based on the page state and any user interaction with the page via their browser • The list/tree of controls is walked and each in turn is asked to render itself (output HTML for the browser) The Three Styles • “Visual” – drag and drop GUI/Web UI builder – setting properties via forms and check boxes • “Declarative” – entering definitions of controls and properties – using an XML based markup language • “Programmatic” – plain old fashioned (but OO) code – allows for more dynamic interactions Benefits of This Approach • This event-driven style of development is very similar to modern Windows programming • Rich database-driven web applications can be created using the (code-free) “visual” style – complex logic can still be coded when needed – and the different styles can be mixed • Despite the multiple layers of translation, ASP.NET web applications run as fast as Java(EE), and use less code [Petstore benchmark] Recent Developments • The slides so far describe facilities already present in ASP.NET versions 1.0 and 2.0 • Recent enhancements include – – – – – Silverlight/Moonlight (Flash competitor) LINQ (Hibernate competitor) MVC (Struts competitor) Dynamic Data (Ruby on Rails competitor) Chart controls • LINQ in particular uses new C# language features (Some of the) New Language Features • C# 2.0 introduced – partial classes, iterators, nullable types • C# 3.0 introduced – object and collection initialisers – anonymous types and type inference – lambda expressions and expression trees – partial and extension methods • Visual Basic has introduced most of these and also XML literals LINQ • LINQ stands for Language INtegrated Query • Adds a query sublanguage to C# and VB.NET • Familiar (SQL-like) syntax – using the C# language extensions listed above • You can teach yourself LINQ using LINQPad* – and the Microsoft 101 LINQ samples *Albahari and Albahari: http://www.linqpad.net/ Simple LINQ Examples • To select integers from 3 to 5 and their squares: from x in (new int[] {1,2,3,4,5}) where (x > 2) select (new {x, square = x*x}) • This is a sugared version of (new int[] {1,2,3,4,5}) .Where(x => x > 2) .Select(x => new {x, square = x*x}) LINQ to SQL Queries • Once a database mapping has been defined, a database query can be coded similarly from p in Policies where (p.PolicyID > 1) orderby p.Type select (new {p.Premium, p.Type}) • A query such as this is stored as an expression tree, and executed lazily when the data is required • The LINQ code is translated into an optimised SQL query at run-time Running the Simple Queries Running Database Queries Benefits of In Language Queries • • • • Compile time (and IDE) type checking Syntax highlighting, autocompletion Eliminates SQL injection attacks Extensible – can also query XML and .NET collection classes – and one query can combine all these sources – and you can code your own LINQ methods Summary • Teaching C# and ASP.NET is worthwhile – interested advanced language features – simplifies professional web development – already successful in ELEC2018 and COMP6003 • If you wish to promote your research with a rich interactive web site, consider ASP.NET • The IT industry has selected ASP.NET – rich, flexible and powerful web controls and class libraries – less code higher productivity, lower maintenance – high performance