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

Language C# Lines 262
MD5 Hash 4398a35f62043f3bea49c3c4f3ea1548 Estimated Cost $6,170 (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
namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
{
    using Microsoft.Win32;
    using System;
    using System.CodeDom;
    using System.Collections.Generic;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.IO;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Linq;

    internal class LinqToSqlContext : BusinessLogicContext
    {
        private System.Data.Linq.DataContext _dataContext;
        private System.Data.Linq.Mapping.MetaModel _metaModel;
        private const string AssemblyPathKeyValueName = "AssemblyPath";
        private const string EnableDataContextKeyValueName = "EnableDataContext";
        private static string linqToSqlDomainServiceAssemblyPath;
        private const string ToolkitDomainServiceWizardRegKey = @"SOFTWARE\Microsoft\WCFRIAServices\v1.0\Toolkit\DomainServiceWizard";
        private static bool EnableDataContextTypes_TestOverride_k__BackingField;

        public LinqToSqlContext(Type contextType) : base(contextType, contextType.Name)
        {
        }

        protected override CodeTypeDeclaration CreateBusinessLogicClass(CodeGenContext codeGenContext, CodeNamespace codeNamespace, string className)
        {
            if (linqToSqlDomainServiceAssemblyPath == null)
            {
                return null;
            }
            foreach (string str in BusinessLogicClassConstants.LinqToSqlImports)
            {
                codeNamespace.Imports.Add(new CodeNamespaceImport(str));
            }
            if (base.ContextType.Namespace != codeNamespace.Name)
            {
                codeNamespace.Imports.Add(new CodeNamespaceImport(base.ContextType.Namespace));
            }
            codeGenContext.AddReference(typeof(System.Data.Linq.DataContext).Assembly.FullName);
            codeGenContext.AddReference(linqToSqlDomainServiceAssemblyPath);
            CodeTypeDeclaration declaration = CodeGenUtilities.CreateTypeDeclaration(className, codeNamespace.Name);
            CodeTypeReference reference = new CodeTypeReference(BusinessLogicClassConstants.LinqToSqlDomainServiceTypeName, new CodeTypeReference[] { new CodeTypeReference(base.ContextType.Name) });
            declaration.BaseTypes.Add(reference);
            return declaration;
        }

        protected override IEnumerable<BusinessLogicEntity> CreateEntities()
        {
            IEnumerable<MetaTable> enumerable = from t in this.MetaModel.GetTables()
                where t.RowType.IsEntity
                select t;
            List<BusinessLogicEntity> list = new List<BusinessLogicEntity>();
            foreach (MetaTable table in enumerable)
            {
                MetaType rowType = table.RowType;
                list.Add(new LinqToSqlEntity(this, rowType));
            }
            return list;
        }

        protected override void GenerateDeleteMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            string name = CodeGenUtilities.MakeLegalParameterName(entity.Name);
            CodeMemberMethod method = new CodeMemberMethod();
            businessLogicClass.Members.Add(method);
            LinqToSqlEntity entity2 = (LinqToSqlEntity) entity;
            method.Name = "Delete" + CodeGenUtilities.MakeLegalEntityName(entity.Name);
            method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            method.Parameters.Add(new CodeParameterDeclarationExpression(entity.ClrType.Name, name));
            CodeExpression targetObject = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
            CodeExpression expression2 = new CodePropertyReferenceExpression(targetObject, entity2.TablePropertyName);
            CodeMethodInvokeExpression expression3 = new CodeMethodInvokeExpression(expression2, "Attach", new CodeExpression[] { new CodeArgumentReferenceExpression(name) });
            method.Statements.Add(expression3);
            CodeMethodInvokeExpression expression4 = new CodeMethodInvokeExpression(expression2, "DeleteOnSubmit", new CodeExpression[] { new CodeArgumentReferenceExpression(name) });
            method.Statements.Add(expression4);
        }

        protected override void GenerateInsertMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            CodeMemberMethod method = new CodeMemberMethod();
            businessLogicClass.Members.Add(method);
            string name = CodeGenUtilities.MakeLegalParameterName(entity.Name);
            LinqToSqlEntity entity2 = (LinqToSqlEntity) entity;
            method.Name = "Insert" + CodeGenUtilities.MakeLegalEntityName(entity.Name);
            method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            method.Parameters.Add(new CodeParameterDeclarationExpression(entity.ClrType.Name, name));
            CodeExpression targetObject = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
            CodeExpression expression2 = new CodePropertyReferenceExpression(targetObject, entity2.TablePropertyName);
            CodeMethodInvokeExpression expression3 = new CodeMethodInvokeExpression(expression2, "InsertOnSubmit", new CodeExpression[] { new CodeArgumentReferenceExpression(name) });
            method.Statements.Add(expression3);
        }

        protected override CodeMemberMethod GenerateSelectMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            CodeMemberMethod method = null;
            LinqToSqlEntity entity2 = entity as LinqToSqlEntity;
            if ((entity2 != null) && (entity2.TablePropertyName != null))
            {
                method = new CodeMemberMethod();
                businessLogicClass.Members.Add(method);
                method.Comments.Add(new CodeCommentStatement(Resources.BusinessLogicClass_Query_Method_Remarks, false));
                method.Name = "Get" + CodeGenUtilities.MakeLegalEntityName(entity2.TablePropertyName);
                method.ReturnType = new CodeTypeReference("IQueryable", new CodeTypeReference[] { new CodeTypeReference(entity.Name) });
                method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
                CodeExpression targetObject = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
                CodeExpression expression = new CodePropertyReferenceExpression(targetObject, entity2.TablePropertyName);
                CodeMethodReturnStatement statement = new CodeMethodReturnStatement(expression);
                method.Statements.Add(statement);
            }
            return method;
        }

        protected override void GenerateUpdateMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
        {
            string name = "current" + entity.ClrType.Name;
            CodeMemberMethod method = new CodeMemberMethod();
            businessLogicClass.Members.Add(method);
            LinqToSqlEntity entity2 = (LinqToSqlEntity) entity;
            method.Name = "Update" + CodeGenUtilities.MakeLegalEntityName(entity.Name);
            method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            method.Parameters.Add(new CodeParameterDeclarationExpression(entity.ClrType.Name, name));
            if (!entity2.HasTimestampMember)
            {
                CodeExpression targetObject = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "ChangeSet");
                CodeMethodReferenceExpression expression2 = new CodeMethodReferenceExpression(targetObject, "GetOriginal");
                CodeMethodInvokeExpression expression3 = new CodeMethodInvokeExpression(expression2, new CodeExpression[] { new CodeArgumentReferenceExpression(name) });
                CodeExpression expression4 = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
                CodeExpression expression5 = new CodePropertyReferenceExpression(expression4, entity2.TablePropertyName);
                CodeMethodInvokeExpression expression6 = new CodeMethodInvokeExpression(expression5, "Attach", new CodeExpression[] { new CodeArgumentReferenceExpression(name), expression3 });
                method.Statements.Add(expression6);
            }
            else
            {
                CodeExpression expression7 = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
                CodeExpression expression8 = new CodePropertyReferenceExpression(expression7, entity2.TablePropertyName);
                CodeMethodInvokeExpression expression9 = new CodeMethodInvokeExpression(expression8, "Attach", new CodeExpression[] { new CodeArgumentReferenceExpression(name), new CodePrimitiveExpression(true) });
                method.Statements.Add(expression9);
            }
        }

        internal static void OverrideAssemblyPath(string assemblyPath)
        {
            EnableDataContextTypes_TestOverride = !string.IsNullOrEmpty(assemblyPath);
            linqToSqlDomainServiceAssemblyPath = assemblyPath;
        }

        public override string DataAccessLayerName
        {
            get
            {
                return Resources.BusinessLogicClass_LinqToSql;
            }
        }

        private System.Data.Linq.DataContext DataContext
        {
            get
            {
                if (this._dataContext == null)
                {
                    try
                    {
                        this._dataContext = (System.Data.Linq.DataContext) Activator.CreateInstance(base.ContextType, new object[] { string.Empty });
                    }
                    catch (TargetInvocationException exception)
                    {
                        if (exception.InnerException != null)
                        {
                            throw exception.InnerException;
                        }
                        throw;
                    }
                }
                return this._dataContext;
            }
        }

        public static bool EnableDataContextTypes
        {
            get
            {
                if (EnableDataContextTypes_TestOverride)
                {
                    return true;
                }
                linqToSqlDomainServiceAssemblyPath = null;
                RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\WCFRIAServices\v1.0\Toolkit\DomainServiceWizard");
                if (key != null)
                {
                    object obj2 = key.GetValue("EnableDataContext", string.Empty);
                    int result = 0;
                    if (((obj2 != null) && int.TryParse(obj2.ToString(), out result)) && (result == 1))
                    {
                        string path = key.GetValue("AssemblyPath", null) as string;
                        if (File.Exists(path))
                        {
                            try
                            {
                                Assembly.LoadFrom(path);
                                linqToSqlDomainServiceAssemblyPath = path;
                            }
                            catch (FileNotFoundException)
                            {
                            }
                            catch (BadImageFormatException)
                            {
                            }
                            catch (FileLoadException)
                            {
                            }
                        }
                    }
                    key.Close();
                }
                return (linqToSqlDomainServiceAssemblyPath != null);
            }
        }

        private static bool EnableDataContextTypes_TestOverride
        {
            [CompilerGenerated]
            get
            {
                return EnableDataContextTypes_TestOverride_k__BackingField;
            }
            [CompilerGenerated]
            set
            {
                EnableDataContextTypes_TestOverride_k__BackingField = value;
            }
        }

        internal static string LinqToSqlDomainServiceAssemblyPath
        {
            get
            {
                if (!EnableDataContextTypes)
                {
                    return null;
                }
                return linqToSqlDomainServiceAssemblyPath;
            }
        }

        private System.Data.Linq.Mapping.MetaModel MetaModel
        {
            get
            {
                if (this._metaModel == null)
                {
                    this._metaModel = this.DataContext.Mapping;
                }
                return this._metaModel;
            }
        }
    }
}
Back to Top