SolutionFramework /SolutionFramework/Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.10.0/Microsoft/VisualStudio/ServiceModel/DomainServices/Tools/BusinessLogicContext.cs

Language C# Lines 401
MD5 Hash 3386c59f26de19f19526adad70778b7c Estimated Cost $9,539 (why?)
Repository https://SolutionFramework.svn.codeplex.com/svn View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
{
    using System;
    using System.CodeDom;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Reflection;
    using System.ServiceModel.DomainServices.Hosting;
    using System.Linq;

    internal class BusinessLogicContext
    {
        private Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.ContextData _contextData;
        private Type _contextType;
        private List<BusinessLogicEntity> _entities;
        private string _name;
        private static int uniqueContextID;
        private const string UserDataHelperMethods = "HelperMethods";

        public BusinessLogicContext(Type contextType, string name)
        {
            this._contextType = contextType;
            this._name = name;
        }

        protected virtual bool CanGeneratePropertyOfType(Type type)
        {
            type = TypeUtilities.GetElementType(type);
            if (TypeUtilities.IsPredefinedType(type))
            {
                return true;
            }
            foreach (BusinessLogicEntity entity in this.Entities)
            {
                if (entity.ClrType == type)
                {
                    return true;
                }
            }
            return false;
        }

        protected virtual CodeTypeDeclaration CreateBusinessLogicClass(CodeGenContext codeGenContext, CodeNamespace codeNamespace, string className)
        {
            CodeTypeDeclaration declaration = CodeGenUtilities.CreateTypeDeclaration(className, codeNamespace.Name);
            declaration.BaseTypes.Add(BusinessLogicClassConstants.DomainServiceTypeName);
            return declaration;
        }

        protected virtual IEnumerable<BusinessLogicEntity> CreateEntities()
        {
            return new BusinessLogicEntity[0];
        }

        protected virtual bool GenerateAdditionalMetadataClasses(CodeGenContext codeGenContext, string optionalSuffix, BusinessLogicEntity entity)
        {
            return false;
        }

        protected void GenerateBusinessLogicClass(CodeGenContext codeGenContext, string className, string namespaceName)
        {
            string str;
            if (codeGenContext == null)
            {
                throw new ArgumentNullException("codeGenContext");
            }
            if (string.IsNullOrEmpty(className))
            {
                throw new ArgumentNullException("className");
            }
            if (string.IsNullOrEmpty(namespaceName))
            {
                throw new ArgumentNullException("namespaceName");
            }
            CodeNamespace orGenNamespace = codeGenContext.GetOrGenNamespace(namespaceName);
            CodeTypeDeclaration declaration = this.CreateBusinessLogicClass(codeGenContext, orGenNamespace, className);
            orGenNamespace.Types.Add(declaration);
            if (this.ContextType == null)
            {
                str = Resources.BusinessLogicClass_Class_Remarks_Empty;
            }
            else
            {
                str = string.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_Class_Remarks, new object[] { this.ContextType.Name });
            }
            declaration.Comments.Add(new CodeCommentStatement(str, false));
            if (this.ContextType != null)
            {
                str = codeGenContext.IsCSharp ? Resources.BusinessLogicClass_RequiresAuthentication_CSharp : Resources.BusinessLogicClass_RequiresAuthentication_VB;
                declaration.Comments.Add(new CodeCommentStatement(str, false));
            }
            if (this.IsClientAccessEnabled)
            {
                CodeAttributeDeclaration declaration2 = CodeGenUtilities.CreateAttributeDeclaration(BusinessLogicClassConstants.EnableClientAccessAttributeTypeName);
                declaration.CustomAttributes.Add(declaration2);
            }
            else
            {
                declaration.Comments.Add(new CodeCommentStatement(Resources.BusinessLogicClass_EnableClientAccess_Comment));
            }
            foreach (BusinessLogicEntity entity in from e in this.Entities
                orderby e.Name
                select e)
            {
                if (entity.IsIncluded)
                {
                    CodeGenUtilities.AddImportIfNeeded(orGenNamespace, entity.ClrType.Namespace);
                    this.GenerateEntityDomainOperationEntries(codeGenContext, declaration, entity);
                }
            }
            Dictionary<string, CodeTypeMember> helperMemberDictionary = GetHelperMemberDictionary(declaration);
            foreach (string str2 in from s in helperMemberDictionary.Keys
                orderby s
                select s)
            {
                declaration.Members.Add(helperMemberDictionary[str2]);
            }
            if (this.IsODataEndpointEnabled)
            {
                codeGenContext.AddReference(typeof(ODataEndpointFactory).Assembly.FullName);
            }
        }

        public GeneratedCode GenerateBusinessLogicClass(string language, string className, string namespaceName, string rootNamespace)
        {
            using (CodeGenContext context = new CodeGenContext(language, rootNamespace))
            {
                this.GenerateBusinessLogicClass(context, className, namespaceName);
                return context.GenerateCode();
            }
        }

        protected virtual void GenerateDeleteMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
        }

        internal void GenerateEntityDomainOperationEntries(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            CodeMemberMethod method = this.GenerateSelectMethod(codeGenContext, businessLogicClass, entity);
            if ((method != null) && this.IsODataEndpointEnabled)
            {
                CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(new CodeTypeReference("Query"), new CodeAttributeArgument[] { new CodeAttributeArgument("IsDefault", new CodePrimitiveExpression(true)) });
                method.CustomAttributes.Add(declaration);
            }
            if (entity.IsEditable)
            {
                this.GenerateInsertMethod(codeGenContext, businessLogicClass, entity);
                this.GenerateUpdateMethod(codeGenContext, businessLogicClass, entity);
                this.GenerateDeleteMethod(codeGenContext, businessLogicClass, entity);
            }
        }

        internal static void GenerateHelperMemberIfNecessary(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, string helperMemberName, Func<CodeTypeMember> generatorCallback)
        {
            Dictionary<string, CodeTypeMember> helperMemberDictionary = GetHelperMemberDictionary(businessLogicClass);
            if (!helperMemberDictionary.ContainsKey(helperMemberName))
            {
                CodeTypeMember member = generatorCallback();
                if (member != null)
                {
                    helperMemberDictionary[helperMemberName] = member;
                }
            }
        }

        protected virtual void GenerateInsertMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
        }

        internal bool GenerateMetadataClass(CodeGenContext codeGenContext, string optionalSuffix, Type type)
        {
            if (TypeUtilities.GetAssociatedMetadataType(type) != null)
            {
                return false;
            }
            string name = type.Name;
            string namespaceName = type.Namespace;
            bool flag = !string.IsNullOrEmpty(optionalSuffix);
            if (flag)
            {
                name = name + optionalSuffix;
                namespaceName = namespaceName + optionalSuffix;
            }
            CodeNamespace orGenNamespace = codeGenContext.GetOrGenNamespace(namespaceName);
            if (flag)
            {
                CodeGenUtilities.AddImportIfNeeded(orGenNamespace, type.Namespace);
            }
            string typeName = name + "Metadata";
            string str4 = name + "." + typeName;
            CodeTypeDeclaration declaration = null;
            declaration = CodeGenUtilities.CreateTypeDeclaration(name, namespaceName);
            declaration.IsPartial = true;
            declaration.TypeAttributes = TypeAttributes.Public;
            declaration.Comments.Add(new CodeCommentStatement(string.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_Entity_Partial_Class_Remarks, new object[] { typeName, name }), false));
            CodeAttributeDeclaration declaration2 = CodeGenUtilities.CreateAttributeDeclaration(BusinessLogicClassConstants.MetadataTypeAttributeTypeName);
            CodeAttributeArgument argument = new CodeAttributeArgument(new CodeTypeOfExpression(str4));
            declaration2.Arguments.Add(argument);
            declaration.CustomAttributes.Add(declaration2);
            CodeTypeDeclaration buddyClass = CodeGenUtilities.CreateTypeDeclaration(typeName, namespaceName);
            buddyClass.TypeAttributes = TypeAttributes.Sealed | TypeAttributes.NestedAssembly;
            bool flag2 = false;
            buddyClass.Comments.Add(new CodeCommentStatement(string.Format(CultureInfo.CurrentCulture, Resources.Buddy_Class_Remarks, new object[] { type.Name })));
            string text = codeGenContext.IsCSharp ? Resources.Buddy_Class_Remarks_CSharp : Resources.Buddy_Class_Remarks_VB;
            buddyClass.Comments.Add(new CodeCommentStatement(text, false));
            CodeConstructor constructor = new CodeConstructor {
                Attributes = MemberAttributes.Private
            };
            constructor.Comments.Add(new CodeCommentStatement(Resources.BusinessLogicClass_Private_Ctor_Comment));
            buddyClass.Members.Add(constructor);
            foreach (PropertyInfo info in from p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                orderby p.Name
                select p)
            {
                Type propertyType = info.PropertyType;
                if ((propertyType.IsVisible && (info.GetGetMethod() != null)) && this.CanGeneratePropertyOfType(propertyType))
                {
                    CodeGenUtilities.AddImportIfNeeded(orGenNamespace, info.PropertyType.Namespace);
                    CodeSnippetTypeMember member = CodeGenUtilities.CreateAutomaticPropertyDeclaration(codeGenContext, buddyClass, info, !string.IsNullOrEmpty(orGenNamespace.Name));
                    buddyClass.Members.Add(member);
                    flag2 = true;
                }
            }
            if (flag2)
            {
                orGenNamespace.Types.Add(declaration);
                declaration.Members.Add(buddyClass);
            }
            return flag2;
        }

        protected bool GenerateMetadataClasses(CodeGenContext codeGenContext, string optionalSuffix)
        {
            bool flag = false;
            if (this.NeedToGenerateMetadataClasses)
            {
                foreach (BusinessLogicEntity entity in from e in this.Entities
                    orderby e.Name
                    select e)
                {
                    if (entity.IsIncluded)
                    {
                        flag |= this.GenerateMetadataClass(codeGenContext, optionalSuffix, entity.ClrType);
                        flag |= this.GenerateAdditionalMetadataClasses(codeGenContext, optionalSuffix, entity);
                    }
                }
            }
            return flag;
        }

        public GeneratedCode GenerateMetadataClasses(string language, string rootNamespace, string optionalSuffix)
        {
            using (CodeGenContext context = new CodeGenContext(language, rootNamespace))
            {
                if (this.GenerateMetadataClasses(context, optionalSuffix))
                {
                    return context.GenerateCode();
                }
            }
            return new GeneratedCode();
        }

        protected virtual CodeMemberMethod GenerateSelectMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            return null;
        }

        protected virtual void GenerateUpdateMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
        }

        protected static Dictionary<string, CodeTypeMember> GetHelperMemberDictionary(CodeTypeDeclaration businessLogicClass)
        {
            Dictionary<string, CodeTypeMember> dictionary = null;
            if (businessLogicClass.UserData.Contains("HelperMethods"))
            {
                return (businessLogicClass.UserData["HelperMethods"] as Dictionary<string, CodeTypeMember>);
            }
            dictionary = new Dictionary<string, CodeTypeMember>();
            businessLogicClass.UserData["HelperMethods"] = dictionary;
            return dictionary;
        }

        public Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.ContextData ContextData
        {
            get
            {
                if (this._contextData == null)
                {
                    Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.ContextData data = new Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.ContextData {
                        Name = this.NameAndDataAccessLayerName,
                        IsClientAccessEnabled = true,
                        ID = uniqueContextID++
                    };
                    this._contextData = data;
                }
                return this._contextData;
            }
            set
            {
                this._contextData = value;
            }
        }

        public Type ContextType
        {
            get
            {
                return this._contextType;
            }
        }

        public virtual string DataAccessLayerName
        {
            get
            {
                return null;
            }
        }

        public IEnumerable<BusinessLogicEntity> Entities
        {
            get
            {
                if (this._entities == null)
                {
                    try
                    {
                        this._entities = new List<BusinessLogicEntity>(this.CreateEntities());
                    }
                    catch (Exception exception)
                    {
                        if (exception is OutOfMemoryException)
                        {
                            throw;
                        }
                        this._entities = new List<BusinessLogicEntity>();
                    }
                    this._entities.Sort((Comparison<BusinessLogicEntity>) ((x, y) => string.Compare(x.Name, y.Name, StringComparison.OrdinalIgnoreCase)));
                }
                return this._entities;
            }
        }

        public IEnumerable<EntityData> EntityDataItems
        {
            get
            {
                return (from ble in this.Entities select ble.EntityData);
            }
        }

        public bool IsClientAccessEnabled
        {
            get
            {
                return this.ContextData.IsClientAccessEnabled;
            }
        }

        public bool IsODataEndpointEnabled
        {
            get
            {
                return this.ContextData.IsODataEndpointEnabled;
            }
        }

        public string Name
        {
            get
            {
                return this.ContextData.Name;
            }
        }

        public string NameAndDataAccessLayerName
        {
            get
            {
                string str = this._name;
                string dataAccessLayerName = this.DataAccessLayerName;
                if (!string.IsNullOrEmpty(dataAccessLayerName))
                {
                    return string.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_Name_And_Technology, new object[] { str, dataAccessLayerName });
                }
                return str;
            }
        }

        internal virtual bool NeedToGenerateMetadataClasses
        {
            get
            {
                return true;
            }
        }
    }
}
Back to Top