1-
2- using ConsoleAppFramework ;
3- using Microsoft . EntityFrameworkCore ;
1+ using ConsoleAppFramework ;
42using Microsoft . Extensions . DependencyInjection ;
53using Microsoft . Extensions . Logging ;
6- using Microsoft . Extensions . Options ;
7- using Net6Console ;
8- using ZLogger ;
94
5+ args = new [ ] { "test" } ;
106
7+ var builder = ConsoleApp . CreateBuilder ( args ) ;
8+ builder . ConfigureServices ( x =>
9+ {
10+ x . AddSingleton ( typeof ( ISingletonPublisher < > ) , typeof ( MessageBroker < > ) ) ;
11+ } ) ;
1112
12- var app = ConsoleApp . Create ( args ) ;
13-
14- var a = app . Services ;
15- var b = app . Configuration ;
16- var c = app . Environment ;
17- var d = app . Lifetime ;
18- var e = app . Logger ;
1913
20- app . AddSubCommands < FooBarBaz > ( ) ;
14+ var app = builder . Build ( ) ;
2115
22- //app.AddAllCommandType();
16+ app . AddCommand ( "test" , ( ISingletonPublisher < string > logger ) =>
17+ {
18+ Console . WriteLine ( "OK" ) ;
19+ } ) ;
2320
2421app . Run ( ) ;
2522
26- //args = new[] { "--message", "tako" };
27-
28- public class FooBarBaz : ConsoleAppBase
23+ public interface IPublisher < TMessage >
2924{
30- public void EchoNano ( string msg , int repeat = 3 )
31- {
32- for ( var i = 0 ; i < repeat ; i ++ )
33- {
34- Console . WriteLine ( msg ) ;
35- }
36- }
37-
38- public void Sum ( [ Option ( 0 ) ] int x , [ Option ( 1 ) ] int y )
39- {
40- Console . WriteLine ( ( x + y ) . ToString ( ) ) ;
41- }
25+ void Publish ( TMessage message ) ;
4226}
27+ public interface ISingletonPublisher < TMessage > : IPublisher < TMessage > { }
4328
44-
45- //static int Hello([Option("m")]string message, [Option("e")] bool end, [Option("r")] int repeat = 3)
46- //{
47- // for (int i = 0; i < repeat; i++)
48- // {
49- // Console.WriteLine(message);
50- // }
51- // if (end)
52- // {
53- // Console.WriteLine("END");
54- // }
55- // //throw new OperationCanceledException("hogemoge");
56- // return 999;
57- //}
58-
59- //ConsoleApp.Run(args, Hello);
60-
61- //return;
62-
63-
64- //args = new[] { "help" };
65-
66-
67-
68- //// You can use full feature of Generic Host(same as ASP.NET Core).
69-
70- //var builder = ConsoleApp.CreateBuilder(args);
71- //builder.ConfigureServices((ctx, services) =>
72- //{
73- // // Register EntityFramework database context
74- // services.AddDbContext<MyDbContext>();
75-
76- // // Register appconfig.json to IOption<MyConfig>
77- // services.Configure<MyConfig>(ctx.Configuration);
78-
79- // // Using Cysharp/ZLogger for logging to file
80- // services.AddLogging(logging =>
81- // {
82- // logging.AddZLoggerFile("log.txt");
83- // });
84- //});
85-
86-
87- //var app = builder.Build();
88-
89- //// setup many command, async, short-name/description option, subcommand, DI
90- //app.AddCommand("calc-sum", (int x, int y) => Console.WriteLine(x + y));
91- //app.AddCommand("sleep", async ([Option("t", "seconds of sleep time.")] int time) =>
92- //{
93- // await Task.Delay(TimeSpan.FromSeconds(time));
94- //});
95- //app.AddSubCommand("verb", "childverb", () => Console.WriteLine("called via 'verb childverb'"));
96-
97- //// You can insert all public methods as sub command => db select / db insert
98- //// or AddCommand<T>() all public methods as command => select / insert
99- //app.AddSubCommands<DatabaseApp>();
100-
101- //// some argument from DI.
102- //app.AddRootCommand((ConsoleAppContext ctx, IOptions<MyConfig> config, string name) => { });
103-
104- //app.Run();
105-
106- // ----
107-
108- //[Command("db")]
109- //public class DatabaseApp : ConsoleAppBase, IAsyncDisposable
110- //{
111- // readonly ILogger<DatabaseApp> logger;
112- // readonly MyDbContext dbContext;
113- // readonly IOptions<MyConfig> config;
114-
115- // // you can get DI parameters.
116- // public DatabaseApp(ILogger<DatabaseApp> logger, IOptions<MyConfig> config, MyDbContext dbContext)
117- // {
118- // this.logger = logger;
119- // this.dbContext = dbContext;
120- // this.config = config;
121- // }
122-
123- // [Command("select")]
124- // public async Task QueryAsync(int id)
125- // {
126- // // select * from...
127- // }
128-
129- // // also allow defaultValue.
130- // [Command("insert")]
131- // public async Task InsertAsync(string value, int id = 0)
132- // {
133- // // insert into...
134- // }
135-
136- // // support cleanup(IDisposable/IAsyncDisposable)
137- // public async ValueTask DisposeAsync()
138- // {
139- // await dbContext.DisposeAsync();
140- // }
141- //}
142-
143- //public class MyConfig
144- //{
145- // public string FooValue { get; set; } = default!;
146- // public string BarValue { get; set; } = default!;
147- //}
148-
149-
150- //// System.CommandLine
151- //// Create a root command with some options
152- ////using System.CommandLine;
153-
154- ////var rootCommand = new RootCommand
155- ////{
156- //// new Option<int>(
157- //// "--int-option",
158- //// getDefaultValue: () => 42,
159- //// description: "An option whose argument is parsed as an int"),
160- //// new Option<bool>(
161- //// "--bool-option",
162- //// "An option whose argument is parsed as a bool"),
163- //// new Option<FileInfo>(
164- //// "--file-option",
165- //// "An option whose argument is parsed as a FileInfo")
166- ////};
167-
168- ////rootCommand.Description = "My sample app";
169-
170- ////rootCommand.SetHandler<int, bool, FileInfo>((intOption, boolOption, fileOption) =>
171- ////{
172- //// Console.WriteLine($"The value for --int-option is: {intOption}");
173- //// Console.WriteLine($"The value for --bool-option is: {boolOption}");
174- //// Console.WriteLine($"The value for --file-option is: {fileOption?.FullName ?? "null"}");
175- ////});
176-
177- ////await rootCommand.InvokeAsync(args);
178-
179-
180-
181- ////args = new[] { "check-timeout" };
182-
183- ////var builder = ConsoleApp.CreateBuilder(args);
184- ////builder.ConfigureHostOptions(options =>
185- ////{
186- //// global::System.Console.WriteLine(options.ShutdownTimeout);
187- //// options.ShutdownTimeout = TimeSpan.FromSeconds(10);
188- ////});
189-
190-
191- ////var app = builder.Build();
192-
193-
194- ////app.AddCommands<DisposeMan>();
195-
196- ////app.Run();
197-
198- //// System.CommandLine v2, requires many boilerplate code.
199- ////using System.CommandLine;
200-
201- ////var option = new Option<string>("name");
202- ////var rootCommand = new RootCommand
203- ////{
204- //// option
205- ////};
206-
207- ////rootCommand.SetHandler((string name) =>
208- ////{
209- //// Console.WriteLine($"Hello {name}");
210- ////}, option);
211-
212- ////rootCommand.Invoke(args);
213-
214-
215-
216-
217- ////return;
218-
219- //public class DisposeMan : ConsoleAppBase, IDisposable
220- //{
221- // public void Tako()
222- // {
223- // Console.WriteLine("Tako");
224- // }
225-
226- // public async Task CheckTimeout()
227- // {
228- // await Task.Delay(TimeSpan.FromSeconds(30));
229- // }
230-
231- // public void Dispose()
232- // {
233- // Console.WriteLine("foo!");
234- // }
235- //}
236-
237-
238- ////rootCommand.Handler = CommandHandler.Create<int, bool, FileInfo>((intOption, boolOption, fileOption) =>
239- ////{
240- //// Console.WriteLine($"The value for --int-option is: {intOption}");
241- //// Console.WriteLine($"The value for --bool-option is: {boolOption}");
242- //// Console.WriteLine($"The value for --file-option is: {fileOption?.FullName ?? "null"}");
243- ////});
244-
245- ////// Parse the incoming args and invoke the handler
246- ////return rootCommand.InvokeAsync(args).Result;
247-
248-
249-
250- ////using ConsoleAppFramework;
251- ////using Microsoft.Extensions.Hosting;
252- ////using Microsoft.Extensions.Logging;
253-
254- ////args = new[] { "iroiro-case", "tako", "--help" };
255- //////args = new[] { "console-foo", "hello-anonymous", "--help" };
256- //////args = new[] { "console-foo", "hello-anonymous", "--hyper-name", "takoyaki" };
257-
258- ////var app = ConsoleApp.Create(args);
259-
260- //////app.AddCommand("foo", (ConsoleAppContext context, [Option(0)] int x, ILogger<string> oreLogger, [Option(1)] int y) =>
261- //////{
262- ////// global::System.Console.WriteLine(context.Timestamp);
263- ////// global::System.Console.WriteLine(x + ":" + y);
264- //////});
265- ////app.AddRoutedCommands();
266- ////app.AddDefaultCommand((string foo) => { });
267-
268- //////app.AddCommands<ConsoleFoo>();
269-
270- ////app.Run();
271-
272- ////static class Foo
273- ////{
274- //// public static void Barrier(int x, int y)
275- //// {
276- //// Console.WriteLine($"OK:{x + y}");
277- //// }
278- ////}
279-
280- ////public class ConsoleFoo : ConsoleAppBase
281- ////{
282- //// // [DefaultCommand]
283- //// public void HelloAnonymous(string hyperName)
284- //// {
285- //// Console.WriteLine("OK:" + hyperName);
286- //// }
287-
288- //// public void Hello2([Option("n", "name of send user.")] string name, [Option("r", "repeat count.")] int repeat = 3)
289- //// {
290- //// }
291-
292-
293- ////}
294-
295- ////public class IroiroCase : ConsoleAppBase
296- ////{
297- //// [Command(new[] { "tako", "Yaki", "nanobee", "hatchi" })]
298- //// public void Tes(int I, int i, int ID, int XML, int Xml, int Id, int IdCheck, int IDCheck, int IdCheckZ, int IdCheckXML, int IdCheckXml)
299- //// {
300- //// }
301-
302-
303- //// public void Hot()
304- //// {
305- //// }
306- ////}
29+ public class MessageBroker < TMessage > : IPublisher < TMessage >
30+ {
31+ public void Publish ( TMessage message )
32+ {
33+ throw new NotImplementedException ( ) ;
34+ }
35+ }
36+ public class SingletonMessageBroker < TMessage > : MessageBroker < TMessage >
37+ {
38+ }
0 commit comments