Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / DynamicData / DynamicData / ModelProviders / DLinqColumnProvider.cs / 1305376 / DLinqColumnProvider.cs
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace System.Web.DynamicData.ModelProviders {
internal sealed class DLinqColumnProvider : ColumnProvider {
private static Regex s_varCharRegEx = new Regex(@"N?(?:Var)?Char\(([0-9]+)\)", RegexOptions.IgnoreCase); // accepts char, nchar, varchar, and nvarchar
private AttributeCollection _attributes;
private AssociationProvider _association;
private bool _isAssociation;
public DLinqColumnProvider(DLinqTableProvider table, MetaDataMember member)
: base(table) {
Member = member;
Name = member.Name;
ColumnType = GetMemberType(member);
IsPrimaryKey = member.IsPrimaryKey;
IsGenerated = member.IsDbGenerated;
_isAssociation = member.IsAssociation;
IsCustomProperty = !member.IsAssociation && Member.DbType == null;
Nullable = Member.IsAssociation ? Member.Association.IsNullable : Member.CanBeNull;
MaxLength = ProcessMaxLength(ColumnType, Member.DbType);
IsSortable = ProcessIsSortable(ColumnType, Member.DbType);
}
public override AttributeCollection Attributes {
get {
if (!Member.IsDiscriminator)
return base.Attributes;
if (_attributes == null) {
List newAttributes = new List();
bool foundScaffoldAttribute = false;
foreach (Attribute attr in base.Attributes) {
if (attr is ScaffoldColumnAttribute) {
foundScaffoldAttribute = true;
break;
}
newAttributes.Add(attr);
}
if (foundScaffoldAttribute)
_attributes = base.Attributes;
else {
newAttributes.Add(new ScaffoldColumnAttribute(false));
_attributes = new AttributeCollection(newAttributes.ToArray());
}
}
return _attributes;
}
}
// internal to facilitate unit testing
internal static int ProcessMaxLength(Type memberType, String dbType) {
// Only strings and chars that come in from a database have max lengths
if (dbType == null || (memberType != typeof(string) && Misc.RemoveNullableFromType(memberType) != typeof(char)))
return 0;
if (dbType.StartsWith("NText", StringComparison.OrdinalIgnoreCase)) {
return Int32.MaxValue >> 1; // see sql server 2005 spec for ntext
}
if (dbType.StartsWith("Text", StringComparison.OrdinalIgnoreCase)) {
return Int32.MaxValue; // see sql server 2005 spec for text
}
if (dbType.StartsWith("NVarChar(MAX)", StringComparison.OrdinalIgnoreCase)) {
return (Int32.MaxValue >> 1) - 2; // see sql server 2005 spec for nvarchar
}
if (dbType.StartsWith("VarChar(MAX)", StringComparison.OrdinalIgnoreCase)) {
return Int32.MaxValue - 2; // see sql server 2005 spec for varchar
}
Match m = s_varCharRegEx.Match(dbType);
if (m.Success) {
return Int32.Parse(m.Groups[1].Value, CultureInfo.InvariantCulture);
}
return 0;
}
internal static bool ProcessIsSortable(Type memberType, String dbType) {
if (dbType == null)
return false;
if (memberType == typeof(string) &&
(dbType.StartsWith("Text", StringComparison.OrdinalIgnoreCase)
|| dbType.StartsWith("NText", StringComparison.OrdinalIgnoreCase))) {
return false;
}
if (memberType == typeof(Binary) && dbType.StartsWith("Image", StringComparison.OrdinalIgnoreCase)) {
return false;
}
if (memberType == typeof(XElement)) {
return false;
}
return true;
}
internal MetaDataMember Member {
get;
private set;
}
internal void Initialize() {
if (_isAssociation && _association == null) {
_association = new DLinqAssociationProvider(this);
}
}
internal bool ShouldRemove { get; set; }
private static Type GetMemberType(MetaDataMember member) {
Type type = member.Type;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(EntitySet<>)) {
return type.GetGenericArguments()[0];
}
else {
return type;
}
}
#region IEntityMember Members
public override PropertyInfo EntityTypeProperty {
get { return (PropertyInfo)Member.Member; }
}
public override AssociationProvider Association {
get {
Initialize();
return _association;
}
}
internal new bool IsForeignKeyComponent {
set {
base.IsForeignKeyComponent = value;
}
}
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace System.Web.DynamicData.ModelProviders {
internal sealed class DLinqColumnProvider : ColumnProvider {
private static Regex s_varCharRegEx = new Regex(@"N?(?:Var)?Char\(([0-9]+)\)", RegexOptions.IgnoreCase); // accepts char, nchar, varchar, and nvarchar
private AttributeCollection _attributes;
private AssociationProvider _association;
private bool _isAssociation;
public DLinqColumnProvider(DLinqTableProvider table, MetaDataMember member)
: base(table) {
Member = member;
Name = member.Name;
ColumnType = GetMemberType(member);
IsPrimaryKey = member.IsPrimaryKey;
IsGenerated = member.IsDbGenerated;
_isAssociation = member.IsAssociation;
IsCustomProperty = !member.IsAssociation && Member.DbType == null;
Nullable = Member.IsAssociation ? Member.Association.IsNullable : Member.CanBeNull;
MaxLength = ProcessMaxLength(ColumnType, Member.DbType);
IsSortable = ProcessIsSortable(ColumnType, Member.DbType);
}
public override AttributeCollection Attributes {
get {
if (!Member.IsDiscriminator)
return base.Attributes;
if (_attributes == null) {
List newAttributes = new List();
bool foundScaffoldAttribute = false;
foreach (Attribute attr in base.Attributes) {
if (attr is ScaffoldColumnAttribute) {
foundScaffoldAttribute = true;
break;
}
newAttributes.Add(attr);
}
if (foundScaffoldAttribute)
_attributes = base.Attributes;
else {
newAttributes.Add(new ScaffoldColumnAttribute(false));
_attributes = new AttributeCollection(newAttributes.ToArray());
}
}
return _attributes;
}
}
// internal to facilitate unit testing
internal static int ProcessMaxLength(Type memberType, String dbType) {
// Only strings and chars that come in from a database have max lengths
if (dbType == null || (memberType != typeof(string) && Misc.RemoveNullableFromType(memberType) != typeof(char)))
return 0;
if (dbType.StartsWith("NText", StringComparison.OrdinalIgnoreCase)) {
return Int32.MaxValue >> 1; // see sql server 2005 spec for ntext
}
if (dbType.StartsWith("Text", StringComparison.OrdinalIgnoreCase)) {
return Int32.MaxValue; // see sql server 2005 spec for text
}
if (dbType.StartsWith("NVarChar(MAX)", StringComparison.OrdinalIgnoreCase)) {
return (Int32.MaxValue >> 1) - 2; // see sql server 2005 spec for nvarchar
}
if (dbType.StartsWith("VarChar(MAX)", StringComparison.OrdinalIgnoreCase)) {
return Int32.MaxValue - 2; // see sql server 2005 spec for varchar
}
Match m = s_varCharRegEx.Match(dbType);
if (m.Success) {
return Int32.Parse(m.Groups[1].Value, CultureInfo.InvariantCulture);
}
return 0;
}
internal static bool ProcessIsSortable(Type memberType, String dbType) {
if (dbType == null)
return false;
if (memberType == typeof(string) &&
(dbType.StartsWith("Text", StringComparison.OrdinalIgnoreCase)
|| dbType.StartsWith("NText", StringComparison.OrdinalIgnoreCase))) {
return false;
}
if (memberType == typeof(Binary) && dbType.StartsWith("Image", StringComparison.OrdinalIgnoreCase)) {
return false;
}
if (memberType == typeof(XElement)) {
return false;
}
return true;
}
internal MetaDataMember Member {
get;
private set;
}
internal void Initialize() {
if (_isAssociation && _association == null) {
_association = new DLinqAssociationProvider(this);
}
}
internal bool ShouldRemove { get; set; }
private static Type GetMemberType(MetaDataMember member) {
Type type = member.Type;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(EntitySet<>)) {
return type.GetGenericArguments()[0];
}
else {
return type;
}
}
#region IEntityMember Members
public override PropertyInfo EntityTypeProperty {
get { return (PropertyInfo)Member.Member; }
}
public override AssociationProvider Association {
get {
Initialize();
return _association;
}
}
internal new bool IsForeignKeyComponent {
set {
base.IsForeignKeyComponent = value;
}
}
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WindowHideOrCloseTracker.cs
- ListViewItem.cs
- EventEntry.cs
- DiagnosticSection.cs
- RuleElement.cs
- UriTemplateQueryValue.cs
- HwndSource.cs
- ReadWriteSpinLock.cs
- WebPartMovingEventArgs.cs
- RectValueSerializer.cs
- StrongNamePublicKeyBlob.cs
- SettingsPropertyCollection.cs
- PageThemeBuildProvider.cs
- XmlRootAttribute.cs
- Environment.cs
- DesignerVerbCollection.cs
- Viewport3DAutomationPeer.cs
- StorageAssociationSetMapping.cs
- ConsumerConnectionPoint.cs
- oledbconnectionstring.cs
- XPathItem.cs
- MediaPlayer.cs
- TypeUsageBuilder.cs
- ApplicationProxyInternal.cs
- serverconfig.cs
- CustomExpressionEventArgs.cs
- TCEAdapterGenerator.cs
- XmlSchema.cs
- XmlSchemaDatatype.cs
- UnauthorizedWebPart.cs
- SizeLimitedCache.cs
- SymmetricAlgorithm.cs
- dataSvcMapFileLoader.cs
- DodSequenceMerge.cs
- OutputCacheSettingsSection.cs
- X509CertificateValidator.cs
- HttpConfigurationSystem.cs
- QueryServiceConfigHandle.cs
- RIPEMD160Managed.cs
- EncryptedHeaderXml.cs
- controlskin.cs
- VisualBrush.cs
- TextRange.cs
- ToolBar.cs
- GridView.cs
- TripleDES.cs
- SerializationTrace.cs
- TabControl.cs
- HttpApplication.cs
- ObjectTag.cs
- UmAlQuraCalendar.cs
- XmlCDATASection.cs
- TransformDescriptor.cs
- XmlElementList.cs
- UnregisterInfo.cs
- Ipv6Element.cs
- TransactionContext.cs
- ControlCachePolicy.cs
- SystemDiagnosticsSection.cs
- TransactionInterop.cs
- OutputScopeManager.cs
- ObjectDataSourceEventArgs.cs
- Geometry.cs
- Types.cs
- _BufferOffsetSize.cs
- RegexGroup.cs
- AncestorChangedEventArgs.cs
- KnownTypes.cs
- GeometryDrawing.cs
- TCPListener.cs
- pingexception.cs
- DictionaryContent.cs
- SystemWebSectionGroup.cs
- DocumentXmlWriter.cs
- UmAlQuraCalendar.cs
- FixedSOMLineRanges.cs
- StylusPoint.cs
- TableRowGroup.cs
- ScrollBar.cs
- safex509handles.cs
- RelationshipDetailsCollection.cs
- IODescriptionAttribute.cs
- RewritingSimplifier.cs
- EventlogProvider.cs
- AbstractDataSvcMapFileLoader.cs
- WebBrowserPermission.cs
- HttpCachePolicyBase.cs
- SqlIdentifier.cs
- SQLInt32Storage.cs
- MenuAdapter.cs
- GetRecipientRequest.cs
- PngBitmapEncoder.cs
- SemanticValue.cs
- __FastResourceComparer.cs
- SiteMapDataSourceDesigner.cs
- OperationAbortedException.cs
- WebSysDisplayNameAttribute.cs
- ProofTokenCryptoHandle.cs
- NominalTypeEliminator.cs
- WSSecureConversation.cs