SOLID原则和网页类 [英] SOLID principles and web page class

查看:132
本文介绍了SOLID原则和网页类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写新的类和code时要遵循SOLID原则。

I am trying to follow SOLID principles when writing new classes and code.

我的问题是关于扩展Page类即ASPX文件ASP.NET类。如果我有,有创建多个对象如的实例的Page_Load事件Page类Person类,体育类等话,我认为此页面类是紧密耦合与这些类。这样的话还是我失去了一些东西明显?难道说所有类应揭露接口和客户端(aspx页面)应该使用接口,而不是直接实例化的类。

My question is about ASP.NET classes that extend the Page class i.e. ASPX files. If I had a page class that has a page_load event that creates instances of multiple objects e.g. Person class, Sport class etc then I believe this page class is tightly coupled with these classes. Is this the case or am I missing something obvious? Could it be that all classes should expose interfaces and the client (aspx pages) should use the interfaces rather than instantiating the classes directly.

我觉得接口有用的,如果多态是涉及如用学生界面在运行时创建的研究生或者本科生的一个实例。如果所有的类都具有的接口?

I find interfaces useful if Polymorphism is involved e.g. using a Student interface to create an instance of graduate or undergraduate at runtime. Should all classes have interfaces?

推荐答案

如果您创建类(presentation层)在你的实体,你都清楚地违反了单一职责原则,因为页面类将有多个理由来改变。

If you create your entities inside your Page class (presentation layer), you are clearly violating the Single Responsibility Principle, since the page class will have multiple reasons to change.

相反,推动这一逻辑在业务层和创建处理这个逻辑的服务。

Instead, move this logic to the business layer and create a service that handles this logic.

您的网页需要交谈的服务接口,而不是实现(DIP)和该服务接口需要窄(ISP);可能只是一种方法。

Your page will need to talk to the service interface, not the implementation (DIP) and this service interface needs to be narrow (ISP); probably just have one method.

如果您封装为服务的所有参数到一个单一的对象,从而分离数据和行为,并使用通用接口为您服务(即 ICommandHandler< TCommand> ) ,你甚至可以符合欧盟的OCP,因为你现在可以添加行为(如验证,交易,死锁检测,异步处理,排队)到服务,任何内部消除更改应用程序。

If you package all arguments for the service into a single object, thus separating data and behavior, and use a generic interface for your services (i.e. ICommandHandler<TCommand>), you can even comply to the OCP, since you can now add behavior (such as validation, transactions, deadlock detection, asynchronous processing, queueing) to services, whithout any changes to the application.

最后一个音符,没有为实体的接口。这是相当无用的,而掩盖了你的code。

Last note, don't create interfaces for entities. This is rather useless, and obscures your code.

这篇关于SOLID原则和网页类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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