Mvc Scaffolding - Part two

I need to start tonight's blog post with an apology … in last nights post, I neglected to explain what NuGet is – I kind of took it for granted that you developers out there would know.

NuGet is a Microsoft spin off that is intended to make the introduction of new developer frameworks to Visual Studio and your projects easier – removing the need for you to remember all the assembly dependencies. All you do is open the Package Manager Console (View, Other Windows, Package Manager Console) and key in a few PowerShell commands – and NuGet will pull down the dependencies onto your machine, and update your open project. Simples.

So, lets get rocking. We have our sample project (MVC 3 Razor) up and running. For this sample we are going to build a very simple prototype around an order, and the information it would hold.

We need the following classes:

Address.cs

public class Address
{
    [Key]
    public Guid AddressID { get; set; }

    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public string PostCode { get; set; }
    public string Country { get; set; }
}

OrderItem.cs

public class OrderItem
{
    [Key]
    public Guid OrderItemID { get; set; }

    public int Quantity { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
}

Order.cs

public class Order
{
    [Key]
    public Guid OrderID { get; set; }

    public Address DeliveryAddress { get; set; }
    public Address InvoiceAddress { get; set; }

    public DateTime OrderDatae { get; set; }

    public List<OrderItem> OrderItems { get; set; }
}

You’ll probably notice the [Key] decoration in that lot – these define key indexes, which Entity Framework Code First needs to setup the necessary indexes for CRUD operations on the database. You will need to add a using statement to each of your class files for the namespace of System.ComponentModel.DataAnnotations.

So – that defines an order which has two address (one for the delivery and one for the invoices), and a list of items.

Lets go ahead and create a controller, and the CRUD pages for the Order. Open up Package Manager Console, and type in

scaffold controller Order

You will see that the MvcScaffolding framework gets busy and creates the relevant update pages – but most importantly it creates a Context, also within the Models folder. In this example its called ScaffoldingExampleContext.cs. This brings us to the first couple of annoyances with Scaffolding, and EFCodeFirst.

- The database context gets placed in the same project – no easy separation here to give you a distinct data access layer.

- The context connection string expects you to have SQLExpress installed, and operating as a named instance of “SQLExpress”.

On my machines, the latter is not the case, so we need to pop into web.config and add a ConnectionString into the configuration block:

<connectionStrings>
  <add name="ScaffoldingExampleContext" connectionString="data source=.;Integrated Security=SSPI;Database=ExampleData" providerName="System.Data.SqlClient"/>
</connectionStrings>

Fire up your project, and got to /Orders/ on the site that launches – you will be presented with a very simple CRUD interface for the Orders.

You would need to run the same Mvc Scaffolding command (scaffold controller <classname>) against each of the classes, and then you have a little but of manual plumbing to do – such as navigation, and in this case, the actual selection of things like Address and Items.

Scaffolding, at this time, can not handle complex objects such as other classes or list definitions to give you a total coverage CRUD interface, but it can generate most of the simple stuff that makes up a lot of the work involved in most prototypes.

image

Comments are closed