Search code examples
c#class-designconventions

C# conventions / best practices


I was wondering is constantly reusing namespace names is valid for c# conventions/best practises.

I am develop most of my programs in Java, and i would have a packet for implementations, eg:

com.ajravindiran.jolt.game.items.sql
com.ajravindiran.jolt.game.users.sql
com.ajravindiran.jolt.events.impl
com.ajravindiran.jolt.tasks.impl

Let's talk about com.ajravindiran.jolt.game.items.sql, which is most close my situation. I current wrote a library that wraps the MySQL Connection/Net into a OODBMS.

So i have an interface called ISqlDataObject which has the following members:

bool Insert(SqlDatabaseClient client);
bool Delete(SqlDatabaseClient client);
bool Update(SqlDatabaseClient client);
bool Load(SqlDatabaseClient client);

and used like such:

public class SqlItem : Item, ISqlDataObject
{
    public bool Load(SqlDatabaseClient client)
    {
        client.AddParameter("id", this.Id);
        DataRow row = client.ReadDataRow("SELECT * FROM character_items WHERE item_uid = @id;");
        this.Examine = (string)row["examine_quote"];
        ...
    }

    ...
}

called:

SqlItem item = new SqlItem(int itemid);
GameEngine.Database.Load(item);

Console.WriteLine(item.Examine);

So i was wondering if it's ok to add the sql editions of the items into something like JoltEnvironment.Game.Items.Sql or should i just keep it at JoltEnvironment.Game.Items?

Thanks in adnvanced, AJ Ravindiran.


Solution

  • You're asking about naming conventions, and the answer is, it's really up to you.

    I allow for extra levels of hierarchy in a namespace if there will be multiple implementations. In your case, the .Sql is appropriate if there is some other storage mechanism that doesn't use Sql for queries. Maybe it's XML/Xpath. But if you don't have that, then it seems like the .Sql layer of naming isn't necessary.

    At that poiint, though, I'm wondering why you would use {games,users} at the prior level. Feels like the namespace is more naturally

    JoltEnvironment.Game.Storage

    ..And the Fully-qualified type names would be

    JoltEnvironment.Game.Storage.SqlItem JoltEnvironment.Game.Storage.SqlUser

    and so on.

    If a namespace, like JoltEnvironment.Game.Items, has only one or two classes, it seems like it ought to be collapsed into a higher level namespace.