Bonjour,
J'ai une question sur l'organisation à adopter niveau code pour gérer plusieurs version d'un objet.
Un peu plus d'explications :
J'ai aujourd'hui un objet Example_V1 branché à une base de données et accessible via webservice.
Aujourd'hui je crée l'application donc pas de problème particulier mais j'aimerai pouvoir faire évoluer mon objet Example_V1 en Example_V2 côté serveur de base de données mais l'application se connectant au webservice ne connait que l'objet Example_V1 et n'est pas forcément appelée à évoluer en même temps que sur le serveur !!! Plusieurs version seraient ainsi amenées à cohabiter.
Comment organiser mon code aujourd'hui pour préparer cette évolution ?
Merci
Un petit code de ce que j'ai fait pour le moment :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 namespace DAL.Persistence.Example.LastVersion { using OldVersions; public class ExampleIdentityB_V1 { } public interface IExampleB_V2 : IExampleB_V1 { bool TestExampleValid { get; set; } } public class ExampleB_V2 : ExampleB_V1 , IExampleB_V2 { public bool TestExampleValid { get; set; } } } namespace DAL.Persistence.Example.OldVersions { public interface IExampleB_V1 : IExampleIdentityB_V1 { int TestExample { get; set; } } public class ExampleB_V1 : ExampleIdentityB_V1 , IExampleB_V1 { public int TestExample { get; set; } } } namespace DAL.Proxy { public class Example : IExample_V1 , IExample_V2 { private readonly IExample_V2 _example; // Initial properties in V1 public int TestExample { get => _example.TestExample; set => _example.TestExample = value; } // Added properties in V2 public bool TestExampleValid { get => _example.TestExampleValid; set => _example.TestExampleValid = value; } #region Instanciate proxy public Example(IExample_V2 example) => _example = example; //Old public Example(IExample_V1 example) => _example = example; public Example(IExample_V1 example) => _example = ProxyTool.ConvertTo<Example_V2>(example); #endregion } } namespace DAL { using System; using System.Reflection; public static class ProxyTool { /// <summary> /// Convert same type and name properies of an instancied class to another instancied (or not) class /// If the second class is instancied, the first class overload data of the second for the same type and name properties /// Is the second class is not instancied (or not given), it will be new instanciation /// </summary> /// <typeparam name="T">Final class</typeparam> /// <param name="from">Instancied class to convert</param> /// <param name="linkObjects">True to link all objects, False to do not, only string and primitives properties will be copied</param> /// <param name="to">Instancied (or not) class to receive from object</param> /// <returns>Return instancied class with copy of source class for the same type and name properties</returns> public static T ConvertTo<T>(object from, bool linkObjects = false, T to = default(T)) where T : new() { if (from == null) return to; Type type = typeof(T); T result; result = to?.Equals(default(T)) == false ? to : new T(); foreach (PropertyInfo piSource in from.GetType().GetProperties()) { PropertyInfo piDestination = type.GetProperty(piSource.Name); if (piDestination == null) continue; if ((piSource.PropertyType.IsPrimitive || piSource.PropertyType.Name.ToLower() == string.Empty.GetType().Name.ToLower()) && piSource.PropertyType == piDestination.PropertyType) piDestination.SetValue(result, piSource.GetValue(from)); } return result; } } }
Partager