2015-10-12 124 views
0

我試圖用統一的容器在vb.net依賴注入vb.net使用統一

當我這樣做調用resolve方法,我收到了resolutionFaieldException錯誤

依賴的解決方案未能實現依賴注入,type =「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 

我已經添加了兩個類型的電子郵件類別的其中實現I誤差接口

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 

上述類實現繼承了I誤差接口和當電子郵件方法被調用它的一個輸出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 
+2

你也註冊了EmailSending嗎?因爲它沒有在 –

+0

以上的代碼中註冊不,我不會嘗試 – user1339913

+0

您嘗試創建的對象圖是什麼?你想在EmailSending中注入哪個'IErrorEmail'實現?你能解釋一下你想做什麼嗎? –

回答

1

所以,你要創建一個知道這取決於一些邏輯使用哪個IErrorEmail類(EmailSending)。

首先,您不必將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基於一些邏輯的實現(例如,類型)。這裏有一個例子:

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 

然後,你會注入IErrorEmailFactoryEmailSending這樣的:

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() 
+0

你是一個救星!!!!。很好的解釋。是由RegisterType創建的ierrorEmail接口還是統一的解析方法。 – user1339913

+0

RegisterType只是創建一個接口和一個類之間的映射(還有其他類型的映射)。 'Resolve'用於實際創建對象(或對象圖)。我希望我能正確理解你的問題。 –