1
\$\begingroup\$

In my web development company, we have many designers who have the following development knowledge:

  • Client-side web languages (HTML, CSS, JavaScript)
  • Basic database design (how to create tables, relationships, etc), and
  • Basic SQL

So we thought about giving them a simple platform to create data-oriented web apps.

  1. What do you think of the following coding style for ASP.NET MVC?
  2. How can we improve it?
  3. Any ideas for a better SQL injection solution than the params array? Remember that the code should look clean and simple.

/apps/pages/index.html

@(DB.Select(table: "pages", 
            where: "id = ? and date > ?",
            params: new[] { Request.QueryString["ID"], Request.QueryString["Date"] }))

<html>
<body>
    <ul>
    @foreach (var row in Model) {
        <li><a href="@row.URL">@row.Name</a></li>
    }
    </ul>
</body>
</html>

/apps/pages/create.html

@(DB.InsertOnPost(table: "pages",
                  fields: "name, description, date",
                  values: "?, ?, Now",
                  params: new[] { Request.Form["Name"], Request.Form["Description"] }))

@RedirectOnPost("/pages")

<form action="/pages/create" method=”post”>
    <div>
        <label for="name">Name</label>
        <input type="text" name="name" />
    </div>

    <div>
        <label for="description">Description</label>
        <textarea name="description"></textarea>
    </div>
</form>

/apps/pages/edit.html

@(DB.UpdateOnPost(table: "pages",
                  set: "name = ?, description = ?",
                  where: "id = ?",
                  params: new[] { Request.Form["Name"], Request.Form["Description"], 
                                  Request.QueryString["ID"] }))

@RedirectOnPost("/pages")

@(DB.Select(table: "pages", 
            where: "id = ?",
            params: new[] { Request.QueryString["ID"] })

<form action="/pages/edit" method="post">
    <div>
        <label for="name">Name</label>
        <input type="text" name="name" value="@Model.Name" />
    </div>

    <div>
        <label for="description">Description</label>
        <textarea name="description">@Model.Description</textarea>
    </div>
</form>
\$\endgroup\$
2
  • 2
    \$\begingroup\$ What's the reason for not using controllers and repositories for interaction with the database? \$\endgroup\$ Commented Jul 26, 2013 at 19:16
  • 2
    \$\begingroup\$ I'm not too sure if I'm keen on mixing the DB specific stuff in with the views themselves. Seems to be crossing boundaries to me \$\endgroup\$
    – dreza
    Commented Jul 27, 2013 at 19:28

1 Answer 1

2
\$\begingroup\$

That coding style looks more like the WebMatrix Web Pages pattern rather than ASP.Net MVC. If this coding style is desired then it would be preferable to go ahead and use the data access library that is usually used with Web Pages, the WebMatrix.Data library. That will by default guard against SQL Injection as long as the convention of the parameterized queries is used, e.g.

    selectCommand = "SELECT URL, Name FROM Pages WHERE ID = @0 AND Date = @1";
    selectedData = db.Query(selectCommand, Request.QueryString["Id"], Request.QueryString["Date"]);

This should be OK for simple sites, but for larger or more complex sites you would be better to separate the data access from the presentation layer, using the full MVC pattern. The downside of the pattern shown here is that is difficult to test or debug compared to MVC or WebForms and it tends to introduce duplication in the code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.