PageRenderTime 47ms CodeModel.GetById 13ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Plugins/GizmoBeach.Components/ServiceLayer/ServiceClassesAndCommonBaseClassForDbContextServiceLayerObjects.cs

http://github.com/kahanu/CondorXE
C# | 357 lines | 305 code | 39 blank | 13 comment | 4 complexity | 44b4b8a7e41ecfe8fa44e59101fdf021 MD5 | raw file
  1using System;
  2using System.Linq;
  3using Condor.Core;
  4using Condor.Core.Interfaces;
  5using MyMeta;
  6
  7namespace GizmoBeach.Components.ServiceLayer
  8{
  9    public class ServiceClassesAndCommonBaseClassForDbContextServiceLayerObjects : RenderBase, IServiceObjects
 10    {
 11        #region ctors
 12        private readonly RequestContext _context;
 13
 14        public ServiceClassesAndCommonBaseClassForDbContextServiceLayerObjects(RequestContext context)
 15            : base(context.Zeus.Output)
 16        {
 17            this._context = context;
 18        }
 19        #endregion
 20
 21        #region IRenderObject Members
 22
 23        public void Render()
 24        {
 25            _context.Dialog.InitDialog(_context.Database.Tables.Count);
 26            _context.FileList.Add("");
 27            _context.FileList.Add("Generated ServiceClassesForDbContext Service Layer interfaces: ");
 28            RenderServiceCRUDInterface();
 29            foreach (string tableName in _script.Tables)
 30            {
 31                _context.Dialog.Display("Adding I" + tableName + "Service Interface");
 32                ITable table = _context.Database.Tables[tableName];
 33                RenderInterface(table);
 34            }
 35
 36            _context.Dialog.InitDialog(_context.Database.Tables.Count);
 37            _context.FileList.Add("");
 38            _context.FileList.Add("Generated ServiceClassesForDbContext Service Layer Base class. ");
 39            RenderServiceBaseClass();
 40            //foreach (string tableName in _script.Tables)
 41            //{
 42            //    _context.Dialog.Display("Adding " + tableName + "ServiceBase class");
 43            //    ITable table = _context.Database.Tables[tableName];
 44            //    RenderServiceBaseClass(table);
 45            //}
 46
 47            _context.Dialog.InitDialog(_context.Database.Tables.Count);
 48            _context.FileList.Add("");
 49            _context.FileList.Add("Generated ServiceClassesForDbContext Service Layer classes: ");
 50            foreach (string tableName in _script.Tables)
 51            {
 52                _context.Dialog.Display("Adding " + tableName + "Service class");
 53                ITable table = _context.Database.Tables[tableName];
 54                RenderConcreteClass(table);
 55            }
 56        }
 57
 58
 59
 60  
 61        
 62
 63        #endregion
 64
 65        #region Private Methods
 66
 67        private void RenderServiceCRUDInterface()
 68        {
 69            _hdrUtil.WriteClassHeader(_output);
 70
 71            _output.autoTabLn("using System;");
 72            _output.autoTabLn("using System.Collections.Generic;");
 73            _output.autoTabLn("using System.Linq;");
 74            _output.autoTabLn("using System.Linq.Expressions;");
 75            _output.autoTabLn("");
 76            _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".Generated");
 77            _output.autoTabLn("{");
 78            _output.tabLevel++;
 79            _output.autoTabLn("public interface IService<TEntity>");
 80            _output.autoTabLn("{");
 81            _output.tabLevel++;
 82            _output.autoTabLn("IEnumerable<TEntity> Get(");
 83            _output.tabLevel++;
 84            _output.autoTabLn("Expression<Func<TEntity, bool>> filter = null,");
 85            _output.autoTabLn("Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,");
 86            _output.autoTabLn("string includeProperties = \"\");");
 87            _output.tabLevel--;
 88            _output.autoTabLn("");
 89            _output.autoTabLn("TEntity GetById(object id);");
 90            _output.autoTabLn("void Insert(TEntity entity);");
 91            _output.autoTabLn("void Delete(object id);");
 92            _output.autoTabLn("void Delete(TEntity entityToDelete);");
 93            _output.autoTabLn("void Update(TEntity entityToUpdate);");
 94            _output.tabLevel--;
 95            _output.autoTabLn("}");
 96            _output.tabLevel--;
 97            _output.autoTabLn("}");
 98
 99            _context.FileList.Add("    IService.cs");
100            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\Generated", "IService.cs"), SaveActions.Overwrite);
101        }
102
103        private void RenderInterface(ITable table)
104        {
105            _hdrUtil.WriteClassHeader(_output);
106
107            _output.autoTabLn("using System;");
108            _output.autoTabLn("using System.Linq;");
109            _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
110            _output.autoTabLn("using " + _script.Settings.ServiceLayer.ServiceNamespace + ".Generated;");
111            _output.autoTabLn("");
112            _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".Interfaces");
113            _output.autoTabLn("{");
114            _output.tabLevel++;
115            _output.autoTabLn("public interface I" + StringFormatter.CleanUpClassName(table.Name) + "Service : IService<" + StringFormatter.CleanUpClassName(table.Name) + ">");
116            _output.autoTabLn("{");
117
118            // Updated 7/15/2014 - set a custom member for SiteSettings - used by King Wilder
119            if (table.Name == "SiteSetting")
120            {
121                _output.tabLevel++;
122                _output.autoTabLn("SiteSetting GetByKey(string key);");
123                _output.tabLevel--;
124            }
125
126            _output.autoTabLn("}");
127            _output.tabLevel--;
128            _output.autoTabLn("}");
129
130            _context.FileList.Add("    I" + StringFormatter.CleanUpClassName(table.Name) + "Service.cs");
131            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\Interfaces", "I" + StringFormatter.CleanUpClassName(table.Name) + "Service.cs"), SaveActions.DontOverwrite);
132        }
133
134
135        /// <summary>
136        /// New! Added 12/30/2013
137        /// Streamlined the CRUD operations into a single base class instead of a base class
138        /// for each entity.
139        /// </summary>
140        private void RenderServiceBaseClass()
141        {
142            _hdrUtil.WriteClassHeader(_output);
143
144            _output.autoTabLn("using System;");
145            _output.autoTabLn("using System.Collections.Generic;");
146            _output.autoTabLn("using System.Linq;");
147            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Generated;");
148            _output.autoTabLn("");
149            _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".Generated");
150            _output.autoTabLn("{");
151            _output.tabLevel++;
152            _output.autoTabLn("/// <summary>");
153            _output.autoTabLn("/// The common base class for CRUD operations.");
154            _output.autoTabLn("/// </summary>");
155            _output.autoTabLn("/// <typeparam name=\"T\">the business object</typeparam>");
156            _output.autoTabLn("/// <typeparam name=\"TInterface\">the data objects repository interface</typeparam>");
157            _output.autoTabLn("public class ServiceBase<T, TInterface> : IService<T> where TInterface : IRepository<T>");
158            _output.autoTabLn("{");
159            _output.tabLevel++;
160            _output.autoTabLn("#region Constructors");
161            _output.autoTabLn("protected readonly TInterface _dao;");
162            _output.autoTabLn("");
163            _output.autoTabLn("public ServiceBase(TInterface dao)");
164            _output.autoTabLn("{");
165            _output.tabLevel++;
166            _output.autoTabLn("this._dao = dao;");
167            _output.tabLevel--;
168            _output.autoTabLn("} ");
169            _output.autoTabLn("#endregion");
170            _output.autoTabLn("");
171            _output.autoTabLn("#region CRUD Operations");
172            _output.autoTabLn("public IEnumerable<T> Get(System.Linq.Expressions.Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = \"\")");
173            _output.autoTabLn("{");
174            _output.tabLevel++;
175            _output.autoTabLn("return _dao.Get(filter, orderBy, includeProperties);");
176            _output.tabLevel--;
177            _output.autoTabLn("}");
178            _output.autoTabLn("");
179            _output.autoTabLn("public T GetById(object id)");
180            _output.autoTabLn("{");
181            _output.tabLevel++;
182            _output.autoTabLn("return _dao.GetById(id);");
183            _output.tabLevel--;
184            _output.autoTabLn("}");
185            _output.autoTabLn("");
186            _output.autoTabLn("public void Insert(T entity)");
187            _output.autoTabLn("{");
188            _output.tabLevel++;
189            _output.autoTabLn("_dao.Insert(entity);");
190            _output.tabLevel--;
191            _output.autoTabLn("}");
192            _output.autoTabLn("");
193            _output.autoTabLn("public void Delete(object id)");
194            _output.autoTabLn("{");
195            _output.tabLevel++;
196            _output.autoTabLn("T entity = _dao.GetById(id);");
197            _output.autoTabLn("_dao.Delete(entity);");
198            _output.tabLevel--;
199            _output.autoTabLn("}");
200            _output.autoTabLn("");
201            _output.autoTabLn("public void Delete(T entityToDelete)");
202            _output.autoTabLn("{");
203            _output.tabLevel++;
204            _output.autoTabLn("_dao.Delete(entityToDelete);");
205            _output.tabLevel--;
206            _output.autoTabLn("}");
207            _output.autoTabLn("");
208            _output.autoTabLn("public void Update(T entityToUpdate)");
209            _output.autoTabLn("{");
210            _output.tabLevel++;
211            _output.autoTabLn("_dao.Update(entityToUpdate);");
212            _output.tabLevel--;
213            _output.autoTabLn("}");
214            _output.autoTabLn("#endregion");
215            _output.tabLevel--;
216            _output.autoTabLn("}");
217            _output.tabLevel--;
218            _output.autoTabLn("}");
219
220            _context.FileList.Add("    ServiceBase.cs");
221            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\Generated", "ServiceBase.cs"), SaveActions.Overwrite);
222        }
223
224
225        private void RenderServiceBaseClass(ITable table)
226        {
227            _hdrUtil.WriteClassHeader(_output);
228
229            _output.autoTabLn("using System;");
230            _output.autoTabLn("using System.Collections.Generic;");
231            _output.autoTabLn("using System.Linq;");
232            _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
233            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Interfaces;");
234            _output.autoTabLn("");
235            _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace +".Generated");
236            _output.autoTabLn("{");
237            _output.tabLevel++;
238            _output.autoTabLn("public class " + StringFormatter.CleanUpClassName(table.Name) + "ServiceBase : IService<" + _context.Utility.BuildModelClassWithNameSpace(table.Name) + ">");
239            _output.autoTabLn("{");
240            _output.tabLevel++;
241            _output.autoTabLn("#region Constructors");
242            _output.autoTabLn("protected readonly I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " _" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ";");
243            _output.autoTabLn("");
244            _output.autoTabLn("public " + StringFormatter.CleanUpClassName(table.Name) + "ServiceBase(I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " " + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ")");
245            _output.autoTabLn("{");
246            _output.tabLevel++;
247            _output.autoTabLn("this._" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + " = " + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ";");
248            _output.tabLevel--;
249            _output.autoTabLn("} ");
250            _output.autoTabLn("#endregion");
251            _output.autoTabLn("");
252            _output.autoTabLn("#region CRUD Methods");
253            _output.autoTabLn("public IEnumerable<" + StringFormatter.CleanUpClassName(table.Name) + "> Get(System.Linq.Expressions.Expression<Func<" + StringFormatter.CleanUpClassName(table.Name) + ", bool>> filter = null, Func<IQueryable<" + StringFormatter.CleanUpClassName(table.Name) + ">, IOrderedQueryable<" + StringFormatter.CleanUpClassName(table.Name) + ">> orderBy = null, string includeProperties = \"\")");
254            _output.autoTabLn("{");
255            _output.tabLevel++;
256            _output.autoTabLn("return _" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".Get(filter, orderBy, includeProperties);");
257            _output.tabLevel--;
258            _output.autoTabLn("}");
259            _output.autoTabLn("");
260            _output.autoTabLn("public " + StringFormatter.CleanUpClassName(table.Name) + " GetById(object id)");
261            _output.autoTabLn("{");
262            _output.tabLevel++;
263            _output.autoTabLn("return _" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".GetById(id);");
264            _output.tabLevel--;
265            _output.autoTabLn("}");
266            _output.autoTabLn("");
267            _output.autoTabLn("public void Insert(" + StringFormatter.CleanUpClassName(table.Name) + " entity)");
268            _output.autoTabLn("{");
269            _output.tabLevel++;
270            _output.autoTabLn("_" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".Insert(entity);");
271            _output.tabLevel--;
272            _output.autoTabLn("}");
273            _output.autoTabLn("");
274            _output.autoTabLn("public void Delete(object id)");
275            _output.autoTabLn("{");
276            _output.tabLevel++;
277            _output.autoTabLn(StringFormatter.CleanUpClassName(table.Name) + " entity = _" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".GetById(id);");
278            _output.autoTabLn("_" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".Delete(entity);");
279            _output.tabLevel--;
280            _output.autoTabLn("}");
281            _output.autoTabLn("");
282            _output.autoTabLn("public void Delete(" + StringFormatter.CleanUpClassName(table.Name) + " entityToDelete)");
283            _output.autoTabLn("{");
284            _output.tabLevel++;
285            _output.autoTabLn("_" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".Delete(entityToDelete);");
286            _output.tabLevel--;
287            _output.autoTabLn("}");
288            _output.autoTabLn("");
289            _output.autoTabLn("public void Update(" + StringFormatter.CleanUpClassName(table.Name) + " entityToUpdate)");
290            _output.autoTabLn("{");
291            _output.tabLevel++;
292            _output.autoTabLn("_" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".Update(entityToUpdate);");
293            _output.tabLevel--;
294            _output.autoTabLn("} ");
295            _output.autoTabLn("#endregion");
296            _output.tabLevel--;
297            _output.autoTabLn("}");
298            _output.tabLevel--;
299            _output.autoTabLn("");
300            _output.autoTabLn("}");
301
302            _context.FileList.Add("    " + StringFormatter.CleanUpClassName(table.Name) + "ServiceBase.cs");
303            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\Generated", StringFormatter.CleanUpClassName(table.Name) + "ServiceBase.cs"), SaveActions.Overwrite);
304
305        }
306
307        private void RenderConcreteClass(ITable table)
308        {
309            _hdrUtil.WriteClassHeader(_output);
310
311            _output.autoTabLn("using System;");
312            _output.autoTabLn("using System.Linq;");
313            _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
314            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Interfaces;");
315            _output.autoTabLn("using " + _script.Settings.ServiceLayer.ServiceNamespace + ".Generated;");
316            _output.autoTabLn("using " + _script.Settings.ServiceLayer.ServiceNamespace + ".Interfaces;");
317            _output.autoTabLn("");
318            _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace);
319            _output.autoTabLn("{");
320            _output.tabLevel++;
321            _output.autoTabLn("public class " + StringFormatter.CleanUpClassName(table.Name) + "Service : ServiceBase<" + StringFormatter.CleanUpClassName(table.Name) + ", I" + StringFormatter.CleanUpClassName(table.Name) + "Dao>, I" + StringFormatter.CleanUpClassName(table.Name) + "Service");
322            _output.autoTabLn("{");
323            _output.tabLevel++;
324            _output.autoTabLn("public " + StringFormatter.CleanUpClassName(table.Name) + "Service(I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " " + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ")");
325            _output.tabLevel++;
326            _output.autoTabLn(": base(" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ")");
327            _output.tabLevel--;
328            _output.autoTabLn("{");
329            _output.autoTabLn("");
330            _output.autoTabLn("}");
331
332
333            // Updated 7/15/2014 - by King Wilder - write custom code for SiteSetting - used by King Wilder
334            if (table.Name == "SiteSetting")
335            {
336                _output.autoTabLn("public SiteSetting GetByKey(string key)");
337                _output.autoTabLn("{");
338                _output.tabLevel++;
339                _output.autoTabLn("return _dao.Get(s => s.Name == key).SingleOrDefault();");
340                _output.tabLevel--;
341                _output.autoTabLn("}");
342            }
343
344
345            _output.tabLevel--;
346            _output.autoTabLn("}");
347            _output.tabLevel--;
348            _output.autoTabLn("}");
349
350            _context.FileList.Add("    " + StringFormatter.CleanUpClassName(table.Name) + "Service.cs");
351            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace, StringFormatter.CleanUpClassName(table.Name) + "Service.cs"), SaveActions.DontOverwrite);
352        }
353        #endregion
354    }
355}
356
357