RSCG- part 14 – DP_Decorator

 

 

name AutoInterface
nuget

https://www.nuget.org/packages/BeaKona.AutoInterfaceGenerator

link https://github.com/beakona/AutoInterface
author beakona

Implement the Design Pattern Decorator. Based on template – you can modify the source code generated
 

The code that you start with is

01
02
03
04
05
06
07
08
09
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
public interface ICoffee                                                                                              
 
{
 
    public int Price { get; }
 
    public string Description { get; }
 
}
 
 
 
public class SimpleCoffee : ICoffee
 
{
 
    public SimpleCoffee()
 
    {
 
        Price = 3;
 
        Description = "Simple Coffee";
 
    }
 
    public int Price { get; set; }
 
    public string Description { get; set; }
 
 
 
public partial class MilkDecorator : ICoffee
 
{
 
    [BeaKona.AutoInterface(TemplateLanguage = "scriban", TemplateBody = SimpleCoffee.TemplateCoffeeDecorator)]
 
    private readonly ICoffee coffee;
 
 
 
    public int DecoratorPrice { get; set; } = 1;
 
    public MilkDecorator(ICoffee coffee)
 
    {
 
        this.coffee = coffee;
 
    }
 
 
 
 
 
 
 
}
 
 
 
public partial class ChocoDecorator : ICoffee
 
{
 
    [BeaKona.AutoInterface(TemplateLanguage = "scriban", TemplateBody = SimpleCoffee.TemplateCoffeeDecorator)]
 
    private readonly ICoffee coffee;
 
 
 
    public int DecoratorPrice { get; set; } = 2;
 
    public ChocoDecorator(ICoffee coffee)
 
    {
 
        this.coffee = coffee;
 
    }
 
 
 
 
 
}

The code that you will use is

01
02
03
04
05
06
07
08
09
10
11
SimpleCoffee s = new SimpleCoffee();
 
Console.WriteLine(s.Description +" with Price "+ s.Price);
 
ICoffee withMilk = new MilkDecorator(s);
 
Console.WriteLine(withMilk.Description} +" with Price "+ withMilk.Price);
 
ICoffee withMilkAndChoco = new ChocoDecorator(withMilk);
 
Console.WriteLine(withMilkAndChoco.Description +" with Price "+ withMilkAndChoco.Price);

 

The code that is generated is

01
02
03
04
05
06
07
08
09
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
partial class MilkDecorator                                                       
 
{
 
    int ICoffee.Price
 
    {
 
        get
 
        {
 
                return ((ICoffee)this.coffee).Price + DecoratorPrice;
 
        }
 
    }
 
 
 
    string ICoffee.Description
 
    {
 
        get
 
        {
                var name = this.GetType().Name.Replace("Decorator","");
 
                return ((ICoffee)this.coffee).Description + " with " + name;
 
        }
 
    }
 
}

Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/DP_Decorator

All RSCG

NrBlog Post
1RSCG–part 1
2RSCG- AppVersion–part 2
3http://msprogrammer.serviciipeweb.ro/2021/02/17/rsgc-enum-part-3/
4RSGC-JSON to Class- part 4
5RSGC-Constructor – Deconstructor – part 5
6RSGC – DTO Mapper – part 6
7RSGC – Skinny Controllers- part 7
8RSGC-Builder Design Pattern – part 8
9RSGC- MetadataFromObject – part 9
10RSGC- Dynamic Mock – part 10
11RSCG- Method Decorator – part 11
12RSCG – Curry – Partial function – part 12
13RSCG- part 13 – IFormattable
14RSCG- part 14 – DP_Decorator
15RSCG- part 15 – Expression Generator
16RSCG- part 16 – Many Others
17RSCG- the book
18RSCG–Template Rendering- part 17
19CI Version
20HttpClientGenerator
21Query from database
22AutoRegister
23TinyTypes
24Static2Interface
25AppSettings
26Properties
27
Roslyn Source Code Generators