Mdf walkthrough

Custom Class Generator

CCG is the core of entire framework.

You can describe your entities (equipment, orders, materials, lots, sublots, handling units and personnel) and an useful Visual Studio addin create sql tables, data sets and .NET classes.

In example, in your MES application you want to model an equipmet like a Tank with a Volume property. So you add a new EquipmentClass (Tank), an EquipmentProperty (decimal Volume) and associate property to equipment class. Launching Visual Studio addin, CCG creates for you Tanks tables, Mdf.Custom.Equipment.DsEquipment, a data set to access the table, and the Tank class.

Here you can find the Tank.Mdf.cs generated from the CCG

1#region Using
2
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Text;
7using System.Xml;
8using System.Transactions;
9using System.Linq.Expressions;
10using System.Data.Objects;
11using System.Reflection;
12using System.Data.Objects.DataClasses;
13using Mdf.Equipment;
14using Mdf.Linq;
15
16#endregion
17
18namespace Mdf.BusinessEntities.Equipment
19{
20    public partial class Tank
21    {
22        #region Override
23
24        /// <summary>
25        /// Convert equipment primary key into string
26        /// </summary>
27        public override string ToString()
28        {
29            return this.TankID.ToString();
30        }
31
32        #endregion
33
34        #region Properties
35
36        private string _Description = null;
37
38        /// <summary>
39        /// The equipment description
40        /// </summary>
41        public string Description
42        {
43            get
44            {
45                if (Equipment != null)
46                {
47                    return Equipment.Description;
48                }
49                else
50                {
51                    return _Description;
52                }
53            }
54            set
55            {
56                if (Equipment != null)
57                {
58                    Equipment.Description = value;
59                }
60                else
61                {
62                    _Description = value;
63                }
64            }
65        }
66
67        private string _ParentEquipmentID = null;
68
69        /// <summary>
70        /// The parent equipment id
71        /// </summary>
72        public string ParentEquipmentID
73        {
74            get
75            {
76                if (Equipment != null)
77                {
78                    if (Equipment.ParentEquipment != null)
79                    {
80                        return Equipment.ParentEquipment.EquipmentID;
81                    }
82                    else
83                    {
84                        return null;
85                    }
86                }
87                else
88                {
89                    return _ParentEquipmentID;
90                }
91            }
92            set
93            {
94                if (Equipment != null)
95                {
96                    if (value != null)
97                    {
98                        Equipment.ParentEquipment = CustomEquipmentEntities.Equipments.Where(equipmentRow => equipmentRow.EquipmentID == value).First();
99                    }
100                    else
101                    {
102                        Equipment.ParentEquipment = null;
103                    }
104                }
105                else
106                {
107                    _ParentEquipmentID = value;
108                }
109            }
110        }
111
112        protected CustomEquipmentEntities _CustomEquipmentEntities;
113        protected CustomEquipmentEntities CustomEquipmentEntities
114        {
115            get
116            {
117                if (_CustomEquipmentEntities == null)
118                {
119                    if (((IEntityWithRelationships)this).RelationshipManager.GetAllRelatedEnds().First().CreateSourceQuery() == null)
120                    {
121                        _CustomEquipmentEntities = CustomEquipmentEntities.CreateCustomEquipmentEntities();
122                    }
123                    else
124                    {
125                        _CustomEquipmentEntities = (CustomEquipmentEntities)((ObjectQuery)((IEntityWithRelationships)this).RelationshipManager.GetAllRelatedEnds().First().CreateSourceQuery()).Context;
126                    }
127                }
128                return _CustomEquipmentEntities;
129            }
130        }
131
132        #endregion
133
134        #region Methods
135
136        /// <summary>
137        /// Save all changes
138        /// </summary>
139        public void Save()
140        {
141            Save(CustomEquipmentEntities);
142        }
143
144        /// <summary>
145        /// Save all changes by entities
146        /// </summary>
147        /// <param name="customEquipmentEntities">The custom equipment entities</param>
148        private void Save(CustomEquipmentEntities customEquipmentEntities)
149        {
150            if (Equipment == null)
151            {
152                Equipment = Equipment.CreateEquipment(this.TankID, this.Description, "Tank");
153                if (this._ParentEquipmentID != null)
154                {
155                    Equipment.ParentEquipment = customEquipmentEntities.Equipments.Where(equipmentRow => equipmentRow.EquipmentID == this._ParentEquipmentID).First();
156                }
157            }
158
159            using (TransactionScope transactionScope = new TransactionScope())
160            {
161                if (Equipment.EntityKey == null)
162                {
163                    customEquipmentEntities.AddToEquipments(Equipment);
164                }
165                if (this.EntityKey == null)
166                {
167                    customEquipmentEntities.AddToTanks(this);
168                }
169
170                customEquipmentEntities.SaveChanges();
171                transactionScope.Complete();
172            }
173        }
174
175        #endregion
176
177        #region Static methods
178
179        #region Add, modify and delete methods
180
181        /// <summary>
182        /// Add a new equipment
183        /// </summary>
184        /// <param name="tankID">The tank id of the equipment</param>
185        /// <param name="description">The description of the equipment</param>
186        /// <param name="parentEquipmentID">The parent equipment id of the equipment</param>
187        /// <param name="volume">The volume of the equipment</param>
188        /// <returns>The new equipment row</returns>
189        public static Tank Add(string tankID, string description, string parentEquipmentID, decimal? volume)
190        {
191            CustomEquipmentEntities customEquipmentEntities = CustomEquipmentEntities.CreateCustomEquipmentEntities();
192            return Add(customEquipmentEntities, tankID, description, parentEquipmentID, volume);
193        }
194
195        /// <summary>
196        /// Add a new equipment
197        /// </summary>
198        /// <param name="customEquipmentEntities">The equipment entities</param>
199        /// <param name="tankID">The tank id of the equipment</param>
200        /// <param name="description">The description of the equipment</param>
201        /// <param name="parentEquipmentID">The parent equipment id of the equipment</param>
202        /// <param name="volume">The volume of the equipment</param>
203        /// <returns>The new equipment row</returns>
204        public static Tank Add(CustomEquipmentEntities customEquipmentEntities, string tankID, string description, string parentEquipmentID, decimal? volume)
205        {
206            Equipment equipment = Equipment.CreateEquipment(tankID, description, "Tank");
207
208            if (parentEquipmentID != null)
209            {
210                equipment.ParentEquipment = customEquipmentEntities.Equipments.Where(equipmentRow => equipmentRow.EquipmentID == parentEquipmentID).First();
211            }
212            else
213            {
214                equipment.ParentEquipment = null;
215            }
216
217            Tank tank = Tank.CreateTank(tankID);
218            tank.Equipment = equipment;
219            if (volume != null)
220            {
221                tank.Volume = volume;
222            }
223            else
224            {
225                tank.Volume = null;
226            }
227
228            using (TransactionScope transactionScope = new TransactionScope())
229            {
230                tank.OnBeforeAdd();
231                customEquipmentEntities.AddToTanks(tank);
232                customEquipmentEntities.SaveChanges();
233                tank.OnAfterAdd();
234                transactionScope.Complete();
235            }
236
237            return tank;
238        }
239
240        /// <summary>
241        /// Modify the equipment
242        /// </summary>
243        /// <param name="tankID">The tank id of the equipment</param>
244        /// <param name="description">The description of the equipment</param>
245        /// <param name="parentEquipmentID">The parent equipment id of the equipment</param>
246        /// <param name="volume">The volume of the equipment</param>
247        public static void Modify(string tankID, string description, string parentEquipmentID, decimal? volume)
248        {
249            CustomEquipmentEntities customEquipmentEntities = CustomEquipmentEntities.CreateCustomEquipmentEntities();
250            Modify(customEquipmentEntities, tankID, description, parentEquipmentID, volume);
251        }
252        
253        /// <summary>
254        /// Modify the equipment
255        /// </summary>
256        /// <param name="customEquipmentEntities">The equipment entities</param>
257        /// <param name="tankID">The tank id of the equipment</param>
258        /// <param name="description">The description of the equipment</param>
259        /// <param name="parentEquipmentID">The parent equipment id of the equipment</param>
260        /// <param name="volume">The volume of the equipment</param>
261        public static void Modify(CustomEquipmentEntities customEquipmentEntities, string tankID, string description, string parentEquipmentID, decimal? volume)
262        {
263            Equipment equipment = customEquipmentEntities.Equipments.Where(equipmentRow => equipmentRow.EquipmentID == tankID).First();
264            Tank tank = Load(customEquipmentEntities, tankID);
265
266            tank.Description = description;
267
268            if (parentEquipmentID != null)
269            {
270                equipment.ParentEquipment = customEquipmentEntities.Equipments.Where(equipmentRow => equipmentRow.EquipmentID == parentEquipmentID).First();
271            }
272            else
273            {
274                equipment.ParentEquipment = null;
275            }
276            
277            tank.ParentEquipmentID = equipment.ParentEquipment.EquipmentID;
278
279            if (volume != null)
280            {
281                tank.Volume = volume;
282            }
283            else
284            {
285                tank.Volume = null;
286            }
287
288            using (TransactionScope transactionScope = new TransactionScope())
289            {
290                tank.OnBeforeModify();
291                customEquipmentEntities.SaveChanges();
292                tank.OnAfterModify();
293                transactionScope.Complete();
294            }
295        }
296        
297        /// <summary>
298        /// Delete the selected equipment
299        /// </summary>
300        /// <param name="tankID">The tank id of the equipment</param>
301        public static void Delete(string tankID)
302        {
303            CustomEquipmentEntities customEquipmentEntities = CustomEquipmentEntities.CreateCustomEquipmentEntities();
304            Delete(customEquipmentEntities, tankID);
305        }
306
307        /// <summary>
308        /// Delete the selected equipment
309        /// </summary>
310        /// <param name="customEquipmentEntities">The equipment entities</param>
311        /// <param name="tankID">The tank id of the equipment</param>
312        public static void Delete(CustomEquipmentEntities customEquipmentEntities, string tankID)
313        {
314            Equipment equipment = customEquipmentEntities.Equipments.Where(equipmentRow => equipmentRow.EquipmentID == tankID).First();
315            Tank tank = Load(customEquipmentEntities, tankID);
316
317            using (TransactionScope transactionScope = new TransactionScope())
318            {
319                tank.OnBeforeDelete();
320                customEquipmentEntities.DeleteObject(tank);
321                customEquipmentEntities.DeleteObject(equipment);
322                customEquipmentEntities.SaveChanges();
323                tank.OnAfterDelete();
324                transactionScope.Complete();
325            }
326        }
327
328        #endregion
329
330        #region Partial methods
331
332        partial void OnBeforeAdd();
333        partial void OnAfterAdd();
334        partial void OnBeforeModify();
335        partial void OnAfterModify();
336        partial void OnBeforeDelete();
337        partial void OnAfterDelete();
338
339        #endregion
340
341        #region Exists method
342
343        /// <summary>
344        /// Check if equipment row by primary key exists
345        /// </summary>
346        /// <param name="tankID">The tank id of the equipment</param>
347        /// <returns>Return true if row exists</returns>
348        public static bool Exists(string tankID)
349        {
350            CustomEquipmentEntities customEquipmentEntities = CustomEquipmentEntities.CreateCustomEquipmentEntities();
351            return Exists(customEquipmentEntities, tankID);
352        }
353
354        /// <summary>
355        /// Check if equipment row by primary key exists
356        /// </summary>
357        /// <param name="customEquipmentEntities">The equipment entities</param>
358        /// <param name="tankID">The tank id of the equipment</param>
359        /// <returns>Return true if row exists</returns>
360        public static bool Exists(CustomEquipmentEntities customEquipmentEntities, string tankID)
361        {
362            ObjectQuery<Tank> tanks = customEquipmentEntities.Tanks.Where("it.TankID = @TankID"new ObjectParameter[] {
363                    new ObjectParameter("TankID", tankID)});
364
365            return tanks.Count() > 0;
366        }
367
368        #endregion
369
370        #region Load
371
372        /// <summary>
373        /// Get selected tank object by primary key
374        /// </summary>
375        /// <param name="tankID">The tank id of the equipment</param>
376        /// <returns>Return Tank object identified by PK</returns>
377        public static Tank Load(string tankID)
378        {
379            return GetTankByTankID(tankID).First();
380        }
381
382        /// <summary>
383        /// Get selected tank object by primary key
384        /// </summary>
385        /// <param name="customEquipmentEntities">The equipment entities</param>
386        /// <param name="tankID">The tank id of the equipment</param>
387        /// <returns>Return Tank object identified by PK</returns>
388        public static Tank Load(CustomEquipmentEntities customEquipmentEntities, string tankID)
389        {
390            return GetTankByTankID(customEquipmentEntities, tankID).First();
391        }
392
393        #endregion
394
395        #region Get methods
396
397        /// <summary>
398        /// Get selected equipment row by primary key
399        /// </summary>
400        /// <param name="tankID">The tank id of the equipment</param>
401        /// <returns>Return equipment row identified by PK</returns>
402        public static IQueryable<Tank> GetTankByTankID(string tankID)
403        {
404            CustomEquipmentEntities customEquipmentEntities = CustomEquipmentEntities.CreateCustomEquipmentEntities();
405            return GetTankByTankID(customEquipmentEntities, tankID);
406        }
407
408        /// <summary>
409        /// Get selected equipment row by primary key
410        /// </summary>
411        /// <param name="customEquipmentEntities">The equipment entities</param>
412        /// <param name="tankID">The tank id of the equipment</param>
413        /// <returns>Return equipment row identified by PK</returns>
414        public static IQueryable<Tank> GetTankByTankID(CustomEquipmentEntities customEquipmentEntities, string tankID)
415        {
416            var query = GetTanks(customEquipmentEntities);
417            query = query.Where(tank => tank.TankID == tankID);
418            IQueryable<Tank> countResult = query;
419            if (countResult.Count() == 0)
420            {
421                throw new Exception(ErrorResource.EquipmentNotFound);
422            }
423
424            return query;
425        }
426
427        /// <summary>
428        /// Get all equipment rows
429        /// </summary>
430        /// <returns>Return an IQueryable with all equipment rows</returns>
431        public static IQueryable<Tank> GetTanks()
432        {
433            CustomEquipmentEntities customEquipmentEntities = CustomEquipmentEntities.CreateCustomEquipmentEntities();
434            return GetTanks(customEquipmentEntities);
435        }
436
437        /// <summary>
438        /// Get all equipment rows
439        /// </summary>
440        /// <param name="customEquipmentEntities">The equipment entities</param>
441        /// <returns>Return an IQueryable with all equipment rows</returns>
442        public static IQueryable<Tank> GetTanks(CustomEquipmentEntities customEquipmentEntities)
443        {
444            var query = from tank in customEquipmentEntities.Tanks.Include("Equipment")
445                        select tank;
446
447            return query;
448        }
449
450        /// <summary>
451        /// Get sorted equipment rows
452        /// </summary>
453        /// <param name="sortExpression">Sort expression</param>
454        /// <returns>Return an IQueryable with all sorted equipment rows</returns>
455        public static IQueryable<Tank> GetTanks(string sortExpression)
456        {
457            var query = GetTanks();
458
459            if (sortExpression == "")
460            {
461                sortExpression = "TankID";
462            }
463
464            var parameter = Expression.Parameter(typeof(Equipment), "tank");
465
466            if (sortExpression.IndexOf(" DESC") < 0)
467            {
468                return query.OrderBy(sortExpression);
469            }
470            else
471            {
472                sortExpression = sortExpression.Replace(" DESC""");
473                return query.OrderByDescending(sortExpression);
474            }
475        }
476
477        /// <summary>
478        /// Get the specified number of sorted equipment rows starting from the given index
479        /// </summary>
480        /// <param name="sortExpression">Sort expression</param>
481        /// <param name="maximumRows">Maximun number of rows</param>
482        /// <param name="startRowIndex">Start row index</param>
483        /// <returns>Return an IQueryable with the specified number of sorted equipment rows starting from the given index</returns>
484        public static IQueryable<Tank> GetTanks(string sortExpression, int maximumRows, int startRowIndex)
485        {
486            if ((maximumRows > 0) && (startRowIndex > 0))
487            {
488                return GetTanks(sortExpression).Skip(startRowIndex).Take(maximumRows);
489            }
490            else if (startRowIndex > 0)
491            {
492                return GetTanks(sortExpression).Skip(startRowIndex);
493            }
494            else if (maximumRows > 0)
495            {
496                return GetTanks(sortExpression).Take(maximumRows);
497            }
498            else
499            {
500                return GetTanks(sortExpression);
501            }
502        }
503
504        #endregion
505
506        #region Count method
507
508        /// <summary>
509        /// Count all rows
510        /// </summary>
511        /// <returns>Number of tank rows</returns>
512        public static int Count()
513        {
514            return GetTanks().Count();
515        }
516
517        #endregion
518
519        #region Extended methods
520
521        /// <summary>
522        /// Get equipment by parentEquipmentID
523        /// </summary>
524        /// <param name="parentEquipmentID">The parent equipment id of the equipment</param>
525        /// <returns>Return an IQueryable with all filtered equipment rows</returns>
526        public static IQueryable<Tank> GetTanksByParentEquipmentID(string parentEquipmentID)
527        {
528            CustomEquipmentEntities customEquipmentEntities = CustomEquipmentEntities.CreateCustomEquipmentEntities();
529            var query = from tank in customEquipmentEntities.Tanks.Include("Equipment")
530                        join equipment in customEquipmentEntities.Equipments.Include("ParentEquipment")
531                        on tank.Equipment.EquipmentID equals equipment.EquipmentID
532                        where equipment.ParentEquipment.EquipmentID == parentEquipmentID
533                        select tank;
534
535            return query;
536        }
537
538        /// <summary>
539        /// Get sorted equipment rows by parentEquipmentID
540        /// </summary>
541        /// <param name="sortExpression">Sort expression</param>
542        /// <param name="parentEquipmentID">The parent equipment id of the equipment</param>
543        /// <returns>Return an IQueryable with all sorted and filtered equipment rows</returns>
544        public static IQueryable<Tank> GetTanksByParentEquipmentID(string sortExpression, string parentEquipmentID)
545        {
546            var query = GetTanksByParentEquipmentID(parentEquipmentID);
547            if (sortExpression == "")
548            {
549                sortExpression = "TankID";
550            }
551            var parameter = Expression.Parameter(typeof(Equipment), "tank");
552            if (sortExpression.IndexOf(" DESC") < 0)
553            {
554                return query.OrderBy(sortExpression);
555            }
556            else
557            {
558                sortExpression = sortExpression.Replace(" DESC""");
559                return query.OrderByDescending(sortExpression);
560            }
561        }
562
563        /// <summary>
564        /// Get the specified number of sorted equipment rows starting from the given index by parentEquipmentID
565        /// </summary>
566        /// <param name="sortExpression">Sort expression</param>
567        /// <param name="maximumRows">Maximun number of rows</param>
568        /// <param name="startRowIndex">Start row index</param>
569        /// <param name="parentEquipmentID">The parent equipment id of the equipment</param>
570        /// <returns>Return an IQueryable with the specified number of sorted and filtered equipment rows starting from the given index</returns>
571        public static IQueryable<Tank> GetTanksByParentEquipmentID(string sortExpression, int maximumRows, int startRowIndex, string parentEquipmentID)
572        {
573            var query = GetTanksByParentEquipmentID(sortExpression, parentEquipmentID);
574            if ((maximumRows > 0) && (startRowIndex > 0))
575            {
576                return query.Skip(startRowIndex).Take(maximumRows);
577            }
578            else if (startRowIndex > 0)
579            {
580                return query.Skip(startRowIndex);
581            }
582            else if (maximumRows > 0)
583            {
584                return query.Take(maximumRows);
585            }
586            else
587            {
588                return query;
589            }
590        }
591
592        /// <summary>
593        /// Count all rows
594        /// </summary>
595        /// <param name="parentEquipmentID">The parent equipment id of the equipment</param>
596        /// <returns>Number of equipment rows</returns>
597        public static int CountEquipmentsByParentEquipmentID(string parentEquipmentID)
598        {
599            return GetTanksByParentEquipmentID(parentEquipmentID).Count();
600        }
601
602        #endregion
603
604        #endregion
605    }
606}
607

Next step is Using partial class

 

Mdf - Mes development framework

Mdf - Mes development framework

Mdf - MES Development Framework has been defined from experienced developers to respond to both customers and developers needs.

Write complex Manufacturing Execution Systems fully ISA-95 compliant will never be easier using c# or VB.NET with LINQ to Entity technology, SQL Server 2005/2008 as storage layer, ASP.NET 3.5 with extensive use of Ajax and ASP.NET MVC for user interface, OPC to communicate with PLCs and field devices and Windows Workflow Foundation for plant business rules.

Download the Mdf latest version here.

Mdf Demo