1: public class SoftDeleteModelBuilderExtension : IModelBuilderExtension
2: {
3:
4: public static IDictionary<Type, SoftDeleteModel> SoftDeleteModels = new Dictionary<Type, SoftDeleteModel>();
5:
6: #region IModelBuilderExtension Members
7:
8: public void ProcessBelongsTo(System.Reflection.PropertyInfo pi, BelongsToModel belongsToModel, ActiveRecordModel model)
9: {
10: }
11:
12: public void ProcessClass(Type type, ActiveRecordModel model)
13: {
14: GetModel(type, model);
15: }
16:
17: public void ProcessField(System.Reflection.FieldInfo fi, ActiveRecordModel model)
18: {
19: }
20:
21: public void ProcessHasAndBelongsToMany(System.Reflection.PropertyInfo pi, HasAndBelongsToManyModel hasAndBelongManyModel, ActiveRecordModel model)
22: {
23: if (!IsSoftModel(hasAndBelongManyModel.HasManyAtt.MapType)) return;
24: var softDeleteModel = GetModel(model.Type, model);
25: softDeleteModel.HasAndBelongsToMany.Add(hasAndBelongManyModel);
26: }
27:
28: public void ProcessHasMany(System.Reflection.PropertyInfo pi, HasManyModel hasManyModel, ActiveRecordModel model)
29: {
30: if (!IsSoftModel(hasManyModel.HasManyAtt.MapType)) return;
31: var softDeleteModel = GetModel(model.Type, model);
32: softDeleteModel.HasMany.Add(hasManyModel);
33: }
34:
35: public void ProcessHasManyToAny(System.Reflection.PropertyInfo pi, HasManyToAnyModel hasManyModel, ActiveRecordModel model)
36: {
37: if (!IsSoftModel(hasManyModel.HasManyToAnyAtt.MapType)) return;
38: var softDeleteModel = GetModel(model.Type, model);
39: softDeleteModel.ManyToAny.Add(hasManyModel);
40: }
41:
42: public void ProcessProperty(System.Reflection.PropertyInfo pi, ActiveRecordModel model)
43: {
44: }
45:
46: private static bool IsSoftModel(Type type)
47: {
48: return (typeof(ISoftDeleteable).IsAssignableFrom(type));
49: }
50:
51: private static SoftDeleteModel GetModel(Type type, ActiveRecordModel model)
52: {
53: SoftDeleteModel softDeleteModel;
54: SoftDeleteModels.TryGetValue(type, out softDeleteModel);
55:
56: if (softDeleteModel == null)
57: {
58: softDeleteModel = new SoftDeleteModel {Type = type, ActiveRecordModel = model};
59: if (IsSoftModel(type))
60: softDeleteModel.SoftDeleteable = true;
61: SoftDeleteModels[type] = softDeleteModel;
62: }
63: return softDeleteModel;
64: }
65:
66: #endregion
67: }