Wednesday, 28 January 2009

Making Individual Tables Read Only – Dynamic Data

These are two thing you can do to make your table Read Only.

  1. Add an attribute for instance the ReadOnlyAttribute or your own custom attribute
  2. Using Routing to restrict access to the Edit or Insert pages

The first solution I covered in this article Writing Attributes and Extension Methods for Dynamic Data so I’ll just show the Routing example here.

// Setting a table to Read only
var visibleTables = from t in MetaModel.Default.Tables
                    where t.Scaffold == true
                    select t;

var readOnlyTables = new StringBuilder();
foreach (var table in visibleTables)
{
    var isReadOnly = table.Attributes.OfType<ReadOnlyAttribute>().
DefaultIfEmpty(new ReadOnlyAttribute(false)).
FirstOrDefault();
    if (isReadOnly.IsReadOnly)
        readOnlyTables.Append(table.Name + "|");
}

routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
{
    Constraints = new RouteValueDictionary(new
    {
        action = "Edit|Insert",
        table = readOnlyTables.ToString().Substring(0, readOnlyTables.Length - 1)
    }),
    ViewName = "Details",
    Model = model
});

Listing 1 – Routing example.

[ReadOnly(true)]
public partial class Employee {}

Listing 2 – sample attribute.

In Listing 1 we see three things going on:

  1. We get a list of visible tables using the Scaffold attribute to find out if the table should be shown.
  2. Next we loop through the visible tables and if the table is set to ReadOnly then we add it to the readOnlyTables StringBuilder to build a list of read only tables (e.g. “Products|Customers”).
  3. Build a route that constrains the these tables to only get to the Details page.
Note: That this route must appear before the default routes.

Happy coding and remember your a PC HappyWizard but for us coders I prefer the short lived “Live to Code and Code to Live”

Important: The only caveat here is that the Delete button still works. :( But you could catch that in Business logic if you didn’t want to edit each page.

8 comments:

Anonymous said...

Hi,

I Used [ReadOnly(true)] in my table, but it doesn't work for foreign keys! Which attributes should I use for make them read only also?


Best regards,


Sahar

Stephen J. Naughton said...

Hi Sahar, you need this article here (http://csharpbits.notaclue.net/2009/01/making-field-read-only-via.html)

Steve :D

ET said...

make sure you have the right name for the column, since it's a foreign key VS might have capitalized it and appended it with the number 1.

:)))

Stephen J. Naughton said...

That is understood :) and has been cover several time in the forums plus you would get an error when you run your appp as the meta model woudl not recognise the table.

Steve

Tahir Hassan said...

Awesome,

My solution is not exactly the same as yours but it is fairly similar.

Thanks,
Tahir

Stephen J. Naughton said...

Hi Tahir, this is a fairly old post and I would not do this this way today.

Steve

Unknown said...

Steve, could you please give me a clue to how do that?

I already installed your Github packages, but i´m in the beginning! :(

Thanks, from Portugal =)

Stephen J. Naughton said...

Now you just add the [ReadOnly(true)] to the table and the rest is done for you.