是什么在数据访问项目的命名约定上课? [英] What's the naming convention for classes in the DataAccess Project?

查看:177
本文介绍了是什么在数据访问项目的命名约定上课?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常是由类的业务项目为Manager.cs名字一样BaseManager.cs,CommentsManager.cs,ProfileManager.cs,等等...

I usually name by classes in the Business project as Manager.cs, like BaseManager.cs, CommentsManager.cs, ProfileManager.cs, etc...

你如何命名你的类中的数据访问的项目?你叫它CommentsDA,CommentsDB,还是什么?

How do you name your classes in the DataAccess project? Do you call it CommentsDA, CommentsDB, or what?

只是好奇......顺便说一句,我使用.NET C#。

Just curious...BTW, I'm using .NET C#.

推荐答案

我用单独的类的命名规则为每一种软件层的就像你问。然而,在.NET世界中时实现每一层都它自己的组装和装配经常更换与执行相同的接口/ s的组件,我认为是最重要的/有效的改变了命名空间,回落类特定prefixes和后缀,在大多数情况下

Naming Conventions between Software Layers

I used to separate class naming conventions for each kind of software layer like you're asking about. However in the .NET world upon realizing each layer is it's own assembly, and assemblies are often replaceable with assemblies implementing same interface/s, I found the namespace to be the most important/effective change, and dropped class-specific prefixes and suffixes, for the most part.

例如,我曾经有过客户(企业)和 CustomerDAL (数据访问层)

For example, I used to have Customer (business) and CustomerDAL (data access layer)

..并有经常的修改在我最近的项目,分别为...

.. and that has often changed in my recent projects to, respectively ...

Com.Example.ProjectName.Business.Customer Com.Example.ProjectName.Data.Customer ICustomer 它们之间的接口,而不是直接访问任何特定类目标的项目。

Com.Example.ProjectName.Business.Customer and Com.Example.ProjectName.Data.Customer with an ICustomer interface being used between them instead of directly accessing any specific class in the target project.

通常情况下,通过实施类后缀或preFIX 你想prevent对命名冲突紧耦合组件之间。

Usually, by implementing a class suffix or prefix you're trying to prevent against naming conflicts between tightly coupled assemblies.

但它通常是建议有松散耦合的组件按方式接口;一个副作用是,你不要用具体的类名直接了。否则,通过使用紧密耦合的组件,你还不如将它们组合成一个组件,因为它们依赖于一个直接与另一个独立组件的好处是减少了。

However it's usually recommended to have loosely coupled assemblies by way of interfaces; a side effect is you don't use the concrete class name directly anymore. Otherwise by using tightly-coupled assemblies you might as well combine them into one assembly because they rely on one another directly and the benefit of separate assemblies is diminished.

有时候,我的软件需要像一个更具描述性的方法的客户 CustomerData 的班,我意识到这是一个使用后缀,但其目的是为自然流动,而不是prevent对命名冲突,因为我的接口,它们之间无论坐在

Sometimes my software takes a more descriptive approach like Customer and CustomerData classes, and I realize this is using a suffix, but the intention is for natural flow and not to prevent against naming conflicts because my interface sits between them regardless.

简单地说,低凝聚力呈现的是什么类应/可/将项目相关的任何一层命名为彼此的问题毫无意义。因为你已经有企业单独的组件和数据的责任,你必须要含蓄低凝聚力是present。所以,我在应用设计方面的回答是无类后缀或preFIX是客观上更好根据问题的概念。

In a nutshell, low cohesion renders the question moot of what classes should/could/would be named in any layer of the project relative to one another. And because you already have separate assemblies for business and data responsibilities, you must implicitly want low cohesion to be present. So my answer in context of application design is no class suffix or prefix is objectively better according to the concept of the question.

为了一个有用的code的例子,我包括下面的C#骨架,以显示类的命名政策,我倾向于对(或缺乏政策可能会更准确)。

For the sake of a useful code example, I'm including the following C# skeleton to show the class naming policy I gravitate toward (or lack of policy might be more accurate).

注:这个例子是不完整的,在某些方面,但显示了如何类的命名并不组件之间的关系(即使是相同的类名),因为它们使用的接口分离的概念

Note: This example is incomplete in some ways but shows the concept of how class naming doesn't matter between assemblies (even if it's the same class name) because they are separated using interfaces.

Business.dll - 业务层组件

参考Shared.dll组装。

Reference Shared.dll assembly.

namespace Com.Examle.MyProject.Business {
    using Com.Example.MyProject.Shared; // for ICustomer

    class Customer {
        // Reference obtained to an ICustomer implementation (not shown).
        // Composition of the data customer, just for illustration purposes
        ICustomer _cust;

        // From business, a data access usage example:
        public void GetData() {
            _cust.SaveToDataSource();
        }
    }
}

商业正在使用,而不是被硬连线到Data.dll组件ICustomer

Business is using ICustomer instead of being hard-wired to the Data.dll assembly.

Shared.dll - 共享组件 - 常见的类型是引用到两个业务和数据组件

Shared.dll - Shared assembly - Common types are referenced into both business and data assemblies.

// This is the only required link between business and data assemblies. 
// Business is shielded from Data concrete class names and vice-versa. 
namespace Com.Example.MyProject.Shared {
    public interface ICustomer {
        void SaveToDataSource();
    }
}

Data.dll - 数据访问层

Data.dll - Data access layer

参考Shared.dll组装。

Reference Shared.dll assembly.

namespace Com.Example.MyProject.Data {
    using Com.Example.MyProject.Shared; // for ICustomer

    class Customer : ICustomer { // implement ICustomer - other assemblies can too
        public void SaveToDataSource() {
            // logic to save data to the data source
        }
    }

}

这篇关于是什么在数据访问项目的命名约定上课?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆