Question

I have an architectural situation that I wanted to get an opinion on.

I have a poker system that I am developing. Currently I have many different types of games currently handled by different classes (with a common interface of course).

Games include Texas Hold 'Em, Omaha Hi, Omaha Hi/Lo, 7-Card Stud, 7-Card razz, London Lowball, and Follow the Queen. I expect to have more in the future.

Currently, I am using a Provider Model, similar to SQL connection strings. Deal is, all of the type names of the games are located in the app.config file.

I was thinking about putting the types in the database so the config file does not need to be touched. Then when I develop a new game, an installer can just copy a new DLL and a script to insert the game's type in the DB can be called.

Is there a better way to dynamically allow this type of dependency?

Was it helpful?

Solution

Perhaps you can even do this without needing to update the database. Since you put one game in one DLL, why not have the game's name in the DLLs metadata. For instance, you can have the game's name as an attribute on the class:

[Game("Texas Hold Em")]
public class TexasHoldEm : Game
{
}

Loading the game plugins can be done as follows:

string pluginDirectory =
    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
        "Plugins");

var pluginAssemblies =
    from file in new DirectoryInfo(pluginDirectory).GetFiles()
    where file.Extension == ".dll"
    select Assembly.LoadFile(file.FullName);

var gameTypes =
    from dll in pluginAssemblies
    from type in dll.GetExportedTypes()
    where typeof(Game).IsAssignableFrom(type)
    where !type.IsAbstract
    where !type.IsGenericTypeDefinition
    select type;

If you use a dependency injection container, you can often easily register those gameTypes in the container. However, you probably need some sort of factory anyway to get the correct game:

public interface IGameFactory
{
    IEnumerable<Game> GetGames();

    Game GetGame(string name);
}

OTHER TIPS

I think this is a very solid way of solving your issue. Is there something specific you don't like about storing class names in the database?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top