简体   繁体   English

ASP.NET类库最佳实践

[英]ASP.NET Class Library best practice

I have an ASP.NET project that contains many classes. 我有一个包含许多类的ASP.NET项目。

I am thinking about creating a class library for the class files so that the code can be reused in other applications. 我正在考虑为类文件创建一个类库,以便可以在其他应用程序中重用该代码。 There are two options: 有两种选择:

1) Create a class library containing all the classes 2) Create a class library for only the classes that contain code that will be shared and then perhaps use interfaces 1)创建一个包含所有类的类库2)仅为包含将被共享然后可能使用接口的代码的类创建一个类库

Is there any true benefits of option 2? 选项2有真正的好处吗? Option 1 would be beneficial because all the code would be in one place. 选项1将是有益的,因为所有代码都将放在一个地方。

I would recommend option #2. 我会建议选择#2。

If the code will be reused by other applications, I wouldn't add the additional classes in there, as it would clutter up the library. 如果代码将被其他应用程序重用,则我不会在其中添加其他类,因为这会使库变得混乱。 I would put the remaining classes in the App_Code folder of your ASP.NET application that are specific to that application. 我将其余的类放在特定于该应用程序的ASP.NET应用程序的App_Code文件夹中。

If your purpose is re-use, then it makes sense to bundle together classes that will be reused. 如果您的目的是重用,则将要重用的类捆绑在一起是有意义的。 However, this is not the only concern when developing a class library. 但是,这不是开发类库时唯一关心的问题。 Ultimately you want high cohesion and low coupling, not only at the class/method level, but at the class library (component level) as well. 最终,不仅在类/方法级别,而且在类库(组件级别),您都希望具有高凝聚力和低耦合。 See: 看到:

http://jasoncoffin.com/2011/03/10/cohesion-and-coupling-principles-of-orthogonal-object-orientated-programming/ http://jasoncoffin.com/2011/03/10/cohesion-and-coupling-principles-of-orthogonal-object-orientated-programming/

Loose Coupling and OO Practices for Beginners 初学者的松耦合和OO实践

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

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