songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace PalGain.Core
{
    [Serializable]
    public abstract class BaseEntity
    {
        /// <summary>
        /// Gets or sets the entity identifier
        /// </summary> 
        public int Id { get; set; }  
        //public DateTime? SaveTime { get; set; }
        //public string EntityRemark { get; set; }
 
        public T TransferFrom<T>() where T : BaseEntity
        {
            return TransferFrom<T>((T)this, new string[] { "" });
        }
        public T TransferFrom<T>(string[] ignoreItems) where T : BaseEntity
        {
            return TransferFrom<T>((T)this, ignoreItems);
        }
        public T TransferFrom<T>(T source)
        {
            return TransferFrom<T>(source, new string[] { "" });
        }
        public T TransferFrom<T>(T source, string[] ignoreItems)
        {
            T t = Activator.CreateInstance<T>();
            return TransferFrom<T>(t, source, ignoreItems);
        }
        public T TransferFrom<T>(T target, T source)
        {
            return TransferFrom<T>(target, source, new string[] { "" });
        }
        public virtual T TransferFrom<T>(T target, T source, string[] ignoreItems)
        {
            BatchService.Framework.Utility.ClassValueCopier.Mapper<T, T>
                (target, source, ignoreItems);
            return target;
        }
 
        public override bool Equals(object obj)
        {
            return Equals(obj as BaseEntity);
        }
 
        private static bool IsTransient(BaseEntity obj)
        {
            return obj != null && Equals(obj.Id, default(int));
        }
 
        public virtual T GetEntity<T>() where T : BaseEntity
        {
            return default(T);
        }
 
        private Type GetUnproxiedType()
        {
            return GetType();
        }
 
        public virtual bool Equals(BaseEntity other)
        {
            if (other == null)
                return false;
 
            if (ReferenceEquals(this, other))
                return true;
 
            if (!IsTransient(this) &&
                !IsTransient(other) &&
                Equals(Id, other.Id))
            {
                var otherType = other.GetUnproxiedType();
                var thisType = GetUnproxiedType();
                return thisType.IsAssignableFrom(otherType) ||
                        otherType.IsAssignableFrom(thisType);
            }
 
            return false;
        }
 
        public override int GetHashCode()
        {
            if (Equals(Id, default(int)))
                return base.GetHashCode();
            return Id.GetHashCode();
        }
 
        public static bool operator ==(BaseEntity x, BaseEntity y)
        {
            return Equals(x, y);
        }
 
        public static bool operator !=(BaseEntity x, BaseEntity y)
        {
            return !(x == y);
        }
        public void Save<T>(IRespository<T> respostitory, string[] ignoreProperites) where T : BaseEntity
        {
            if (this.Id == 0)
            {
                respostitory.Insert((T)this);
            }
            else
            {
                var exist = respostitory.GetById(this.Id);
                BatchService.Framework.Utility.ClassValueCopier.Mapper<T, T>((T)exist, (T)this, ignoreProperites);
                respostitory.Update(exist);
            }
        }
 
        public void Save<T>(IRespository<T> respostitory) where T : BaseEntity
        {
            this.Save<T>(respostitory, new string[] { "" });
        }
    }
}