I have an existing Dynamic Data website that contains validation rules and attributes in the Metadata.cs in the App_Code directory. For example:
[MetadataType(typeof(RequestMetadata))]
public partial class Request { }
[DisplayName("Server Requests")]
public class RequestMetadata
{
[DisplayName("PU Required")]
[Range(1, 32)]
public object PURequired { get; set; }
}
I am now adding a WebService interface to the site to allow data to be inserted from an external website. When I insert the data using the standard DataContext object then it does not perform any of this validation.
I can't seem to see a way programmatically of querying what the correct range is for the PURequired parameter in my DB and I don't want hard code the rules directly into my code.
Is there a way I can query the MetadataType properties of the objects created from my dbml file?
As far as I see——if you wanna do validation automatically, I think you should use Dynamic Data presentation control such as GridView, FormView……LINQ's model won't do checking itself.
As far as I am aware I can use an of those as this is a Web Service with no presentation layer. This must be possible or Dynamic Data wouldn't be able to check it either. Thanks
From what I can see the metadata is added to the DD created classes via public partial classes.
What stops me accessing that portion of the class from my own code? Surely it is just a case of knowing the namespace and the appropriate function to query it.
Otherwise this seems a gaping hole in functionality. My site is really two sites, a DD site for configuration and another site using LINQ for SQL. The Webservice is part of the second site.
Hi Steaddyman, what Decker means is the the validation functions are in the field templates and are called client side and server side from the from in the field template, you will need to get access to the metamodel declared in the Global.asax.cs file to
find out what attributes are added to the entity then you will have to call you own set of functions for do the validation.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Hi Steaddyman, what Decker means is the the validation functions are in the field templates and are called client side and server side from the from in the field template, you will need to get access to the metamodel declared in the Global.asax.cs file to
find out what attributes are added to the entity then you will have to call you own set of functions for do the validation.
But how do I access that metadata model? I have no issue writing my own validation functions so long as I can read the same set of rules so I only need to change them in one place.
Now if only I could trigger the OnValidate function on the Request object I wouldn't have to do any external validation that isn't already in the web site.
Hi Seadyman, you can't as they as in the Field template user controls, I would create a set of methods that perform the type of validation you require and pass in the value and the attribute then they should return null for validate and a validation exception
for the error or something :)
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Is that right? Are you thinking of a different type of Validation. I think this validation is exactly the same as the attribute validation, but is instead in code, for example:
public partial class VLAN
{
partial void OnValidate(System.Data.Linq.ChangeAction action)
{
if (action == ChangeAction.Insert || action == ChangeAction.Update)
{
// Check the Default Gateway resides outside the client range
if (((UInt32)_Gateway >= (UInt32)_IPstart) && ((UInt32)_Gateway <= (UInt32)_IPend))
{
throw new ValidationException("The Default Gateway must reside outside the range defined by IP start and IP end.");
}
// Check the end IP address is greated than the start IP
if ((UInt32)_IPstart > (UInt32)_IPend)
{
throw new ValidationException("The IP end address cannot be less than the IP start address.");
}
// Check the start and end addresses are on the same subnet according to the mask
if (((UInt32)_IPstart & (UInt32)_Mask) != ((UInt32)_IPend & (UInt32)_Mask))
{
throw new ValidationException("The IP start and IP end addresses do not reside in the same Subnet as per the Mask.");
}
// Check start and default gateway are in the same subnet
if (((UInt32)_IPstart & (UInt32)_Mask) != ((UInt32)_Gateway & (UInt32)_Mask))
{
throw new ValidationException("The IP start and Default Gateway do not reside in the same Subnet as per the Mask.");
}
}
}
}
Member
15 Points
82 Posts
How to access MetadataType information in Metadata.cs from code?
Oct 22, 2012 12:07 PM|steddyman|LINK
I have an existing Dynamic Data website that contains validation rules and attributes in the Metadata.cs in the App_Code directory. For example:
I am now adding a WebService interface to the site to allow data to be inserted from an external website. When I insert the data using the standard DataContext object then it does not perform any of this validation.
I can't seem to see a way programmatically of querying what the correct range is for the PURequired parameter in my DB and I don't want hard code the rules directly into my code.
Is there a way I can query the MetadataType properties of the objects created from my dbml file?
Member
15 Points
82 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 23, 2012 03:45 AM|steddyman|LINK
Actually, even better than reading the Metadata would be a way of calling a validate function in code against a particular value.
Thanks
All-Star
94130 Points
18109 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 23, 2012 09:45 PM|Decker Dong - MSFT|LINK
Hi
As far as I see——if you wanna do validation automatically, I think you should use Dynamic Data presentation control such as GridView, FormView……LINQ's model won't do checking itself.
Correct me kindly if I misunderstand you.
Member
15 Points
82 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 24, 2012 02:23 AM|steddyman|LINK
All-Star
94130 Points
18109 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 24, 2012 03:10 AM|Decker Dong - MSFT|LINK
But DD has nested the function inside. Your webservice cannot have such a function;)
Member
15 Points
82 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 24, 2012 05:48 AM|steddyman|LINK
I'm not sure I understand that Decker
From what I can see the metadata is added to the DD created classes via public partial classes.
What stops me accessing that portion of the class from my own code? Surely it is just a case of knowing the namespace and the appropriate function to query it.
Otherwise this seems a gaping hole in functionality. My site is really two sites, a DD site for configuration and another site using LINQ for SQL. The Webservice is part of the second site.
All-Star
17916 Points
5681 Posts
MVP
Re: How to access MetadataType information in Metadata.cs from code?
Oct 24, 2012 01:00 PM|sjnaughton|LINK
Hi Steaddyman, what Decker means is the the validation functions are in the field templates and are called client side and server side from the from in the field template, you will need to get access to the metamodel declared in the Global.asax.cs file to find out what attributes are added to the entity then you will have to call you own set of functions for do the validation.
Always seeking an elegant solution.
All-Star
94130 Points
18109 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 24, 2012 08:28 PM|Decker Dong - MSFT|LINK
Yes, many thanks sjnaughton!;)
Member
15 Points
82 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 25, 2012 02:29 AM|steddyman|LINK
All-Star
94130 Points
18109 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 25, 2012 02:58 AM|Decker Dong - MSFT|LINK
Hello again;)
Since you are using Web Service. Maybe you can bind the entity model to the controls to do validations first and then call WebService.
Member
15 Points
82 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 25, 2012 05:06 AM|steddyman|LINK
Massive Thanks!!
I just tried the following and it actually worked.
Member
15 Points
82 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 25, 2012 09:06 AM|steddyman|LINK
Now if only I could trigger the OnValidate function on the Request object I wouldn't have to do any external validation that isn't already in the web site.
All-Star
17916 Points
5681 Posts
MVP
Re: How to access MetadataType information in Metadata.cs from code?
Oct 25, 2012 09:18 AM|sjnaughton|LINK
Hi Seadyman, you can't as they as in the Field template user controls, I would create a set of methods that perform the type of validation you require and pass in the value and the attribute then they should return null for validate and a validation exception for the error or something :)
Always seeking an elegant solution.
Member
15 Points
82 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 25, 2012 10:06 AM|steddyman|LINK
Is that right? Are you thinking of a different type of Validation. I think this validation is exactly the same as the attribute validation, but is instead in code, for example:
All-Star
94130 Points
18109 Posts
Re: How to access MetadataType information in Metadata.cs from code?
Oct 25, 2012 09:11 PM|Decker Dong - MSFT|LINK
Try it and feedback if anything wrong with that;)