English 中文(简体)
• 如何在我《法典》中使用接口,使其更加 s?
原标题:How to Use Interface in my Code to make it more scalabale?

我有一个伙伴关系。 NET Web application which has a UI Project and BL project. BL项目处理所有业务逻辑和数据存取部分。 我简单地把这一方法引向于我的统一倡议。

Public Class User

    Public Property UserID As Integer
    Public Property FirstName As String
    //rest of the properties for user


  Public Sub Save()
   //save the details to the database
  End sub
End Class

以及我方言(C#中的内容)都这样做。

  User objUser=new User();
  objUser.FirstName="Happy";
  objUser.Save();

一切产品都进行罚款。 由于该项目在不断发展,人们想想增加某种单位检测/独立注射检测。 谷歌一环,每一例都使用界面。 我对接口读得如此之多,但不知道如何将接口纳入我的编码,使之采用更好的(可衡量和可检测的)模式。 除了可测试性外,它是否给我带来其他好处?

谁能向我提供如何做到这一点的样本?

问题回答

查阅 设计模式和asp.net mvc framework。 http://msdn.microsoft.com/en-us/library/dd381412%28VS.98%29.aspx”rel=“nofollow”>here,说明这一设计战略的益处。

摘录如下:

The MVC pattern helps you create applications that separate the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements. The pattern specifies where each kind of logic should be located in the application. The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model. This separation helps you manage complexity when you build an application, because it enables you to focus on one aspect of the implementation at a time. For example, you can focus on the view without depending on the business logic.

The loose coupling between the three main components of an MVC application also promotes parallel development. For example, one developer can work on the view, a second developer can work on the controller logic, and a third developer can focus on the business logic in the model.

www.un.org/Depts/DGACM/index_spanish.htm 连接界面提供了大量的优势,可检测性与主要优势相关的副作用是:将物体与其属地脱钩

Say我有一个A类,在执行中使用了B类。 如果你不向B提供接口,那么就将A和B的设计与合并起来。 (如果不具体执行B,就无法认为是A,因为它使用了具体执行。)

如果你写给B(say IB)的接口,并在A中使用该接口,而不是直接使用B,那么,你将与设计挂钩,并使之独立。 如今,A能够独立于具体的增资B独立运作,它只知道与B(IB)的接口以及它所需要的方法。 因此,如果你后来决定,你执行Bn t 好,或者如果你希望A能够根据具体情况在两个不同的IB上工作,那么你就可以将B2改为B2,,后者也执行IB,因此你根本不必修改A。

• 采取行动,通过在运行时注射执行国际会计制度,创建A:

A myA = new A(new B());  // this could also be new A(new B2()); or anythign else that implements IB

<>Restrong>dependency injection,是最佳途径,可实现业务协调处方案拟订中一个非常可取的特点: 转移控制。 (它不是控制其附属公司的行为的A,而是控制A所注入的产品的独立类别——工厂。)


可在<>检测>上检测。 假设B

(没有实际编码实例,因为I m a Java developer,而不是与C#的辛迪一样)

An interface is a description of functionality for a class. Any class that implements the interface must implement the interface s properties, methods, indexers, and/or events. An interface contains no implementation, only the signatures for the functionality the interface provides.

使用接口有几个优势。 其中一项大事是,它允许你在NET中摆脱多重继承。 您不能继承互联网上的多个课程,但可以实施多个接口。

其他好处包括松散的 coup合,易于维持,使代码的再利用更容易获得,因为执行工作与接口分开。

下面是贵国法典如何使用接口的简单例子(使用VB.NET):

Public Interface iPerson
    Property FirstName As String
    Property LastName As String
     Rest of properties for a person

    Sub Save()
End Interface

Public Class User
    Implements iPerson

    Public Property UserId As Integer

    Public Property FirstName As String Implements iPerson.FirstName
    Public Property LastName As String Implements iPerson.LastName

    Public Sub Save() Implements iPerson.Save
         Add code to save user
    End Sub
End Class

你可以:

Dim objUser as iPerson = New User

objUser.FirstName = "Bob"
objUser.LastName = "Mckenzie"
ctype(objUser, User).UserId = 12345

如果你决定成立一个新的班级,在贵股中实施“iPerson”(e - SuperUser)大多数守则,那么:

Dim objUser as iPerson = New SuperUser

objUser.FirstName = "Bob"
objUser.LastName = "Mckenzie"

 The next line would throw a runtime error since object is not of type user
ctype(objUser, User).UserId = 12345

You could add a layer to bridge the UI and BL. I m not a fan of the UI knowing anything about the BL. This can be accomplished with interfaces, but it doesn t need to be done with them.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签