Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / DynamicData / DynamicData / EntityTemplateFactory.cs / 1305376 / EntityTemplateFactory.cs
using System.Diagnostics; using System.Globalization; using System.Web.Compilation; using System.Web.Hosting; using System.Web.UI.WebControls; namespace System.Web.DynamicData { public class EntityTemplateFactory { private const string s_defaultTemplateName = "Default"; private TemplateFactory _factory; private Func_templateInstantiator; public EntityTemplateFactory() : this(CreateEntityTemplateInstance, /* trackChanges */ true) { } // for unit testing internal EntityTemplateFactory(Func templateInstantiator, VirtualPathProvider vpp) : this(templateInstantiator, /* trackChanges */ false) { _factory.VirtualPathProvider = vpp; } private EntityTemplateFactory(Func templateInstantiator, bool trackChanges) { _factory = new TemplateFactory("EntityTemplates", trackChanges); _templateInstantiator = templateInstantiator; } internal string TemplateFolderVirtualPath { get { return _factory.TemplateFolderVirtualPath; } set { _factory.TemplateFolderVirtualPath = value; } } private static EntityTemplateUserControl CreateEntityTemplateInstance(string path) { return (EntityTemplateUserControl)BuildManager.CreateInstanceFromVirtualPath( path, typeof(EntityTemplateUserControl)); } public virtual EntityTemplateUserControl CreateEntityTemplate(MetaTable table, DataBoundControlMode mode, string uiHint) { if (table == null) { throw new ArgumentNullException("table"); } string entityTemplatePath = GetEntityTemplateVirtualPathWithCaching(table, mode, uiHint); if (entityTemplatePath == null) { return null; } return _templateInstantiator(entityTemplatePath); } private string GetEntityTemplateVirtualPathWithCaching(MetaTable table, DataBoundControlMode mode, string uiHint) { long cacheKey = Misc.CombineHashCodes(table, mode, uiHint); return _factory.GetTemplatePath(cacheKey, delegate() { return GetEntityTemplateVirtualPath(table, mode, uiHint); }); } public virtual string GetEntityTemplateVirtualPath(MetaTable table, DataBoundControlMode mode, string uiHint) { if (table == null) { throw new ArgumentNullException("table"); } // Fallback order is as follows (where CustomProducts is the uiHint) // CustomProducts_Insert // CustomProducts_Edit // Products_Insert // Products_Edit // Default_Insert // Default_Edit // CustomProducts_ReadOnly // Products_ReadOnly // Default_ReadOnly // // If nothing matches null is returned return GetVirtualPathFallback(table, mode, uiHint, DataBoundControlMode.Edit) ?? GetVirtualPathFallback(table, DataBoundControlMode.ReadOnly, uiHint, DataBoundControlMode.ReadOnly); } private string GetVirtualPathFallback(MetaTable table, DataBoundControlMode mode, string uiHint, DataBoundControlMode minModeToFallBack) { if (mode < minModeToFallBack) { return null; } // the strategy is to go over each candidate name and try to find an existing template for // each mode between 'mode' and 'minModeToFallBack' // note that GetVirtualPathForMode will return null for empty names (e.g. when the uiHint is not specified) string[] fallbackNames = new string[] { uiHint, table.Name, s_defaultTemplateName }; foreach (var name in fallbackNames) { for (var currentMode = mode; currentMode >= minModeToFallBack; currentMode--) { string virtualPath = GetVirtualPathForMode(name, currentMode); if (virtualPath != null) { return virtualPath; } } } return null; } private string GetVirtualPathForMode(string candidateName, DataBoundControlMode mode) { if (String.IsNullOrEmpty(candidateName)) { return null; } else { string templatePath = BuildEntityTemplateVirtualPath(candidateName, mode); return _factory.FileExists(templatePath) ? templatePath : null; } } public virtual string BuildEntityTemplateVirtualPath(string templateName, DataBoundControlMode mode) { if (templateName == null) { throw new ArgumentNullException("templateName"); } string modeString = mode == DataBoundControlMode.ReadOnly ? String.Empty : "_" + mode.ToString(); return String.Format(CultureInfo.InvariantCulture, this.TemplateFolderVirtualPath + "{0}{1}.ascx", templateName, modeString); } internal void Initialize(MetaModel model) { Debug.Assert(model != null); _factory.Model = model; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System.Diagnostics; using System.Globalization; using System.Web.Compilation; using System.Web.Hosting; using System.Web.UI.WebControls; namespace System.Web.DynamicData { public class EntityTemplateFactory { private const string s_defaultTemplateName = "Default"; private TemplateFactory _factory; private Func _templateInstantiator; public EntityTemplateFactory() : this(CreateEntityTemplateInstance, /* trackChanges */ true) { } // for unit testing internal EntityTemplateFactory(Func templateInstantiator, VirtualPathProvider vpp) : this(templateInstantiator, /* trackChanges */ false) { _factory.VirtualPathProvider = vpp; } private EntityTemplateFactory(Func templateInstantiator, bool trackChanges) { _factory = new TemplateFactory("EntityTemplates", trackChanges); _templateInstantiator = templateInstantiator; } internal string TemplateFolderVirtualPath { get { return _factory.TemplateFolderVirtualPath; } set { _factory.TemplateFolderVirtualPath = value; } } private static EntityTemplateUserControl CreateEntityTemplateInstance(string path) { return (EntityTemplateUserControl)BuildManager.CreateInstanceFromVirtualPath( path, typeof(EntityTemplateUserControl)); } public virtual EntityTemplateUserControl CreateEntityTemplate(MetaTable table, DataBoundControlMode mode, string uiHint) { if (table == null) { throw new ArgumentNullException("table"); } string entityTemplatePath = GetEntityTemplateVirtualPathWithCaching(table, mode, uiHint); if (entityTemplatePath == null) { return null; } return _templateInstantiator(entityTemplatePath); } private string GetEntityTemplateVirtualPathWithCaching(MetaTable table, DataBoundControlMode mode, string uiHint) { long cacheKey = Misc.CombineHashCodes(table, mode, uiHint); return _factory.GetTemplatePath(cacheKey, delegate() { return GetEntityTemplateVirtualPath(table, mode, uiHint); }); } public virtual string GetEntityTemplateVirtualPath(MetaTable table, DataBoundControlMode mode, string uiHint) { if (table == null) { throw new ArgumentNullException("table"); } // Fallback order is as follows (where CustomProducts is the uiHint) // CustomProducts_Insert // CustomProducts_Edit // Products_Insert // Products_Edit // Default_Insert // Default_Edit // CustomProducts_ReadOnly // Products_ReadOnly // Default_ReadOnly // // If nothing matches null is returned return GetVirtualPathFallback(table, mode, uiHint, DataBoundControlMode.Edit) ?? GetVirtualPathFallback(table, DataBoundControlMode.ReadOnly, uiHint, DataBoundControlMode.ReadOnly); } private string GetVirtualPathFallback(MetaTable table, DataBoundControlMode mode, string uiHint, DataBoundControlMode minModeToFallBack) { if (mode < minModeToFallBack) { return null; } // the strategy is to go over each candidate name and try to find an existing template for // each mode between 'mode' and 'minModeToFallBack' // note that GetVirtualPathForMode will return null for empty names (e.g. when the uiHint is not specified) string[] fallbackNames = new string[] { uiHint, table.Name, s_defaultTemplateName }; foreach (var name in fallbackNames) { for (var currentMode = mode; currentMode >= minModeToFallBack; currentMode--) { string virtualPath = GetVirtualPathForMode(name, currentMode); if (virtualPath != null) { return virtualPath; } } } return null; } private string GetVirtualPathForMode(string candidateName, DataBoundControlMode mode) { if (String.IsNullOrEmpty(candidateName)) { return null; } else { string templatePath = BuildEntityTemplateVirtualPath(candidateName, mode); return _factory.FileExists(templatePath) ? templatePath : null; } } public virtual string BuildEntityTemplateVirtualPath(string templateName, DataBoundControlMode mode) { if (templateName == null) { throw new ArgumentNullException("templateName"); } string modeString = mode == DataBoundControlMode.ReadOnly ? String.Empty : "_" + mode.ToString(); return String.Format(CultureInfo.InvariantCulture, this.TemplateFolderVirtualPath + "{0}{1}.ascx", templateName, modeString); } internal void Initialize(MetaModel model) { Debug.Assert(model != null); _factory.Model = model; } } } // 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
- TrackingMemoryStreamFactory.cs
- AccessViolationException.cs
- CardSpacePolicyElement.cs
- NativeMethods.cs
- ProjectionPathSegment.cs
- SqlDataSourceQueryConverter.cs
- ByteStreamMessage.cs
- ButtonField.cs
- SystemMulticastIPAddressInformation.cs
- SendKeys.cs
- OperationCanceledException.cs
- ListViewUpdateEventArgs.cs
- UnsettableComboBox.cs
- ProvidersHelper.cs
- ToolboxService.cs
- hresults.cs
- ArraySet.cs
- XmlSchemaProviderAttribute.cs
- EFAssociationProvider.cs
- ProfessionalColors.cs
- XPathSelectionIterator.cs
- InvokeProviderWrapper.cs
- OLEDB_Util.cs
- DesignerSerializationOptionsAttribute.cs
- TableParagraph.cs
- AesCryptoServiceProvider.cs
- ContextMenuStripActionList.cs
- CodeDelegateInvokeExpression.cs
- IssuedTokenServiceCredential.cs
- Serializer.cs
- SQLBoolean.cs
- ToolStripSplitButton.cs
- Message.cs
- DocumentOrderComparer.cs
- ComponentEditorPage.cs
- SelectionWordBreaker.cs
- PersonalizationDictionary.cs
- TextMetrics.cs
- NonDualMessageSecurityOverHttp.cs
- MediaTimeline.cs
- AncillaryOps.cs
- HtmlControl.cs
- Profiler.cs
- LinkDesigner.cs
- SafeHGlobalHandleCritical.cs
- SchemaImporterExtension.cs
- SuppressMessageAttribute.cs
- BitmapFrameDecode.cs
- ZipFileInfo.cs
- WorkflowServiceHost.cs
- ProfileParameter.cs
- CommandEventArgs.cs
- FrameworkObject.cs
- ColorAnimationBase.cs
- BinaryExpression.cs
- RoleGroup.cs
- ApplicationSettingsBase.cs
- returneventsaver.cs
- EntryPointNotFoundException.cs
- LayoutTable.cs
- SectionVisual.cs
- DataServiceContext.cs
- FileSystemEventArgs.cs
- ReceiveCompletedEventArgs.cs
- ObjectIDGenerator.cs
- backend.cs
- sqlcontext.cs
- ConstrainedDataObject.cs
- JavaScriptSerializer.cs
- ActivityExecutorDelegateInfo.cs
- SimpleBitVector32.cs
- AnimationClock.cs
- TaskForm.cs
- QueryableDataSourceView.cs
- RegexCode.cs
- TemplateParser.cs
- XmlIgnoreAttribute.cs
- ButtonStandardAdapter.cs
- storepermission.cs
- LongTypeConverter.cs
- SecurityContextTokenCache.cs
- CryptoKeySecurity.cs
- MenuCommands.cs
- HttpCacheVary.cs
- IdnMapping.cs
- WebPartActionVerb.cs
- ZipIORawDataFileBlock.cs
- ModelPropertyDescriptor.cs
- ReferenceAssemblyAttribute.cs
- ToolStripComboBox.cs
- Brushes.cs
- Label.cs
- XmlToDatasetMap.cs
- DataGridTemplateColumn.cs
- ExpressionPrefixAttribute.cs
- PrincipalPermission.cs
- IconConverter.cs
- TextSpanModifier.cs
- XmlReflectionMember.cs
- Screen.cs