microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fix/msal-agentic-cache

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Core/Schema/CoreActivityBuilder.cs

166lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4namespace Microsoft.Teams.Core.Schema;
5
6/// <summary>
7/// Provides a fluent API for building CoreActivity instances.
8/// </summary>
9/// <typeparam name="TActivity">The type of activity being built.</typeparam>
10/// <typeparam name="TBuilder">The type of the builder (for fluent method chaining).</typeparam>
11public abstract class CoreActivityBuilder<TActivity, TBuilder>
12 where TActivity : CoreActivity
13 where TBuilder : CoreActivityBuilder<TActivity, TBuilder>
14{
15 /// <summary>
16 /// The activity being built.
17 /// </summary>
18#pragma warning disable CA1051 // Do not declare visible instance fields
19 protected readonly TActivity _activity;
20#pragma warning restore CA1051 // Do not declare visible instance fields
21
22 /// <summary>
23 /// Initializes a new instance of the CoreActivityBuilder class.
24 /// </summary>
25 /// <param name="activity">The activity to build upon.</param>
26 protected CoreActivityBuilder(TActivity activity)
27 {
28 ArgumentNullException.ThrowIfNull(activity);
29 _activity = activity;
30 }
31
32 /// <summary>
33 /// Sets the activity ID.
34 /// </summary>
35 /// <param name="id">The activity ID.</param>
36 /// <returns>The builder instance for chaining.</returns>
37 public TBuilder WithId(string id)
38 {
39 _activity.Id = id;
40 return (TBuilder)this;
41 }
42
43 /// <summary>
44 /// Sets the service URL.
45 /// </summary>
46 /// <param name="serviceUrl">The service URL.</param>
47 /// <returns>The builder instance for chaining.</returns>
48 public TBuilder WithServiceUrl(Uri? serviceUrl)
49 {
50 _activity.ServiceUrl = serviceUrl;
51 return (TBuilder)this;
52 }
53 /// <summary>
54 /// Sets the service URL from a string.
55 /// </summary>
56 /// <param name="serviceUrlString">The service URL as a string.</param>
57 /// <returns>The builder instance for chaining.</returns>
58 public TBuilder WithServiceUrl(string serviceUrlString)
59 {
60 _activity.ServiceUrl = new Uri(serviceUrlString);
61 return (TBuilder)this;
62 }
63
64 /// <summary>
65 /// Sets the channel ID.
66 /// </summary>
67 /// <param name="channelId">The channel ID.</param>
68 /// <returns>The builder instance for chaining.</returns>
69 public TBuilder WithChannelId(string? channelId)
70 {
71 _activity.ChannelId = channelId;
72 return (TBuilder)this;
73 }
74
75 /// <summary>
76 /// Sets the activity type.
77 /// </summary>
78 /// <param name="type">The activity type.</param>
79 /// <returns>The builder instance for chaining.</returns>
80 public TBuilder WithType(string type)
81 {
82 _activity.Type = type;
83 return (TBuilder)this;
84 }
85
86 /// <summary>
87 /// Sets the conversation information.
88 /// </summary>
89 /// <param name="conversation">The conversation information.</param>
90 /// <returns>The builder instance for chaining.</returns>
91 public TBuilder WithConversation(Conversation conversation)
92 {
93 _activity.Conversation = conversation;
94 return (TBuilder)this;
95 }
96
97 /// <summary>
98 /// Sets the sender account information.
99 /// </summary>
100 /// <param name="from">The sender account.</param>
101 /// <returns>The builder instance for chaining.</returns>
102 public TBuilder WithFrom(ConversationAccount? from)
103 {
104 _activity.From = from;
105 return (TBuilder)this;
106 }
107
108 /// <summary>
109 /// Sets the recipient account information.
110 /// </summary>
111 /// <param name="recipient">The recipient account.</param>
112 /// <returns>The builder instance for chaining.</returns>
113 public TBuilder WithRecipient(ConversationAccount? recipient)
114 {
115 _activity.Recipient = recipient;
116 return (TBuilder)this;
117 }
118
119 /// <summary>
120 /// Adds or updates a property in the activity's Properties dictionary.
121 /// </summary>
122 /// <param name="name">Name of the property.</param>
123 /// <param name="value">Value of the property.</param>
124 /// <returns>The builder instance for chaining.</returns>
125 public TBuilder WithProperty<T>(string name, T? value)
126 {
127 _activity.Properties[name] = value;
128 return (TBuilder)this;
129 }
130
131 /// <summary>
132 /// Builds and returns the configured activity instance.
133 /// </summary>
134 /// <returns>The configured activity.</returns>
135 public abstract TActivity Build();
136}
137
138/// <summary>
139/// Provides a fluent API for building CoreActivity instances.
140/// </summary>
141public class CoreActivityBuilder : CoreActivityBuilder<CoreActivity, CoreActivityBuilder>
142{
143 /// <summary>
144 /// Initializes a new instance of the CoreActivityBuilder class.
145 /// </summary>
146 internal CoreActivityBuilder() : base(new CoreActivity())
147 {
148 }
149
150 /// <summary>
151 /// Initializes a new instance of the CoreActivityBuilder class with an existing activity.
152 /// </summary>
153 /// <param name="activity">The activity to build upon.</param>
154 internal CoreActivityBuilder(CoreActivity activity) : base(activity)
155 {
156 }
157
158 /// <summary>
159 /// Builds and returns the configured CoreActivity instance.
160 /// </summary>
161 /// <returns>The configured CoreActivity.</returns>
162 public override CoreActivity Build()
163 {
164 return _activity;
165 }
166}
167