protected virtual AstoriaHashTable<ResourceType, Type> CreateResourceTypeToClientClrTypeMap(Assembly currentAssembly) { AstoriaHashTable<ResourceType, Type> typeMap = new AstoriaHashTable<ResourceType, Type>(); foreach (ResourceType t in this.ServiceContainer.ResourceTypes) { if (!typeMap.ContainsKey(t)) { Type clientType = currentAssembly.GetType(this.ContextNamespace + "ClientLTS." + t.Name); if (clientType == null) throw new TestFailedException("Unable to find client Clr Type:" + this.ContextNamespace + "." + t.Name + " in Assembly:" + currentAssembly); typeMap.Add(t, clientType); } } return typeMap; }
protected internal virtual void CreateLocalAssembly() { string path = Path.Combine(Environment.CurrentDirectory, this.Name + "_" + this.DataLayerProviderKind); string pathTemplate = path + "_{0}.dll"; path += ".dll"; int count = 0; while (File.Exists(path)) { try { File.Delete(path); } catch (Exception) { path = string.Format(pathTemplate, count++); } } _assemblyPath = path; this.CompileCodeFiles(ObjectLayerFileNames.ToArray(), new string[] { }, _assemblyPath); String assemblyName = Path.GetFileNameWithoutExtension(_assemblyPath); Assembly assembly = Assembly.Load(assemblyName); _underlyingContextType = assembly.GetType(this.ContextNamespace + "." + this.ContextTypeName, false); //Assembly ltsAssembly = TypeUtil.GetAssembly(assembly); _resourceTypeToWorkspaceTypeList = CreateResourceTypeToUnderlyingClrTypeMap(assembly, this.ContextNamespace); SetResourceTypesToClrTypes(assembly, this.ContextNamespace, _resourceTypeToWorkspaceTypeList, false); }
public void SetResourceTypesToClrTypes(Assembly assembly, string clrTypeNamespace, AstoriaHashTable<ResourceType, Type> typeDictionary, bool fillClientType) { // Make sure ClrType is set for all types in the assembly and servicecontainer foreach (ResourceType rt in this.ServiceContainer.ResourceTypes) { if (!typeDictionary.ContainsKey(rt)) AstoriaTestLog.FailAndThrow(String.Format("ResourceType '{0}' does not have a corresponding CLR type in the type dictionary", rt.Name)); if (typeDictionary[rt] == null) continue; if (fillClientType) rt.ClientClrType = typeDictionary[rt]; else rt.ClrType = typeDictionary[rt]; // check to see if the base type will not be returned by an underlying provider query if (rt.BaseType != null) { ResourceType baseType = rt.BaseType as ResourceType; // if the basetype is not a resource type, or no entity set returns it, then we should use the current type as its Clr type. // note that this would be incorrect in a MEST scenario with inheritance if (baseType == null || !this.ServiceContainer.ResourceContainers.Any(rc => rc.ResourceTypes.Contains(baseType))) { if (fillClientType) rt.BaseType.ClientClrType = typeDictionary[rt]; else rt.BaseType.ClrType = typeDictionary[rt]; } } foreach (ResourceProperty navProperty in rt.Properties.Cast<ResourceProperty>().Where(rp => rp.IsNavigation)) { if (navProperty.Type.ClrType != null) { Type navPropertyType = null; if (navProperty.Type is CollectionType) { navPropertyType = typeof(System.Collections.ObjectModel.Collection<>).MakeGenericType( typeDictionary[(ResourceType)((CollectionType)navProperty.Type).SubType]); } else { navPropertyType = typeDictionary[(ResourceType)navProperty.Type]; } navProperty.Type.ClientClrType = navPropertyType; } } foreach (ResourceProperty navProperty in rt.Properties.Cast<ResourceProperty>().Where(rp => rp.IsNavigation)) { if (navProperty.Type.ClrType != null) { Type navPropertyType = null; if (navProperty.Type is CollectionType) { navPropertyType = typeof(System.Collections.ObjectModel.Collection<>).MakeGenericType( typeDictionary[(ResourceType)((CollectionType)navProperty.Type).SubType]); } else { navPropertyType = typeDictionary[(ResourceType)navProperty.Type]; } navProperty.Type.ClientClrType = navPropertyType; } } foreach (ResourceProperty complexProperty in rt.Properties.Cast<ResourceProperty>().Where(rp => rp.IsComplexType)) { if (complexProperty.Type.ClrType != null) { complexProperty.Type.ClientClrType = Type.GetType(complexProperty.Type.ClrType.FullName.Replace(this.ContextNamespace, this.ContextNamespace + "Client")); } } } foreach (ResourceType rt in this.ServiceContainer.ResourceTypes) { foreach (ResourceProperty rp in rt.Properties) { if ((rp.IsNavigation || rp.IsComplexType) && (rp.Type.ClrType == null || fillClientType)) { Type edmType = assembly.GetType(clrTypeNamespace + "." + rp.Type.ToString()); if (edmType != null) { if (fillClientType) rp.Type.ClientClrType = edmType; else rp.Type.ClrType = edmType; } if (rp.Type is CollectionType) { CollectionType collectionType = (CollectionType)rp.Type; NodeType subType = collectionType.SubType; if (subType.ClrType == null) { edmType = assembly.GetType(clrTypeNamespace + "." + subType.Name); if (fillClientType) subType.ClientClrType = edmType; else subType.ClrType = edmType; } } } } } }
//Constructor public Workspace(DataLayerProviderKind kind, String name) : base(name) { AstoriaTestLog.WriteLineIgnore("Constructing workspace of type '" + this.GetType() + "' with name '" + this.Name + "'."); OnCreateNewWorkspaceEvent(new NewWorkspaceEventArgs(this)); DataLayerProviderKind = kind; _serviceClassName = this.Name + "Service"; _resourceTypeToWorkspaceTypeList = new AstoriaHashTable<ResourceType, Type>(); _resourceTypeToClientTypeList = new AstoriaHashTable<ResourceType, Type>(); Settings = new WorkspaceSettings(this); BeforeServiceCreation = new List<Action>(); AfterServiceCreation = new List<Action>(); BeforeServiceCreation.Add(this.InferAssociations); if (AstoriaTestProperties.UseOpenTypes) BeforeServiceCreation.Add(() => OpenTypesUtil.SetupDefaultOpenTypeAttributes(this)); BeforeServiceCreation.Add(this.GenerateCallOrderInterceptorCode); BeforeServiceCreation.Add(this.GenerateCustomInterceptors); BeforeServiceCreation.Add(this.PopulateHostSourceFolder); // these can happen before or after, but lets do them before in case other events need them to be done already BeforeServiceCreation.Add(this.CreateLocalAssembly); BeforeServiceCreation.Add(this.PopulateClientTypes); this.DataGenerator = null; this.RequiredFrameworkSources = new List<string>() { "TestDataWebService.cs", "APICallLog.cs", "ProviderWrappers.cs" }; this.ServiceModifications = new ServiceModifications(this); }
protected virtual AstoriaHashTable<ResourceType, Type> CreateResourceTypeToUnderlyingClrTypeMap(Assembly myAssembly, string clrTypeNamespace) { AstoriaHashTable<ResourceType, Type> typeMap = new AstoriaHashTable<ResourceType, Type>(); foreach (ResourceType t in this.ServiceContainer.ResourceTypes) { if (!typeMap.ContainsKey(t)) { Type entityType = myAssembly.GetType(clrTypeNamespace + "." + t.Name); if (entityType == null) AstoriaTestLog.WriteLineIgnore("Unable to find Clr Type:" + clrTypeNamespace + "." + t.Name + " in Assembly:" + myAssembly); //throw new TestFailedException("Unable to find Clr Type:" + clrTypeNamespace + "." + t.Name + " in Assembly:" + myAssembly); typeMap.Add(t, entityType); } } return typeMap; }