A design problem involving Collections, Generics and Interfaces in C# -


There are lots of code in this post, but I really appreciate it if you read something and understood It ... and hopefully one solution will come with

Assume that I am structuring a network game where the institutions need to draw and some of them accordingly to the server Has been updated. / P>

Drawable is in charge of drawing entities on the square screen:

  class drawable {public int id {get; Set; } // I have inserted the ID instead of any other // class which is obtained from Drawable, as does not make the code of example // more complicated Public Zero Draw () {}}  

The data received from the server implies IData with each code IData . Assume that we have the following data that will receive player and enemy :

  interface IData {int ID {get; Set; }} Class PlayerData: idata {public id id {get; Set; } Public string name {get; Set; }} Category EnemyData: IData {public int id {get; Set; } Public perfection loss {received; Set; }}  

Game organizations that update IUpdateable & lt; T & gt; where T is IData :

  interface IUpdateable & lt; T & gt; Where T: IData {Zero Update (T Data); }  

Our organizations are thus drawable and updateable :

  class enemies: Dryable , IUpdateable & lt; EnemyData & gt; {Public Zune Update (Animated Data) {}} Class Player: Dryable, IUpdateable & lt; PlayerData & gt; {Public Zero Update (Player Data Data) {}}  

Then it is the basic framework of the game.


Now, give me a this dry and updateable objects, drawable Key and a complex object that holds Dryer and updateable objects and their remote concrete IData :

  class datahilder & lt; T, T1 & gt; Where T: Dryable, IUpdateable & lt; T1 & gt; Where T1: IDAT {Public T Unit} {Received; Set;} public IData data {get; Set;} Public Data Holder (T-Institution, IDT data) {entity = institution; Data = data; }}  

As an example, say that I currently have the following entities:

  var p1 = new player (); Var p1Data = new player data (); Var e1 = new enemy (); Var e1Data = new animation (); Var PlayerData = New DataHolder & lt; Player, PlayerData & gt; (P1, P1 data); Var EnemyData = New DataHolder & lt; Enemy, Animadata & gt; (E1, E1 data);  

Now I need to put a dictionary which is a key ( p1.id and E1. ID ) and their datahilder ( player data and enemy data ) and their value.

Some of the following ( the code given below just shows what I want to do and thus it does not compile ):

  Dictionary & lt; Int, DataHolder & gt; List = New Dictionary & lt; Int, DataHolder & gt; (); List.Add (p1.id, playerData); List Add (e1.id, enemy data);     Regarding the use, I should be able to do the following:  
  foreach (var values ​​in list.Values) {Var entity = value.Entity; Entity.Update (value.Data); }  

I have also tried to change the design of the DataHolder for the following:

  class datahilder & lt; T & gt; Where T: Dryable, IUpdateable & lt; IData & gt; {Receive the public T unit { Set;} public IData data {get; Set;} Public Data Holder (T-Institution, IDT data) {entity = institution; Data = data; }}  

Then I tried something like this:

  var playerData = new database holder & lt; Player & gt; (P1, p1 data); // error  

but it throws a compile-time error:

Type 'player' to 'IUpdateable & lt; IData & gt; This is the general category 'DataHolder & lt; T & gt;

What is the reason for using the parameter 'T'? Player implies IUpdateable & lt; PlayerData & gt; and PlayerData implies IData . Is this an issue with variance? And is there any way around it?

Apply your dataholder class or a non-normal class or interface , Then make that (non-normal) type of dictionary.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -