Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I read with interest this blog post from LosTechies:<br> <a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/01/14/a-new-breed-of-magic-strings-in-asp-net-mvc.aspx" rel="nofollow noreferrer">A new breed of magic strings in ASP.NET MVC</a></p> <p>They suggest changing this:</p> <pre><code>Url.Action&lt;ProductsController&gt;(x =&gt; x.Edit(null), new { productId = prodId }) </code></pre> <p>which contains the magic string productID, to this:</p> <pre><code>Url.Action&lt;ProductsController&gt;(x =&gt; x.Edit(null), new RouteValueDictionary { { ProductsController.ProductId, prodId } }) </code></pre> <p>ProductId is now strongly-typed.</p> <p>Or you can create a real object instead of an anonymous one:</p> <pre><code>Url.Action&lt;ProductsController&gt;(x =&gt; x.Edit(null), new EditProductParams { product = prodId }) </code></pre> <p>But now you need this:</p> <pre><code>public class EditProductParams { public Guid product { get; set; } } </code></pre> <p>Their last suggestion is to use a fluent interface. You can write this:</p> <pre><code>Url.Action&lt;ProductsController&gt;(x =&gt; x.Edit(null, null), Params.Product(product).Customer(customer)) </code></pre> <p>Which looks pretty clean (if a little redundant), but it requires this:</p> <pre><code>public static class Params { public static ParamBuilder Product(Product product) { var builder = new ParamBuilder(); return builder.Product(product); } public static ParamBuilder Customer(Customer customer) { var builder = new ParamBuilder(); return builder.Customer(customer); } } </code></pre> <p>...and this:</p> <pre><code>public class ParamBuilder { private Product _product; private Customer _customer; public ParamBuilder Product(Product product) { _product = product; return this; } public ParamBuilder Customer(Customer customer) { _customer = customer; return this; } } </code></pre> <p>...and this:</p> <pre><code>public static implicit operator RouteValueDictionary(ParamBuilder paramBuilder) { var values = new Dictionary&lt;string, object&gt;(); if (paramBuilder._product != null) { values.Add("p", paramBuilder._product.Id); } if (paramBuilder._product != null) { values.Add("c", paramBuilder._customer.Id); } return new RouteValueDictionary(values); } </code></pre> <p>I could see this working if it could be written using generics, so that it would only have to be written once.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload