EntityAutomaticValues.cs
1  using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
5 using System.Text;
6 
7 namespace Sage.ES.S50.DataAccess.Models
8 {
9  internal enum Property
10  {
11  GUID_ID,
12  CREATED,
13  MODIFIED
14  }
15  internal class EntityAutomaticValues
16  {
17 
18  #region Members
19  object _entity = null;
20  #endregion
21 
22  #region Constructors
23  public EntityAutomaticValues(object obj)
24  {
25  _entity = obj;
26  }
27 
28  public void SetValue(Property property)
29  {
30  PropertyInfo prop = _entity.GetType().GetProperty(property.ToString());
31  switch (property)
32  {
33  case Property.GUID_ID:
34 
35  if (prop != null && string.IsNullOrWhiteSpace(Convert.ToString(prop.GetValue(_entity, null))))
36  {
37  prop.SetValue(_entity, Guid.NewGuid().ToString(), null);
38  }
39  break;
40  case Property.MODIFIED:
41  case Property.CREATED:
42  if (prop != null)
43  {
44  prop.SetValue(_entity, DateTime.Now, null);
45  }
46  break;
47  default:
48  break;
49  }
50  }
51  public object GetValue(Property property)
52  {
53  object value = null;
54  PropertyInfo prop = _entity.GetType().GetProperty(property.ToString());
55  if (prop != null)
56  {
57  value = prop.GetValue(_entity, null);
58  }
59  return value;
60  }
61  #endregion
62  }
63 }