使用unity的依赖注入vb.net

时间:2015-10-12 17:18:09

标签: .net vb.net dependency-injection

我正在尝试使用vb.net中的unity Container实现依赖注入

当我调用resolve方法时,我得到了resolutionFaieldException错误

依赖关系的解决方案失败,输入=" Test.EmailSending",name =" PDFEmailSender"。 在解决时发生例外情况。

Module Module1

Private iemail As IErrorEmail

Sub Main()

    'Dim email As New Test.EmailSending(iemail)
    'email.Email()
    Dim unity As UnityContainer = New UnityContainer
    unity.RegisterType(Of IErrorEmail, SQLErrorEmail)("SQLErrorEmail")
    unity.RegisterType(Of IErrorEmail, GenericEmail)("GenericEmail")
    unity.RegisterType(Of IErrorEmail, Emailsending)("Emailsending")
    Dim email1 = unity.Resolve(Of EmailSending)("SQLErrorEmail")
    email1.Email()
End Sub



End Module

界面很简单错误电子邮件界面

Namespace InterfaceError
Public Interface IErrorEmail
    Function ErrorEmail() As Boolean
End Interface
End Namespace

我添加了两种实现Ierror接口的电子邮件类

Imports InterfaceSender.InterfaceError
Public Class SQLErrorEmail
    Implements IErrorEmail
    Private _SenderEmail As IErrorEmail
    Public Sub New(SQLErrorEmail As IErrorEmail)
        _SenderEmail = SQLErrorEmail
    End Sub
    Public Function ErrorEmail() As Boolean Implements IErrorEmail.ErrorEmail
        Dim isemailed As Boolean = False
        Try
            Email()
        Catch ex As Exception
        End Try
        Return isemailed
    End Function
    Public Shared Sub Email()
        Console.WriteLine("SQL Error Email")
        Console.ReadLine()
    End Sub
End Class

上面的SQL错误类实现了ErrorEmail函数,并向控制台写入它是一个SQL错误电子邮件。

    Imports InterfaceSender.InterfaceError
Public Class GenericEmail
    Implements IerrorEmail
    Private _SenderEmail As IErrorEmail
    Public Sub New(GenericEmail As IErrorEmail)
        _SenderEmail = GenericEmail
    End Sub
    Public Function SendEmail() As Boolean Implements IErrorEmail.ErrorEmail
        Dim isemailed As Boolean = False
        Try
            Email()
        Catch ex As Exception

        End Try
        Return isemailed
    End Function
    Public Shared Sub Email()
        Console.WriteLine("Generic Error Email")
        Console.ReadLine()
    End Sub
End Class

上面的类实现了继承Ierror接口的泛型错误,当调用email方法时,它会将Genric输出电子邮件输出到控制台。

    Imports Method.GenericEmail
Imports Method.SQLErrorEmail
Imports InterfaceSender.InterfaceError
Public Class EmailSending
    Private _Isender As IErrorEmail
    Public Sub New(ByVal Email As IErrorEmail)
        MyBase.New()
        _Isender = Email
    End Sub
    Public Sub EmailSendingLogic()
        Dim type As String = "SQL"
        If type = "SQL" Then
            Method.SQLErrorEmail.Email()
        ElseIf type = "Generic" Then
            Method.GenericEmail.Email()
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

因此,您正在尝试创建一个类(EmailSending),该类根据某些逻辑知道要使用哪个IErrorEmail

首先,您不必将IErrorEmail注入GenericEmailSQLErrorEmail的构造函数中。你为什么这样做?

以下是我看到这两个类看起来的样子:

Public Class SQLErrorEmail
    Implements IErrorEmail
    Public Sub New()

    End Sub
    Public Function ErrorEmail() As Boolean Implements IErrorEmail.ErrorEmail
        Console.WriteLine("SQL Error Email")
        Return True
    End Function

End Class


Public Class GenericEmail
    Implements IErrorEmail

    Public Sub New()

    End Sub
    Public Function SendEmail() As Boolean Implements IErrorEmail.ErrorEmail
        Console.WriteLine("Generic Error Email")
        Return True
    End Function
End Class

然后,由于EmailSending类需要使用两个电子邮件发件人(GenericEmailSQLErrorEmail),您应该像这样注入两个IErrorEmail依赖项:

Public Class EmailSending
    Private ReadOnly m_GenericSender As IErrorEmail
    Private ReadOnly m_SqlSender As IErrorEmail
    Public Sub New(generic_sender As IErrorEmail, sql_sender As IErrorEmail)
        m_GenericSender = generic_sender
        m_SqlSender = sql_sender
    End Sub

    Public Sub EmailSendingLogic()
        Dim type As String = "SQL"
        If type = "SQL" Then
            m_SqlSender.ErrorEmail()
        ElseIf type = "Generic" Then
            m_GenericSender.ErrorEmail()
        End If
    End Sub
End Class

最后,以下是如何通过统一注册对象图:

Dim unity As UnityContainer = New UnityContainer
unity.RegisterType(Of IErrorEmail, SQLErrorEmail)("SQLErrorEmail")
unity.RegisterType(Of IErrorEmail, GenericEmail)("GenericEmail")
unity.RegisterType(Of EmailSending)(
    New InjectionConstructor(
        New ResolvedParameter(Of IErrorEmail)("GenericEmail"),
        New ResolvedParameter(Of IErrorEmail)("SQLErrorEmail")))

以下是解决和使用EmailSending

的方法
Dim email1 = unity.Resolve(Of EmailSending)()

email1.EmailSendingLogic()

另一种方法(我认为在设计方面更好)是创建abstract factory,根据某些逻辑(例如类型)创建适当的IErrorEmail实现。这是一个例子:

Public Interface IErrorEmailFactory
    Function Create(type As String) As IErrorEmail
End Interface

Public Class ErrorEmailFactory
    Implements IErrorEmailFactory

    Public Function Create(type As String) As IErrorEmail Implements IErrorEmailFactory.Create
        If type = "SQL" Then
            Return New SQLErrorEmail()
        ElseIf type = "Generic" Then
            Return New GenericEmail()
        End If
    End Function
End Class

然后,你会像IErrorEmailFactory这样注入EmailSending

Public Class EmailSending
    Private ReadOnly m_Factory As IErrorEmailFactory

    Public Sub New(factory As IErrorEmailFactory)
        m_Factory = factory
    End Sub

    Public Sub EmailSendingLogic()
        Dim type As String = "SQL"

        Dim error_email = m_Factory.Create(type)

        error_email.ErrorEmail()

    End Sub
End Class

然后你会注册并使用它:

Dim unity As UnityContainer = New UnityContainer

unity.RegisterType(Of IErrorEmailFactory, ErrorEmailFactory)()

Dim email1 = unity.Resolve(Of EmailSending)()

email1.EmailSendingLogic()