-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
V3: Option to have lazy DSC<> initialization. Option to exclude static model #46
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,67 @@ namespace Microsoft.OData.ConnectedService.CodeGeneration | |
{ | ||
internal class V3CodeGenDescriptor : BaseCodeGenDescriptor | ||
{ | ||
const string loadServiceModelAssignment = "this.Format.LoadServiceModel = GeneratedEdmModel.GetInstance;"; | ||
const string dynamicModelLoader = | ||
@"private abstract class RuntimeEdmModel | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(""System.Data.Services.Design"", ""1.0.0"")] | ||
private static global::System.Collections.Generic.Dictionary<string, global::Microsoft.Data.Edm.IEdmModel> models = new global::System.Collections.Generic.Dictionary<string, global::Microsoft.Data.Edm.IEdmModel>(global::System.StringComparer.OrdinalIgnoreCase); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please split into multiple lines for readability. |
||
|
||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(""System.Data.Services.Design"", ""1.0.0"")] | ||
private static object modelsCacheLock = new object(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make this lock readonly as well. |
||
|
||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(""System.Data.Services.Design"", ""1.0.0"")] | ||
public static global::Microsoft.Data.Edm.IEdmModel LoadModel(global::System.Data.Services.Client.DataServiceContext context) | ||
{ | ||
var metadataUri = context.GetMetadataUri(); | ||
global::Microsoft.Data.Edm.IEdmModel model = null; | ||
if (!models.TryGetValue(metadataUri.AbsoluteUri, out model)) | ||
{ | ||
lock (modelsCacheLock) | ||
{ | ||
if (!models.TryGetValue(metadataUri.AbsoluteUri, out model)) | ||
{ | ||
var request = (global::System.Net.HttpWebRequest)global::System.Net.WebRequest.Create(metadataUri); | ||
request.Credentials = context.Credentials; | ||
|
||
using (var response = request.EndGetResponse(request.BeginGetResponse(null, null))) | ||
using (var stream = response.GetResponseStream()) | ||
using (var reader = global::System.Xml.XmlReader.Create(stream)) | ||
{ | ||
model = global::Microsoft.Data.Edm.Csdl.EdmxReader.Parse(reader); | ||
models.Add(metadataUri.AbsoluteUri, model); | ||
} | ||
} | ||
} | ||
} | ||
return model; | ||
} | ||
}"; | ||
|
||
const string collectionReplacer = | ||
@"private $1 __$2 = null; | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(""System.Data.Services.Design"", ""1.0.0"")] | ||
private $1 _$2 | ||
{ | ||
get | ||
{ | ||
if (__$2 == null && !__$2Initialized) | ||
$2 = new $1(null, global::System.Data.Services.Client.TrackingMode.None); | ||
return __$2; | ||
} | ||
set | ||
{ | ||
__$2 = value; | ||
__$2Initialized = true; | ||
} | ||
} | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute(""System.Data.Services.Design"", ""1.0.0"")] | ||
bool __$2Initialized; | ||
"; | ||
|
||
public V3CodeGenDescriptor(string metadataUri, ConnectedServiceHandlerContext context, Project project) | ||
: base(metadataUri, context, project) | ||
{ | ||
|
@@ -85,6 +146,58 @@ public async override Task AddGeneratedClientCode() | |
} | ||
} | ||
|
||
if (this.ServiceConfiguration.LazyInitializedEntityCollections || this.ServiceConfiguration.UseRuntimeModel) | ||
{ | ||
await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Cleaning proxy..."); | ||
string original = null; | ||
using (var textReader = File.OpenText(tempFile)) | ||
{ | ||
original = await textReader.ReadToEndAsync(); | ||
} | ||
string modified = null; | ||
|
||
if (this.ServiceConfiguration.LazyInitializedEntityCollections) | ||
{ | ||
string pattern = null; | ||
if (generator.UseDataServiceCollection) | ||
{ | ||
await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Proxy - injecting lazy initialization of DataServiceCollections"); | ||
pattern = @"private (.+) _(.+) = new global::System.Data.Services.Client.DataServiceCollection\<(.+)\>\(null, global::System.Data.Services.Client.TrackingMode.None\);"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please split into multiple lines for readability. |
||
} | ||
else | ||
{ | ||
await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Proxy - injecting lazy initialization of ObservableCollections"); | ||
pattern = @"private (.+) _(.+) = new global::System.Collections.ObjectModel.Collection\<(.+)\>\(\);"; | ||
} | ||
modified = Regex.Replace(original, pattern, collectionReplacer); | ||
} | ||
else | ||
modified = original; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: please encapsulate this in { }, even for one-liners to be consistent with the style standard. |
||
|
||
if (this.ServiceConfiguration.UseRuntimeModel) | ||
{ | ||
await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Proxy - removing static model"); | ||
int loadServiceModelAssignmentIndex = modified.IndexOf(loadServiceModelAssignment); | ||
if (loadServiceModelAssignmentIndex >= 0) | ||
{ | ||
modified = string.Concat(modified.Substring(0, loadServiceModelAssignmentIndex), "this.Format.LoadServiceModel = () => RuntimeEdmModel.LoadModel(this);", modified.Substring(loadServiceModelAssignmentIndex + loadServiceModelAssignment.Length)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please split into multiple lines for readability. |
||
int classStart = modified.IndexOf("private abstract class GeneratedEdmModel"); | ||
int classEnd = modified.IndexOf("return global::System.Xml.XmlReader.Create(new global::System.IO.StringReader(edmxToParse));"); | ||
if (classStart >= 0 && classEnd > 0) | ||
{ | ||
classEnd = modified.IndexOf('}', classEnd + 1); | ||
classEnd = modified.IndexOf('}', classEnd + 1) + 1; | ||
modified = string.Concat(modified.Substring(0, classStart), dynamicModelLoader, modified.Substring(classEnd)); | ||
} | ||
} | ||
} | ||
await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Proxy generation done. Writing output files..."); | ||
using (StreamWriter writer = File.CreateText(tempFile)) | ||
{ | ||
await writer.WriteAsync(modified); | ||
} | ||
} | ||
|
||
string outputFile = Path.Combine(GetReferenceFileFolder(), this.GeneratedFileNamePrefix + ".cs"); | ||
await this.Context.HandlerHelper.AddFileAsync(tempFile, outputFile); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For maintainability, can you please put a comment with a high-level description on how the class works/will be utilized?