PageRenderTime 121ms CodeModel.GetById 40ms app.highlight 54ms RepoModel.GetById 19ms app.codeStats 0ms

/Plugins/GizmoBeach.Components/DataObjects/EntityFrameworkDbContextRepositoryOrmFrameworkBase.cs

http://github.com/kahanu/CondorXE
C# | 720 lines | 634 code | 76 blank | 10 comment | 6 complexity | 683438edc6f61698504c015d59ae8dad MD5 | raw file
  1using System;
  2using Condor.Core;
  3using Condor.Core.Interfaces;
  4using MyMeta;
  5
  6namespace GizmoBeach.Components.DataObjects
  7{
  8    public class EntityFrameworkDbContextRepositoryOrmFrameworkBase : RenderBase, IORMFramework
  9    {
 10        private readonly EFCriteria _criteria;
 11
 12        protected IDataStore _dataStore;
 13        protected RequestContext _context;
 14        protected IDatabase _database;
 15        protected ProgressDialogWrapper _dialog;
 16
 17        public EntityFrameworkDbContextRepositoryOrmFrameworkBase(IDataStore dataStore, RequestContext context, EFCriteria criteria)
 18            :base(context.Zeus.Output)
 19        {
 20            this._criteria = criteria;
 21            this._dataStore = dataStore;
 22            this._context = context;
 23            this._database = context.Database;
 24            this._dialog = context.Dialog;
 25        }
 26
 27        public EntityFrameworkDbContextRepositoryOrmFrameworkBase(IDataStore dataStore, RequestContext context)
 28            :this(dataStore, context, new EFCriteria()) 
 29        {
 30
 31        }
 32
 33        #region IORMFramework Members
 34
 35        public string Name
 36        {
 37            get { return "Entity Framework DbContext"; }
 38        }
 39
 40        public void Generate()
 41        {
 42            _dialog.InitDialog(7);
 43            _dialog.Display("Processing EF DbContext Classes");
 44
 45            _context.FileList.Add("");
 46            _context.FileList.Add("Generated EF DbContext Classes:");
 47
 48            _dialog.Display("Processing EF DbContext DataContext Class");
 49            RenderDataContextClass();
 50
 51            _dialog.Display("Processing EF DbContext DataObjectFactory Class");
 52            RenderDataObjectFactory();
 53
 54            _dialog.Display("Processing EF DbContext Disposable Class");
 55            RenderDisposableClass();
 56
 57            _dialog.Display("Processing EF DbContext RepositoryBase Class");
 58            RenderRepositoryBaseClass();
 59
 60            _dialog.Display("Processing EF DbContext IUnitOfWork Interface");
 61            RenderUnitOfWorkInterface();
 62
 63            _dialog.Display("Processing EF DbContext UnitOfWork Class");
 64            RenderUnitOfWorkClass();
 65
 66            _dialog.InitDialog(3);
 67            _dialog.Display("Processing EF DbContext Generated Classes");
 68
 69            _context.FileList.Add("");
 70            _context.FileList.Add("Processing EF DbContext Generated Classes:");
 71
 72            _dialog.Display("Processing EF DbContext DataObjectsBase Class");
 73            RenderDataObjectsBaseClass();
 74
 75            _dialog.Display("Processing EF DbContext IRepository Interface");
 76            RenderIRepositoryInterface();
 77
 78            _dialog.InitDialog();
 79            foreach (string tableName in _script.Tables)
 80            {
 81                ITable table = _database.Tables[tableName];
 82                _dialog.Display("Processing EF DbContext Repository Interface for '" + tableName + "'");
 83                RenderConcreteInterface(table);
 84            }
 85
 86            _dialog.InitDialog();
 87            foreach (string tableName in _script.Tables)
 88            {
 89                ITable table = _database.Tables[tableName];
 90                _dialog.Display("Processing EF DbContext Repository Class for '" + tableName + "'");
 91                RenderConcreteRepositoryClass(table);
 92            }
 93        }
 94
 95        #endregion
 96
 97        #region Private Methods
 98
 99        private void RenderDataObjectsBaseClass()
100        {
101            _hdrUtil.WriteClassHeader(_output);
102
103            _output.autoTabLn("using System;");
104            _output.autoTabLn("using System.Data.Entity;");
105            _output.autoTabLn("using System.Linq;");
106            _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
107            _output.autoTabLn("");
108            _output.autoTabLn("namespace " + _script.Settings.DataOptions.DataObjectsNamespace + ".Generated");
109            _output.autoTabLn("{");
110            _output.tabLevel++;
111            _output.autoTabLn("public class DataObjectsBase : DbContext");
112            _output.autoTabLn("{");
113            _output.tabLevel++;
114            _output.autoTabLn("#region Private Variables");
115            _output.autoTabLn("");
116
117            foreach (string tableName in _script.Tables)
118            {
119                _output.autoTabLn("private IDbSet<" + StringFormatter.CleanUpClassName(tableName) + "> _" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(tableName, true)) + ";");
120            }
121
122
123            _output.autoTabLn("");
124            _output.autoTabLn("#endregion");
125            _output.autoTabLn("");
126            _output.autoTabLn("#region ctors");
127            _output.autoTabLn("");
128            _output.autoTabLn("public DataObjectsBase(string connectionString)");
129            _output.tabLevel++;
130            _output.autoTabLn(": base(connectionString)");
131            _output.tabLevel--;
132            _output.autoTabLn("{");
133            _output.autoTabLn("");
134            _output.autoTabLn("}");
135            _output.autoTabLn("");
136            _output.autoTabLn("#endregion");
137            _output.autoTabLn("");
138            _output.autoTabLn("#region Public Properties");
139
140            foreach (string tableName in _script.Tables)
141            {
142                _output.autoTabLn("public IDbSet<" + StringFormatter.CleanUpClassName(tableName) + "> " + StringFormatter.CleanUpClassName(tableName, true) + "");
143                _output.autoTabLn("{");
144                _output.tabLevel++;
145                _output.autoTabLn("get");
146                _output.autoTabLn("{");
147                _output.tabLevel++;
148                _output.autoTabLn("return _" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(tableName, true)) + " ?? (_" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(tableName, true)) + " = Set<" + StringFormatter.CleanUpClassName(tableName) + ">());");
149                _output.tabLevel--;
150                _output.autoTabLn("}");
151                _output.tabLevel--;
152                _output.autoTabLn("}");
153                _output.autoTabLn("");
154            }
155
156
157            _output.autoTabLn("#endregion");
158            _output.autoTabLn("");
159            _output.tabLevel--;
160            _output.autoTabLn("}");
161            _output.tabLevel--;
162            _output.autoTabLn("}");
163
164            _context.FileList.Add("    DataObjectsBase.cs");
165            SaveOutput(CreateFullPath(_script.Settings.DataOptions.DataObjectsNamespace + "\\Generated", "DataObjectsBase.cs"), SaveActions.Overwrite);
166        }
167
168        private void RenderIRepositoryInterface()
169        {
170            _hdrUtil.WriteClassHeader(_output);
171
172            _output.autoTabLn("using System;");
173            _output.autoTabLn("using System.Collections.Generic;");
174            _output.autoTabLn("using System.Linq;");
175            _output.autoTabLn("using System.Linq.Expressions;");
176            _output.autoTabLn("");
177            _output.autoTabLn("namespace " + _script.Settings.DataOptions.DataObjectsNamespace + ".Generated");
178            _output.autoTabLn("{");
179            _output.tabLevel++;
180            _output.autoTabLn("public interface IRepository<TEntity>");
181            _output.autoTabLn("{");
182            _output.tabLevel++;
183            _output.autoTabLn("IQueryable<TEntity> GetAll();");
184            _output.autoTabLn("");
185            _output.autoTabLn("IEnumerable<TEntity> Get(");
186            _output.tabLevel++;
187            _output.autoTabLn("Expression<Func<TEntity, bool>> filter = null,");
188            _output.autoTabLn("Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,");
189            _output.autoTabLn("string includeProperties = \"\");");
190            _output.autoTabLn("");
191            _output.tabLevel--;
192            _output.autoTabLn("TEntity GetById(object id);");
193            _output.autoTabLn("");
194            _output.autoTabLn("void Insert(TEntity entity);");
195            _output.autoTabLn("");
196            _output.autoTabLn("void Delete(object id);");
197            _output.autoTabLn("");
198            _output.autoTabLn("void Delete(TEntity entityToDelete);");
199            _output.autoTabLn("");
200            _output.autoTabLn("void Update(TEntity entityToUpdate);");
201            _output.tabLevel--;
202            _output.autoTabLn("}");
203            _output.tabLevel--;
204            _output.autoTabLn("}");
205
206            _context.FileList.Add("    IRepository.cs");
207            SaveOutput(CreateFullPath(_script.Settings.DataOptions.DataObjectsNamespace + "\\Generated", "IRepository.cs"), SaveActions.Overwrite);
208        }
209
210        private void RenderConcreteInterface(ITable table)
211        {
212            _hdrUtil.WriteClassHeader(_output);
213
214            _output.autoTabLn("using System;");
215            _output.autoTabLn("using System.Linq;");
216            _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
217            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Generated;");
218            _output.autoTabLn("");
219            _output.autoTabLn("namespace " + _script.Settings.DataOptions.DataObjectsNamespace + ".Interfaces");
220            _output.autoTabLn("{");
221            _output.tabLevel++;
222            _output.autoTabLn("public interface I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " : IRepository<" + StringFormatter.CleanUpClassName(table.Name) + ">");
223            _output.autoTabLn("{");
224            _output.autoTabLn("}");
225            _output.tabLevel--;
226            _output.autoTabLn("}");
227
228            _context.FileList.Add("    I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + ".cs");
229            SaveOutput(CreateFullPath(_script.Settings.DataOptions.DataObjectsNamespace + "\\Interfaces", "I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + ".cs"), SaveActions.DontOverwrite);
230        }
231
232        private void RenderConcreteRepositoryClass(ITable table)
233        {
234            _hdrUtil.WriteClassHeader(_output);
235
236            _output.autoTabLn("using System;");
237            _output.autoTabLn("using System.Linq;");
238            _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
239            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Interfaces;");
240            _output.autoTabLn("");
241            _output.autoTabLn("namespace " + _script.Settings.DataOptions.DataObjectsNamespace + ".Repositories");
242            _output.autoTabLn("{");
243            _output.tabLevel++;
244            _output.autoTabLn("public class " + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " : RepositoryBase<" + StringFormatter.CleanUpClassName(table.Name) + ">, I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name);
245            _output.autoTabLn("{");
246            _output.tabLevel++;
247            _output.autoTabLn("public " + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + "(IUnitOfWork unitOfWork):base(unitOfWork)");
248            _output.autoTabLn("{");
249            _output.autoTabLn("");
250            _output.autoTabLn("}");
251            _output.tabLevel--;
252            _output.autoTabLn("}");
253            _output.tabLevel--;
254            _output.autoTabLn("}");
255
256            _context.FileList.Add("    " + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + ".cs");
257            SaveOutput(CreateFullPath(_script.Settings.DataOptions.DataObjectsNamespace + "\\Repositories", StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + ".cs"), SaveActions.DontOverwrite);
258        }
259
260        private void RenderDataContextClass()
261        {
262            _hdrUtil.WriteClassHeader(_output);
263
264            _output.autoTabLn("using System.Data.Entity;");
265            _output.autoTabLn("using System.Data.Entity.ModelConfiguration.Conventions;");
266            _output.autoTabLn("using System.Linq;");
267            _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
268            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Generated;");
269            _output.autoTabLn("");
270            _output.autoTabLn("namespace " + _script.Settings.DataOptions.DataObjectsNamespace);
271            _output.autoTabLn("{");
272            _output.tabLevel++;
273            _output.autoTabLn("public class " + _script.Settings.DataOptions.DataContext.Name + " : DataObjectsBase");
274            _output.autoTabLn("{");
275            _output.autoTabLn("");
276            _output.tabLevel++;
277            _output.autoTabLn("#region ctors");
278            _output.autoTabLn("public " + _script.Settings.DataOptions.DataContext.Name + "(string connectionString)");
279            _output.tabLevel++;
280            _output.autoTabLn(": base(connectionString)");
281            _output.tabLevel--;
282            _output.autoTabLn("{");
283            _output.tabLevel++;
284            _output.autoTabLn("Database.SetInitializer<" + _script.Settings.DataOptions.DataContext.Name + ">(null);");
285            _output.tabLevel--;
286            _output.autoTabLn("}");
287            _output.autoTabLn("#endregion");
288            _output.autoTabLn("");
289            _output.autoTabLn("#region Public Methods");
290            _output.autoTabLn("");
291            _output.autoTabLn("public virtual void Commit()");
292            _output.autoTabLn("{");
293            _output.tabLevel++;
294            _output.autoTabLn("base.SaveChanges();");
295            _output.tabLevel--;
296            _output.autoTabLn("}");
297            _output.autoTabLn("");
298            _output.autoTabLn("#endregion");
299            _output.autoTabLn("");
300            _output.autoTabLn("#region Overrides");
301            _output.autoTabLn("");
302            _output.autoTabLn("protected override void OnModelCreating(DbModelBuilder modelBuilder)");
303            _output.autoTabLn("{");
304            _output.tabLevel++;
305            _output.autoTabLn("modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();");
306
307            // Include mappings for entities with schema names.
308            _output.autoTabLn("");
309            foreach (string tableName in _script.Tables)
310            {
311                ITable table = _database.Tables[tableName];
312                if (table.Schema.ToLower() != "dbo")
313                {
314                    _output.autoTabLn("modelBuilder.Entity<" + StringFormatter.CleanUpClassName(tableName) + ">().ToTable(\"" + StringFormatter.CleanUpClassName(tableName) + "\", \"" + table.Schema + "\");");
315                }
316            }
317
318            _output.tabLevel--;
319            _output.autoTabLn("}");
320            _output.autoTabLn("");
321            _output.autoTabLn("#endregion");
322            _output.autoTabLn("");
323            _output.tabLevel--;
324            _output.autoTabLn("}");
325            _output.tabLevel--;
326            _output.autoTabLn("}");
327            _output.autoTabLn("");
328
329            _context.FileList.Add("    " + _script.Settings.DataOptions.DataContext.Name + ".cs");
330            SaveOutput(CreateFullPath(_script.Settings.DataOptions.DataObjectsNamespace, _script.Settings.DataOptions.DataContext.Name + ".cs"), SaveActions.DontOverwrite);
331        }
332
333        private void RenderDataObjectFactory()
334        {
335            _hdrUtil.WriteClassHeader(_output);
336
337            _output.autoTabLn("using System;");
338            _output.autoTabLn("using System.Configuration;");
339            _output.autoTabLn("using System.Linq;");
340            _output.autoTabLn("");
341            _output.autoTabLn("namespace " + _script.Settings.DataOptions.DataObjectsNamespace);
342            _output.autoTabLn("{");
343            _output.tabLevel++;
344            _output.autoTabLn("public static class DataObjectFactory");
345            _output.autoTabLn("{");
346            _output.tabLevel++;
347            _output.autoTabLn("private static readonly string _connectionString;");
348            _output.autoTabLn("");
349            _output.autoTabLn("/// <summary>");
350            _output.autoTabLn("/// Static constructor. Reads the connectionstring from web.config just once.");
351            _output.autoTabLn("/// </summary>");
352            _output.autoTabLn("static DataObjectFactory()");
353            _output.autoTabLn("{");
354            _output.tabLevel++;
355            _output.autoTabLn("string connectionStringName = ConfigurationManager.AppSettings.Get(\"ConnectionStringName\");");
356            _output.autoTabLn("_connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;");
357            _output.tabLevel--;
358            _output.autoTabLn("}");
359            _output.autoTabLn("");
360            _output.autoTabLn("/// <summary>");
361            _output.autoTabLn("/// Creates the Context using the current connectionstring.");
362            _output.autoTabLn("/// </summary>");
363            _output.autoTabLn("/// <remarks>");
364            _output.autoTabLn("/// Gof pattern: Factory method. ");
365            _output.autoTabLn("/// </remarks>");
366            _output.autoTabLn("/// <returns>DbContext.</returns>");
367            _output.autoTabLn("public static " + _script.Settings.DataOptions.DataContext.Name + " CreateContext()");
368            _output.autoTabLn("{");
369            _output.tabLevel++;
370            _output.autoTabLn("return new " + _script.Settings.DataOptions.DataContext.Name + "(_connectionString);");
371            _output.tabLevel--;
372            _output.autoTabLn("}");
373            _output.tabLevel--;
374            _output.autoTabLn("}");
375            _output.tabLevel--;
376            _output.autoTabLn("}");
377
378            _context.FileList.Add("    DataObjectFactory.cs");
379            SaveOutput(CreateFullPath(_script.Settings.DataOptions.DataObjectsNamespace, "DataObjectFactory.cs"), SaveActions.DontOverwrite);
380        }
381
382        private void RenderRepositoryBaseClass()
383        {
384            _hdrUtil.WriteClassHeader(_output);
385
386            _output.autoTabLn("using System;");
387            _output.autoTabLn("using System.Collections.Generic;");
388            _output.autoTabLn("using System.Data;");
389            _output.autoTabLn("using System.Data.Entity;");
390            _output.autoTabLn("using System.Linq;");
391            _output.autoTabLn("using System.Linq.Expressions;");
392            _output.autoTabLn("");
393            _output.autoTabLn("namespace " + _script.Settings.DataOptions.DataObjectsNamespace);
394            _output.autoTabLn("{");
395            _output.tabLevel++;
396            _output.autoTabLn("public abstract class RepositoryBase<TEntity> where TEntity : class");
397            _output.autoTabLn("{");
398            _output.tabLevel++;
399            _output.autoTabLn("#region Locals");
400            _output.autoTabLn("");
401            _output.autoTabLn("internal " + _script.Settings.DataOptions.DataContext.Name + " _context;");
402            _output.autoTabLn("internal IDbSet<TEntity> _dbSet;");
403            _output.autoTabLn("");
404            _output.autoTabLn("#endregion");
405            _output.autoTabLn("");
406            _output.autoTabLn("#region ctors");
407            _output.autoTabLn("private readonly IUnitOfWork _unitOfWork;");
408            _output.autoTabLn("");
409            _output.autoTabLn("public RepositoryBase(IUnitOfWork unitOfWork)");
410            _output.autoTabLn("{");
411            _output.tabLevel++;
412            _output.autoTabLn("this._unitOfWork = unitOfWork;");
413            _output.autoTabLn("this._dbSet = " + _script.Settings.DataOptions.DataContext.Name + ".Set<TEntity>();");
414            _output.tabLevel--;
415            _output.autoTabLn("}");
416            _output.autoTabLn("");
417            _output.autoTabLn("#endregion");
418            _output.autoTabLn("");
419            _output.autoTabLn("#region Public CRUD Methods");
420            _output.autoTabLn("");
421            _output.autoTabLn("public virtual IQueryable<TEntity> GetAll()");
422            _output.autoTabLn("{");
423            _output.tabLevel++;
424            _output.autoTabLn("IQueryable<TEntity> query = _dbSet;");
425            _output.autoTabLn("");
426            _output.autoTabLn("return query;");
427            _output.tabLevel--;
428            _output.autoTabLn("}");
429            _output.autoTabLn("");
430
431            _output.autoTabLn("public virtual IEnumerable<TEntity> Get(");
432            _output.tabLevel++;
433            _output.autoTabLn("Expression<Func<TEntity, bool>> filter = null,");
434            _output.autoTabLn("Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,");
435            _output.autoTabLn("string includeProperties = \"\")");
436            _output.tabLevel--;
437            _output.autoTabLn("{");
438            _output.tabLevel++;
439            _output.autoTabLn("IQueryable<TEntity> query = _dbSet;");
440            _output.autoTabLn("");
441            _output.autoTabLn("if (filter != null)");
442            _output.autoTabLn("{");
443            _output.tabLevel++;
444            _output.autoTabLn("query = query.Where(filter);");
445            _output.tabLevel--;
446            _output.autoTabLn("}");
447            _output.autoTabLn("");
448            _output.autoTabLn("foreach (var includeProperty in includeProperties.Split");
449            _output.tabLevel++;
450            _output.autoTabLn("(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))");
451            _output.tabLevel--;
452            _output.autoTabLn("{");
453            _output.tabLevel++;
454            _output.autoTabLn("query = query.Include(includeProperty);");
455            _output.tabLevel--;
456            _output.autoTabLn("}");
457            _output.autoTabLn("");
458            _output.autoTabLn("if (orderBy != null)");
459            _output.autoTabLn("{");
460            _output.tabLevel++;
461            _output.autoTabLn("return orderBy(query);");
462            _output.tabLevel--;
463            _output.autoTabLn("}");
464            _output.autoTabLn("else");
465            _output.autoTabLn("{");
466            _output.tabLevel++;
467            _output.autoTabLn("return query;");
468            _output.tabLevel--;
469            _output.autoTabLn("}");
470            _output.tabLevel--;
471            _output.autoTabLn("}");
472            _output.autoTabLn("");
473            _output.autoTabLn("public virtual TEntity GetById(object id)");
474            _output.autoTabLn("{");
475            _output.tabLevel++;
476            _output.autoTabLn("return _dbSet.Find(id);");
477            _output.tabLevel--;
478            _output.autoTabLn("}");
479            _output.autoTabLn("");
480            _output.autoTabLn("public virtual void Insert(TEntity entity)");
481            _output.autoTabLn("{");
482            _output.tabLevel++;
483            _output.autoTabLn("_dbSet.Add(entity);");
484            _output.tabLevel--;
485            _output.autoTabLn("}");
486            _output.autoTabLn("");
487            _output.autoTabLn("public virtual void Delete(object id)");
488            _output.autoTabLn("{");
489            _output.tabLevel++;
490            _output.autoTabLn("TEntity entityToDelete = _dbSet.Find(id);");
491            _output.autoTabLn("Delete(entityToDelete);");
492            _output.tabLevel--;
493            _output.autoTabLn("}");
494            _output.autoTabLn("");
495            _output.autoTabLn("public virtual void Delete(TEntity entityToDelete)");
496            _output.autoTabLn("{");
497
498            // Check which version of Entity Framework is being used.
499
500            _output.tabLevel++;
501
502            if (_criteria == null)
503            {
504                _output.autoTabLn("if (_context.Entry(entityToDelete).State == EntityState.Detached)");
505            }
506            else
507            {
508                if (_criteria.EFVersion >= 6.0)
509                {
510                    // Entity Framework 6.x
511                    _output.autoTabLn("if (_context.Entry(entityToDelete).State == System.Data.Entity.EntityState.Detached)");
512                }
513                else
514                {
515                    //     Entity Framework 5.0 and below
516                    //_output.autoTabLn("// If this line goes red, then you are using Entity Framework 6.x");
517                    //_output.autoTabLn("// You need to change the line to this:");
518                    //_output.autoTabLn("//     _context.Entry(entityToDelete).State == System.Data.Entity.EntityState.Detached");
519
520                    _output.autoTabLn("if (_context.Entry(entityToDelete).State == EntityState.Detached)");
521                }
522            }
523
524
525            
526
527            _output.autoTabLn("{");
528            _output.tabLevel++;
529            _output.autoTabLn("_dbSet.Attach(entityToDelete);");
530            _output.tabLevel--;
531            _output.autoTabLn("}");
532            _output.autoTabLn("_dbSet.Remove(entityToDelete);");
533            _output.tabLevel--;
534            _output.autoTabLn("}");
535            _output.autoTabLn("");
536            _output.autoTabLn("public virtual void Update(TEntity entityToUpdate)");
537            _output.autoTabLn("{");
538            _output.tabLevel++;
539            _output.autoTabLn("_dbSet.Attach(entityToUpdate);");
540            //_output.autoTabLn("// If this line goes red, then you are using Entity Framework 6.x");
541            //_output.autoTabLn("// You need to change the line to this:");
542            //_output.autoTabLn("//     _context.Entry(entityToUpdate).State == System.Data.Entity.EntityState.Modified");
543
544            if (_criteria.EFVersion >= 6.0)
545            {
546                _output.autoTabLn("_context.Entry(entityToUpdate).State = System.Data.Entity.EntityState.Modified;");
547            }
548            else
549            {
550                _output.autoTabLn("_context.Entry(entityToUpdate).State = EntityState.Modified;");
551            }
552            
553            
554            
555            _output.tabLevel--;
556            _output.autoTabLn("}");
557            _output.autoTabLn("#endregion");
558            _output.autoTabLn("");
559            _output.autoTabLn("#region Protected Members");
560            _output.autoTabLn("");
561            _output.autoTabLn("protected " + _script.Settings.DataOptions.DataContext.Name + " " + _script.Settings.DataOptions.DataContext.Name + "");
562            _output.autoTabLn("{");
563            _output.tabLevel++;
564            _output.autoTabLn("get");
565            _output.autoTabLn("{");
566            _output.tabLevel++;
567            _output.autoTabLn("return _context ?? (_context = _unitOfWork." + _script.Settings.DataOptions.DataContext.Name + ");");
568            _output.tabLevel--;
569            _output.autoTabLn("}");
570            _output.tabLevel--;
571            _output.autoTabLn("}");
572            _output.autoTabLn("");
573            _output.autoTabLn("#endregion");
574            _output.autoTabLn("");
575            _output.tabLevel--;
576            _output.autoTabLn("}");
577            _output.tabLevel--;
578            _output.autoTabLn("}");
579
580            _context.FileList.Add("    RepositoryBase.cs");
581            SaveOutput(CreateFullPath(_script.Settings.DataOptions.DataObjectsNamespace, "RepositoryBase.cs"), SaveActions.DontOverwrite);
582        }
583
584        private void RenderUnitOfWorkClass()
585        {
586            _hdrUtil.WriteClassHeader(_output);
587
588            _output.autoTabLn("using System;");
589            _output.autoTabLn("using System.Linq;");
590            _output.autoTabLn("");
591            _output.autoTabLn("namespace " + _script.Settings.DataOptions.DataObjectsNamespace);
592            _output.autoTabLn("{");
593            _output.tabLevel++;
594            _output.autoTabLn("public class UnitOfWork : Disposable, IUnitOfWork");
595            _output.autoTabLn("{");
596            _output.tabLevel++;
597            _output.autoTabLn("private " + _script.Settings.DataOptions.DataContext.Name + " _context;");
598            _output.autoTabLn("");
599            _output.autoTabLn("public " + _script.Settings.DataOptions.DataContext.Name + " " + _script.Settings.DataOptions.DataContext.Name + "");
600            _output.autoTabLn("{");
601            _output.tabLevel++;
602            _output.autoTabLn("get");
603            _output.autoTabLn("{");
604            _output.tabLevel++;
605            _output.autoTabLn("return _context ?? (_context = DataObjectFactory.CreateContext());");
606            _output.tabLevel--;
607            _output.autoTabLn("}");
608            _output.tabLevel--;
609            _output.autoTabLn("}");
610            _output.autoTabLn("");
611            _output.autoTabLn("public void Commit()");
612            _output.autoTabLn("{");
613            _output.tabLevel++;
614            _output.autoTabLn(_script.Settings.DataOptions.DataContext.Name + ".Commit();");
615            _output.tabLevel--;
616            _output.autoTabLn("}");
617            _output.autoTabLn("");
618            _output.autoTabLn("protected override void DisposeCore()");
619            _output.autoTabLn("{");
620            _output.tabLevel++;
621            _output.autoTabLn("if (_context != null)");
622            _output.autoTabLn("{");
623            _output.tabLevel++;
624            _output.autoTabLn("_context.Dispose();");
625            _output.tabLevel--;
626            _output.autoTabLn("}");
627            _output.tabLevel--;
628            _output.autoTabLn("}");
629            _output.tabLevel--;
630            _output.autoTabLn("}");
631            _output.tabLevel--;
632            _output.autoTabLn("}");
633
634            _context.FileList.Add("    UnitOfWork.cs");
635            SaveOutput(CreateFullPath(_script.Settings.DataOptions.DataObjectsNamespace, "UnitOfWork.cs"), SaveActions.DontOverwrite);
636        }
637
638        private void RenderUnitOfWorkInterface()
639        {
640            _hdrUtil.WriteClassHeader(_output);
641
642            _output.autoTabLn("namespace " + _script.Settings.DataOptions.DataObjectsNamespace);
643            _output.autoTabLn("{");
644            _output.tabLevel++;
645            _output.autoTabLn("public interface IUnitOfWork");
646            _output.autoTabLn("{");
647            _output.tabLevel++;
648            _output.autoTabLn(_script.Settings.DataOptions.DataContext.Name + " " + _script.Settings.DataOptions.DataContext.Name + " { get; }");
649            _output.autoTabLn("");
650            _output.autoTabLn("void Commit();");
651            _output.tabLevel--;
652            _output.autoTabLn("}");
653            _output.tabLevel--;
654            _output.autoTabLn("}");
655
656            _context.FileList.Add("    IUnitOfWork.cs");
657            SaveOutput(CreateFullPath(_script.Settings.DataOptions.DataObjectsNamespace, "IUnitOfWork.cs"), SaveActions.DontOverwrite);
658        }
659
660        private void RenderDisposableClass()
661        {
662            _hdrUtil.WriteClassHeader(_output);
663            _output.autoTabLn("using System;");
664            _output.autoTabLn("using System.Linq;");
665            _output.autoTabLn("");
666            _output.autoTabLn("namespace " + _script.Settings.DataOptions.DataObjectsNamespace);
667            _output.autoTabLn("{");
668            _output.tabLevel++;
669            _output.autoTabLn("public abstract class Disposable : IDisposable");
670            _output.autoTabLn("{");
671            _output.tabLevel++;
672            _output.autoTabLn("private bool isDisposed;");
673            _output.autoTabLn("");
674            _output.autoTabLn("~Disposable()");
675            _output.autoTabLn("{");
676            _output.tabLevel++;
677            _output.autoTabLn("Dispose(false);");
678            _output.tabLevel--;
679            _output.autoTabLn("}");
680            _output.autoTabLn("");
681            _output.autoTabLn("protected virtual void DisposeCore()");
682            _output.autoTabLn("{");
683            _output.autoTabLn("");
684            _output.autoTabLn("}");
685            _output.autoTabLn("");
686            _output.autoTabLn("private void Dispose(bool disposing)");
687            _output.autoTabLn("{");
688            _output.tabLevel++;
689            _output.autoTabLn("if (!isDisposed && disposing)");
690            _output.autoTabLn("{");
691            _output.tabLevel++;
692            _output.autoTabLn("DisposeCore();");
693            _output.tabLevel--;
694            _output.autoTabLn("}");
695            _output.autoTabLn("");
696            _output.autoTabLn("isDisposed = true;");
697            _output.tabLevel--;
698            _output.autoTabLn("}");
699            _output.autoTabLn("");
700            _output.autoTabLn("public void Dispose()");
701            _output.autoTabLn("{");
702            _output.tabLevel++;
703            _output.autoTabLn("Dispose(true);");
704            _output.autoTabLn("GC.SuppressFinalize(this);");
705            _output.tabLevel--;
706            _output.autoTabLn("}");
707            _output.tabLevel--;
708            _output.autoTabLn("}");
709            _output.tabLevel--;
710            _output.autoTabLn("}");
711
712            _context.FileList.Add("    Disposable.cs");
713            SaveOutput(CreateFullPath(_script.Settings.DataOptions.DataObjectsNamespace, "Disposable.cs"), SaveActions.DontOverwrite);
714        }
715
716        #endregion
717    }
718}
719
720