Variable duplicate as static and non-static [closed]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
So I have implemented an application that acts as a chatbot for telegram in C#. I use a library that allows to set a filter of updates that the application whishes to receive. I assume the filter makes it more performant. This filter is passed in the StartReceiving
method. My bot consists of multiple modules and each of these may require different types of updates. So I have in each module a field public IImmutableSet<UpdateType> UpdateTypeFilter
. When calling the StartReceiving
function at the start of the application I union the sets of all modules to see what kind of updates I need to receive and then pass that.
//need to convert back to mutable set
ISet updateTypes = Module1.UpdateTypeFilter.ToHashSet();
//NOTE: UpdateTypeFilter is static
updateTypes.Union(Module2.UpdateTypeFilter);
telegramClient.StartReceiving(updateTypes);
This requires a static field since at this point the modules have not yet been created as the bot is not part of a chat yet. The modules are created when the bot is added to a chat:
void OnAddedToChat(Chat chat)
IList<Module> modules = new List()
new Module1(),
new Module2()
;
//chats is of type Dictionary<long, IList<Module>>
chats.Add(chat.Id, modules);
Then when actually receiving updates I filter for each module according to which update type they wish to receive.
void OnMessageReceived(Message message)
IList<Module> modules = chats.GetValueOrDefault(message.Chat.Id);
if(modules == null)
//The bot has just been added to the chat.
OnAddedToChat(message.Chat);
return;
foreach(Module module in modules)
//Note UpdateTypeFilter cannot be static
if(module.UpdateTypeFilter.Contains(message.Type)
module.ProcessMessage(message);
As already commented in the code once the UpdateTypeFilter
has to be static and once not. How can I solve this nicely? So far I just have two variables where one is called 'UpdateTypeFilterStatic' but that is not nice. I could move the filtering to the modules themselves. Then I can use the static variable both times but then it would be duplicate code in each module since I cannot generalize it into the interface. And duplicate code is not nice either.
Any ideas how to make it nicer are welcome :)
c#
closed as off-topic by t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo Jun 20 at 10:31
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." â t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo
add a comment |Â
up vote
3
down vote
favorite
So I have implemented an application that acts as a chatbot for telegram in C#. I use a library that allows to set a filter of updates that the application whishes to receive. I assume the filter makes it more performant. This filter is passed in the StartReceiving
method. My bot consists of multiple modules and each of these may require different types of updates. So I have in each module a field public IImmutableSet<UpdateType> UpdateTypeFilter
. When calling the StartReceiving
function at the start of the application I union the sets of all modules to see what kind of updates I need to receive and then pass that.
//need to convert back to mutable set
ISet updateTypes = Module1.UpdateTypeFilter.ToHashSet();
//NOTE: UpdateTypeFilter is static
updateTypes.Union(Module2.UpdateTypeFilter);
telegramClient.StartReceiving(updateTypes);
This requires a static field since at this point the modules have not yet been created as the bot is not part of a chat yet. The modules are created when the bot is added to a chat:
void OnAddedToChat(Chat chat)
IList<Module> modules = new List()
new Module1(),
new Module2()
;
//chats is of type Dictionary<long, IList<Module>>
chats.Add(chat.Id, modules);
Then when actually receiving updates I filter for each module according to which update type they wish to receive.
void OnMessageReceived(Message message)
IList<Module> modules = chats.GetValueOrDefault(message.Chat.Id);
if(modules == null)
//The bot has just been added to the chat.
OnAddedToChat(message.Chat);
return;
foreach(Module module in modules)
//Note UpdateTypeFilter cannot be static
if(module.UpdateTypeFilter.Contains(message.Type)
module.ProcessMessage(message);
As already commented in the code once the UpdateTypeFilter
has to be static and once not. How can I solve this nicely? So far I just have two variables where one is called 'UpdateTypeFilterStatic' but that is not nice. I could move the filtering to the modules themselves. Then I can use the static variable both times but then it would be duplicate code in each module since I cannot generalize it into the interface. And duplicate code is not nice either.
Any ideas how to make it nicer are welcome :)
c#
closed as off-topic by t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo Jun 20 at 10:31
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." â t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo
3
There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
â t3chb0t
Jun 20 at 6:12
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
So I have implemented an application that acts as a chatbot for telegram in C#. I use a library that allows to set a filter of updates that the application whishes to receive. I assume the filter makes it more performant. This filter is passed in the StartReceiving
method. My bot consists of multiple modules and each of these may require different types of updates. So I have in each module a field public IImmutableSet<UpdateType> UpdateTypeFilter
. When calling the StartReceiving
function at the start of the application I union the sets of all modules to see what kind of updates I need to receive and then pass that.
//need to convert back to mutable set
ISet updateTypes = Module1.UpdateTypeFilter.ToHashSet();
//NOTE: UpdateTypeFilter is static
updateTypes.Union(Module2.UpdateTypeFilter);
telegramClient.StartReceiving(updateTypes);
This requires a static field since at this point the modules have not yet been created as the bot is not part of a chat yet. The modules are created when the bot is added to a chat:
void OnAddedToChat(Chat chat)
IList<Module> modules = new List()
new Module1(),
new Module2()
;
//chats is of type Dictionary<long, IList<Module>>
chats.Add(chat.Id, modules);
Then when actually receiving updates I filter for each module according to which update type they wish to receive.
void OnMessageReceived(Message message)
IList<Module> modules = chats.GetValueOrDefault(message.Chat.Id);
if(modules == null)
//The bot has just been added to the chat.
OnAddedToChat(message.Chat);
return;
foreach(Module module in modules)
//Note UpdateTypeFilter cannot be static
if(module.UpdateTypeFilter.Contains(message.Type)
module.ProcessMessage(message);
As already commented in the code once the UpdateTypeFilter
has to be static and once not. How can I solve this nicely? So far I just have two variables where one is called 'UpdateTypeFilterStatic' but that is not nice. I could move the filtering to the modules themselves. Then I can use the static variable both times but then it would be duplicate code in each module since I cannot generalize it into the interface. And duplicate code is not nice either.
Any ideas how to make it nicer are welcome :)
c#
So I have implemented an application that acts as a chatbot for telegram in C#. I use a library that allows to set a filter of updates that the application whishes to receive. I assume the filter makes it more performant. This filter is passed in the StartReceiving
method. My bot consists of multiple modules and each of these may require different types of updates. So I have in each module a field public IImmutableSet<UpdateType> UpdateTypeFilter
. When calling the StartReceiving
function at the start of the application I union the sets of all modules to see what kind of updates I need to receive and then pass that.
//need to convert back to mutable set
ISet updateTypes = Module1.UpdateTypeFilter.ToHashSet();
//NOTE: UpdateTypeFilter is static
updateTypes.Union(Module2.UpdateTypeFilter);
telegramClient.StartReceiving(updateTypes);
This requires a static field since at this point the modules have not yet been created as the bot is not part of a chat yet. The modules are created when the bot is added to a chat:
void OnAddedToChat(Chat chat)
IList<Module> modules = new List()
new Module1(),
new Module2()
;
//chats is of type Dictionary<long, IList<Module>>
chats.Add(chat.Id, modules);
Then when actually receiving updates I filter for each module according to which update type they wish to receive.
void OnMessageReceived(Message message)
IList<Module> modules = chats.GetValueOrDefault(message.Chat.Id);
if(modules == null)
//The bot has just been added to the chat.
OnAddedToChat(message.Chat);
return;
foreach(Module module in modules)
//Note UpdateTypeFilter cannot be static
if(module.UpdateTypeFilter.Contains(message.Type)
module.ProcessMessage(message);
As already commented in the code once the UpdateTypeFilter
has to be static and once not. How can I solve this nicely? So far I just have two variables where one is called 'UpdateTypeFilterStatic' but that is not nice. I could move the filtering to the modules themselves. Then I can use the static variable both times but then it would be duplicate code in each module since I cannot generalize it into the interface. And duplicate code is not nice either.
Any ideas how to make it nicer are welcome :)
c#
asked Jun 19 at 22:30
findusl
1161
1161
closed as off-topic by t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo Jun 20 at 10:31
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." â t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo
closed as off-topic by t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo Jun 20 at 10:31
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." â t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo
3
There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
â t3chb0t
Jun 20 at 6:12
add a comment |Â
3
There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
â t3chb0t
Jun 20 at 6:12
3
3
There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
â t3chb0t
Jun 20 at 6:12
There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
â t3chb0t
Jun 20 at 6:12
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
3
There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
â t3chb0t
Jun 20 at 6:12