Sunday 25 May 2008

Metadata for project - Part 2

Articles in this Series

Sample Metadata

For this project the Northwind database will be used, you can download it from here (it says it is for SQL 2000 but works fine on SQL 2005 and SQL 2008 although if using SQL 2008 the database will be upgraded and then will not run on SQL 2000 or SQL 2005).

Figure 1 has some tables out of the Northwind database that have been added in to the Linq to SQL model.

Northwind DBML

Figure 1

Listing 1 is the metadata on some Northwind model.

[MetadataType(typeof(OrderMetadata))]
[TablePermissions(TablePermissionsAttribute.Permissions.DenyInserts, "Accounts")]
public partial class Order
{
}

public class OrderMetadata
{
    [FieldPermissions(FieldPermissionsAttribute.Permissions.DenyEdit, "Sales", "Production")]
    [FieldPermissions(FieldPermissionsAttribute.Permissions.DenyRead, "Accounts")]
    public Object OrderDate { get; set; }

    [FieldPermissions(FieldPermissionsAttribute.Permissions.DenyEdit, "Sales", "Production")]
    [FieldPermissions(FieldPermissionsAttribute.Permissions.DenyRead, "Accounts")]
    public Object RequiredDate { get; set; }

    // entities
    [HideInFilter]
    public Object Employee { get; set; }
}

[TablePermissions(TablePermissionsAttribute.Permissions.DenyRead, "Sales")]
public partial class Order_Detail
{
}

[TablePermissions(TablePermissionsAttribute.Permissions.DenyDelete, "Sales")]
[MetadataType(typeof(CustomerMetadata))]
public partial class Customer
{
}

public class CustomerMetadata
{
    [ScaffoldColumn(false)]
    public Object UpdatedBy { get; set; }
    [ScaffoldColumn(false)]
    public Object UpdatedOn { get; set; }
    [ScaffoldColumn(false)]
    public Object CreatedBy { get; set; }
    [ScaffoldColumn(false)]
    public Object CreatedOn { get; set; }

}

[TablePermissions(TablePermissionsAttribute.Permissions.DenyRead, "Sales")]
public partial class Employee
{
}

[TablePermissions(TablePermissionsAttribute.Permissions.DenyRead, "Sales")]
public partial class Shipper
{
}

Listing 1

Note: the table level metadata is on the partial class that is implemented from the DBML file this was to workaround a bug in DynamicData 5-12 release but it seems to be the correct place.

Next is the metadata helper classes...

2 comments:

kanishka dubey said...

Where to write all this code i am new to Dynamic Data.

Stephen J. Naughton said...

Hi kanishka dubey, this is a very old post for DD and .Net 3.5 for .Net see my other post Securing Dynamic Data 4 (Replay) this has a sample you doenload and look at :)

Steve