기존의 winforms 애플리케이션을 가지고 있습니다. 이제 모든 곳에서 객체를 수동으로 생성 할 필요없이 컨테이너로 Unity를 사용하려고합니다. 지금은 상황이 그렇게 나쁘지는 않지만 우리는 프로그램을 확장 할 것이며, Unity 없이는 일이 복잡 해지는 것을 볼 수 있습니다. 런타임 정보에 의존하는 객체를 만드는 방법을 이해하는 데 어려움을 겪고 있습니다. Unity Container로 런타임 객체 생성하기
Public Class CustomerSearchPresenter
Implements ICustomerSearchPresenter
Private Repo As ICustomerRepo
Private View As ICustomerSearchView
Public Sub New(_view As ICustomerSearchView, _customerRepo As ICustomerRepo)
View = _view
Repo = _customerRepo
End Sub
Public Sub SearchForCustomer(_customerNumber As String) Implements ICustomerSearchPresenter.SearchForCustomer
'seach the database to see if customer exists and if so create a new customer view...
Dim foundCustomer As ICustomer = Repo.find(_customerNumber)
If Not IsNothing(foundCustomer) Then
'* Manual creation of all objects happens here - how do I change this in unity to create a new
Dim cusView As New customerView
Dim custPresenter As New CustomerPresenter(cusView, foundCustomer)
'custPresenter creates a new tab on the main form to display the details for that customer
Else
View.feedback("No customer found for " & _customerNumber)
End If
End Sub
End Class
Public Class CustomerPresenter
Implements ICustomerPresenter
Private View As ICustomerView
Private Customer As ICustomer
Public Sub New(_View As ICustomerView, _customer As ICustomer)
View = _View
Customer = _customer
End Sub
Public Sub RefreshData() Implements ICustomerPresenter.RefreshData
'do some stuff here ....
End Sub
End Class
CustomerSearchPresenter 새를 만들기위한 책임이 있습니다 .. (이 프로그램은 당신이 한 번에 여러 고객을 볼 수있는 새 탭에서 각 한) 일이 현재 작동하는 방법의 예를 들어 다음과 같은 코드를 가지고 고객 섹션에서 사용자가 유효한 고객 번호를 검색 할 때마다 이것은 고객 객체가 저장소에 의해 반환 될만큼 간단하며 관련 뷰/발표자를 만들 때 수동으로 주입 할 수 있습니다. (MVP 기반이기는하지만 MVC에 적용되는 동일한 이론을 추측합니다.)
Unity에서 이것을 재 작업하려는 첫 번째 시도는 다음과 같았습니다 (Func (of) 사용)하지만 작동하지 않습니다. 호출이 새 객체를 생성하지 않고 동일한 객체를 반환합니다. 사용자가 검색 할 때마다 새로운 고객 특정 발표자를 어떻게 작성해야합니까?
Public Class CustomerSearchPresenter
Implements ICustomerSearchPresenter
Private Repo As ICustomerRepo
Private View As ICustomerSearchView
Private Builder As Func(Of ICustomerPresenter)
Public Sub New(_view As ICustomerSearchView, _customerRepo As ICustomerRepo, _customerBuilder As Func(Of ICustomerPresenter))
View = _view
Repo = _customerRepo
End Sub
Public Sub SearchForCustomer(_customerNumber As String) Implements ICustomerSearchPresenter.SearchForCustomer
'seach the database to see if customer exists and if so create a new customer view...
Dim foundCustomer As ICustomer = Repo.find(_customerNumber)
If Not IsNothing(foundCustomer) Then
'* Assumed I could call invoke and then register the customer afterwards, but this just invokes the same object
Dim custPresenter As ICustomerPresenter = Builder.Invoke
custPresenter.registerCustomer(_foundCustomer)
Else
View.feedback("No customer found for " & _customerNumber)
End If
End Sub
End Class
편집 : 방법은 작업을 수행 (의)를 FUNC, 나는 ContainerControlledLifetimeManager는 항상 같은 발표자를 반환 의미와 유형을 등록 것입니다. 그래서 지금이 작품은 이것을 해결하는 가장 좋은/올바른 방법입니까?
편집 2 : 다른 질문을 보내 주셔서 감사합니다.하지만 코드에 적용하는 데 어려움이 있습니다. 나는 아직도있는 다음 예제 나 그 해결에 도움 않는 방법을 의미합니다 (주변에 컨테이너를 통과하지 않고) 내 개체 컨테이너에서 해결해야 ... 내가 물건 발표자에 대한 액세스 권한이없는 것
Public Class CustomerPresenterFactory
Implements ICustomerPresenterFactory
Function Create(_customer As ICustomer) As ICustomerPresenter Implements ICustomerPresenterFactory.Create
Return ????
End Function
End Class
보기와 같은 고객 이외의 요구 사항.
[DI 컨테이너를 통해 생성 된 개체를 초기화하는 패턴이 있습니까?] 가능한 복제본 (http://stackoverflow.com/questions/1943576/is-there-a-pattern-for-initializing-objects-created-via) -a-di-container) – NightOwl888
관련 항목 : [의존성 삽입 방지 패턴 : 런타임 데이터를 구성 요소에 주입] (https://cuttingedge.it/blogs/steven/pivot/entry.php?id=99) – NightOwl888