简体   繁体   English

C#约定/最佳实践

[英]C# conventions / best practices

I was wondering is constantly reusing namespace names is valid for c# conventions/best practises. 我想知道是否不断重用名称空间名称对c#约定/最佳实践有效。

I am develop most of my programs in Java, and i would have a packet for implementations, eg: 我正在用Java开发大多数程序,并且会有一个实现包,例如:

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. 让我们谈谈com.ajravindiran.jolt.game.items.sql ,这与我的情况最接近。 I current wrote a library that wraps the MySQL Connection/Net into a OODBMS. 我目前编写了一个将MySQL Connection / Net包装到OODBMS中的库。

So i have an interface called ISqlDataObject which has the following members: 所以我有一个名为ISqlDataObject的接口,该接口具有以下成员:

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 ? 所以我想知道是否可以将这些项目的sql版本添加到JoltEnvironment.Game.Items.Sql类的JoltEnvironment.Game.Items.Sql还是应该保留在JoltEnvironment.Game.Items

Thanks in adnvanced, AJ Ravindiran. 在此感谢AJ Ravindiran。

For naming conventions and rules, see MSDN's Framework Guidelines on Names of Namespaces . 有关命名约定和规则,请参见《 MSDN的命名空间框架指南》

That being said, that won't cover this specific issue: 话虽这么说,但这不会涵盖这个特定问题:

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? 所以我想知道是否可以将这些项目的sql版本添加到JoltEnvironment.Game.Items.Sql之类的文件中还是应该保留在JoltEnvironment.Game.Items中?

It is okay to do either, and the most appropriate one depends a bit on your specific needs. 两者都可以,最合适的一个取决于您的特定需求。

If the game items will be used pervasively throughout the game, but the data access will only be used by a small portion, I would probably split it out into its own namespace (though probably not called Sql - I'd probably use Data or DataAccess, since you may eventually want to add non-SQL related information there, too). 如果游戏项目将在整个游戏中被广泛使用,但是数据访问将仅被一小部分使用,我可能会将其拆分成自己的命名空间(尽管可能不称为Sql-我可能会使用Data或DataAccess ,因为您最终可能还会在其中添加与非SQL相关的信息)。

If, however, you'll always use these classes along with the classes in the Items namespace, I'd probably leave them in a single namespace. 但是,如果您总是将这些类与Items命名空间中的类一起使用,那么我可能会将它们放在单个命名空间中。

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. 在您的情况下,如果还有其他一些不使用Sql进行查询的存储机制,则.Sql是合适的。 Maybe it's XML/Xpath. 也许是XML / Xpath。 But if you don't have that, then it seems like the .Sql layer of naming isn't necessary. 但是,如果您没有这个名称,那么似乎不需要.Sql命名层。

At that poiint, though, I'm wondering why you would use {games,users} at the prior level. 但是,在这种情况下,我想知道为什么您会在上一级使用{games,users}。 Feels like the namespace is more naturally 感觉命名空间更自然

JoltEnvironment.Game.Storage JoltEnvironment.Game.Storage

..And the Fully-qualified type names would be ..完全限定的类型名称为

JoltEnvironment.Game.Storage.SqlItem JoltEnvironment.Game.Storage.SqlUser JoltEnvironment.Game.Storage.SqlUser 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. 如果像JoltEnvironment.Game.Items这样的名称空间只有一个或两个类,则似乎应该将其折叠为更高级别的名称空间。

What are you calling SQL Editions? 您在说什么SQL版本? Versions of SQL Server? SQL Server的版本? Or Version of Database Connections? 还是版本的数据库连接? If the later, I would do something like: 如果以后,我会做类似的事情:

JoltEnvironment.Game.Items.DataAccess.SQLServer
JoltEnvironment.Game.Items.DataAccess.MySQL 
JoltEnvironment.Game.Items.DataAccess.Oracle

etc... 等等...

If the former, I thought that ADO.NET would take care of that for you anyway, based on the provider, so everything under the same namespace would be ok. 如果是前者,我认为ADO.NET会根据提供程序为您代劳,因此在相同名称空间下的所有内容都可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM