1

I'm working on library that will help implement dedicated servers for all sorts of applications (mainly my goal is games). I'm working with sockets and I want to implement some sort of command system, where users will be able to invoke functions on the server. I have a problem because I wanna let a user implement interactable command environment in a class created by him that my library will need to know about. I created this template example of how it's all structured:

Implemented by me, part of library (very simplified):

public class UserInfo //class containing info about user
{
    public int id;

    public UserInfo(int _id)
    {
        id = _id;
    }
}

public class UserManager
{
    List<UserInfo> userInfos;

    public UserManager(List<UserInfo> _userInfos) //We get out infos from somewhere...
    {
        userInfos = _userInfos; //...and keep reference to them

        //SetupChildClassAndInfos();
    }

    //I'd like to have something like that BUT I don't know 
    //how ChildClass is called so I can't just type it like here

    /*
    List<ChildClass> childs = new List<ChildClass>();

    void SetupChildClassAndInfos()
    {
        foreach (var userInfo in userInfos)
        {
            ChildClass child = new ChildClass();
            child.someInfo = userInfo;
            childs.Add(child);
        }
    }
    */


    //I tried working with generics but failed miserably xd
    List<T> childs = new List<T>();
    public void GetChildClass<T>()
    {
        foreach (var userInfo in userInfos)
        {
            T child = new T();
            child.userInfo = userInfo;
            childs.Add(child);
        }
    }
    //of course it doesn't work and makes no sense xD but I hope you kinda g
    //get what I'm trying to accomplish
}

public class UserClass //Base class for further implementation containing 
                       //userInfo that user needs to know about
{
    public UserInfo userInfo;
}

Example implementation by someone else, I don't know how ChildClass will be called:

public class ChildClass : UserClass //there needs to be access to UserClass informations
{
    CustomManager customManager;

    [Command] //attribute making method be called automaticly when is the right time
    public void Message(string _message) //A method made by a user BUT(!) he will never use it directly
    {
        customManager.ReceiveMessage(userInfo.id, _message);
    }
}
public class CustomManager
{
    UserManager userManager; //assume we somehow have reference

    public CustomManager()
    {
        userManager.GetChildClass<ChildClass>(); //sending information in beginning to infoManager how I implemented my ChildClass (doesn't work)
    }


    public void ReceiveMessage(int _id, string _message)
    {
        Debug.Log("We got message: " + _message + " from user with id: " + _id);
    }
}

So my question is how do I send a custom made class to UserManager and create instances of this class? Maybe what I'm writing just doesn't make any sense and my aproach is stupid, feel free to criticize.

Bizio
  • 45
  • 7

2 Answers2

0

Make CustomManager a generic class

Generic Property in C#

OR

Use dependency injection via implementing the dependency inversion principal using spring.net or Unity framework.

0

What's wrong with having just a dictionary IReadOnlyDictionary<string, object>? Are you concerned with boxing of value types? A glimpse on your code convinces me that it's too early to proceed on more sophisticated solutions. So it makes sense to abstract out this piece into an interface and implement that one in a simplest possible way: with help of dictionaries (a-ka JavaScript way).

Zazaeil
  • 3,900
  • 2
  • 14
  • 31