How to use the interface in my code to make it more scalable?

I have an ASP.NET web application that has a user interface project and a BL project. The BL project handles all business logic and data access part. I call BL methods from my user interface by simply calling the method in instances.

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 

and from my UI (its in C #), I do this.

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

Everything works perfectly. As the project grows, I thought about trying to add some kind of tests to test the units of testing / dependency. Going around in a circle and seeing that each example uses interfaces. I read so much about interfaces, but I don’t know how to include interfaces in my coding and makes it follow a better (scalable and verifiable) pattern. Besides checking, will it give me another advantage?

Can someone provide me a sample on how to do this?

+4
source share
4 answers

Take a look at the Model-View-Controller design template, and the asp.net mvc framework . There is a good review here that explains the benefits of this design strategy.

Excerpt here:

The MVC pattern helps you create applications that distinguish between different aspects of the application (input logic, business logic, and UI), while providing free communication between these elements. The template determines where each kind of logic in the application should be located. The user interface logic belongs to the view. Input logic belongs to the controller. Business logic belongs to the model. This separation helps you manage complexity when creating your application, as it allows you to focus on one aspect of the time. For example, you can focus on business logic.

Loose communication between the three core components of an MVC application also facilitates concurrent development. For example, one developer can work on a presentation, a second developer can work on a controller logic, and a third developer can focus on the business of logic in a model.

+2
source

Encoding to interfaces provides many advantages; better testability is one side effect associated with the main advantage: to untie an object from its dependencies .

Say I have a class A, and in its implementation it uses class B. If you do not provide an interface for B, then you tightly linked the design of A and B together (you cannot consider A without this specific implementation of B, since it uses a specific implementation).

If you write interface B (let's say IB) and use it in instead of directly using B, you separate the design and make them independent. A can now function independently of a specific implementation of B, it only knows the interface to it (IB) and the methods that it needs to run on it. Therefore, if you later decide that your implementation of B was not good, or if you want to be able to work on two different IBs depending on the context, you can replace B with B2 , which also implements IB , so you do not need to change it at all A.

The action to create A by injecting an implementation of IB at runtime is as follows:

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

called dependency injection and is the best way to achieve a very desirable OO programming function: control inversion (this is not A, it controls the behavior of its dependencies more, it is an independent class - factory - which controls what is introduced in A).


For the purpose of testing, you can run a unit test A without assuming B (which, obviously, needs to be tested separately). Therefore, in your tests, you can inject a stub or a mock implementation of B to help you test the behavior you want.

(Sorry, there are no real code examples, as I am a Java developer and not very good with C # syntax)

+1
source

An interface is a description of the functionality for a class. Any class that implements an interface must implement interface properties, methods, indexes, and / or events. The interface does not contain an implementation, but only signatures for the functionality provided by the interface.

There are several advantages to using interfaces. One of the biggest is that it overcomes the lack of multiple inheritance in .NET. You cannot inherit multiple classes in .NET, but you can implement multiple interfaces.

Other benefits include lighter communications, easier maintenance, and more efficient use of code because the implementation is separate from the interface.

Here is a simple example of how your code can use an interface (using 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 

And from your user interface you can:

 Dim objUser as iPerson = New User objUser.FirstName = "Bob" objUser.LastName = "Mckenzie" ctype(objUser, User).UserId = 12345 

If you decide to create a new class that implements iPerson (i.e., SuperUser), most of the code in your user interface may remain the same:

 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 
+1
source

You can add a layer for connecting the user interface and BL. I'm not a fan of the user interface, knowing anything about BL. This can be done using interfaces, but you do not need to do with them.

0
source

Source: https://habr.com/ru/post/1390850/


All Articles