Dependency Injection - I don't get it!

Ok, so I played with Ninject, "Service Layer" and "Repository Layer".

I created a simple console application for the game, here is what I came up with:

Imports Ninject

Module Module1

    Sub Main()
        Dim Kernel As IKernel = New StandardKernel(New CustomerModule)
        Dim Service = Kernel.Get(Of CustomerService)()
        Console.WriteLine(Service.GetCustomerByID(1).Name)
        Console.Read()

    End Sub

End Module

#Region "Services"

Public Class CustomerService
    Private _Repository As ICustomerRepository

    <Inject()> _
    Public Sub New(ByVal Repository As ICustomerRepository)
        _Repository = Repository

    End Sub

    Public Function GetCustomerByID(ByVal ID As Integer) As Customer
        Return _Repository.GetByID(ID)
    End Function

End Class

#End Region

#Region "Repositories"

Public Interface IRepository(Of T)
    Function Query(ByVal Predicate As Expressions.Expression(Of Func(Of T, Boolean))) As IQueryable(Of T)
    Function GetByID(ByVal ID As Integer) As T

End Interface

Public Interface ICustomerRepository
    Inherits IRepository(Of Customer)

End Interface

Public Class CustomerRepository
    Implements ICustomerRepository

    Public Function GetByID(ByVal ID As Integer) As Customer Implements IRepository(Of Customer).GetByID
        Return New Customer With {.ID = ID, .Name = "Sam Striano"}
    End Function

    Public Function Query(ByVal Predicate As System.Linq.Expressions.Expression(Of System.Func(Of Customer, Boolean))) As System.Linq.IQueryable(Of Customer) Implements IRepository(Of Customer).Query
        Return Nothing
    End Function

End Class

#End Region

#Region "Domain Objects"

Public Class Customer
    Public Property ID As Integer
    Public Property Name As String
End Class

#End Region

#Region "Ninject Modules"

Public Class CustomerModule
    Inherits Modules.NinjectModule

    Public Overrides Sub Load()
        Bind(Of ICustomerRepository).To(Of CustomerRepository)()

    End Sub

End Class

#End Region

My question, or I think I don’t understand, is the module's Main () method:

Sub Main()
    Dim Kernel As IKernel = New StandardKernel(New CustomerModule)
    Dim Service = Kernel.Get(Of CustomerService)()
    Console.WriteLine(Service.GetCustomerByID(710615).Name)
    Console.Read()

End Sub

Why not just do it:

Sub Main()
    Dim Service = New CustomerService(New CustomerRepository)
    Console.WriteLine(Service.GetCustomerByID(710615).Name)
    Console.Read()

End Sub
+5
source share
2 answers

Enabling dependencies allows you to separate specific implementations of objects from their interfaces. This is difficult to justify on most small examples, but for larger systems this can be a life saver. It can also help you isolate your objects in unit tests.

, CustomerService, MockRepository CustomerRepository. CustomerService CustomerRepository.

, , . , SQL Server MySQL. . , :

Function Setup(ByVal dbType As String) As IKernel
    Dim dbModule As NinjectModule
    If dbType = "SQL Server" Then
        dbModule = New SQLServerModule
    Else If dbType = "MySQL" Then
        dbModule = New MySQLModule
    End If

    Return New StandardKernel(dbModule)
End Function

, .

+4

, , DI Pure DI strong > .

DI - , . .

, , , DI - , , .

+9

All Articles