2

I have inherited an a VB.NET WinForms application. It is very poorly written with a lot of bad practices. The first order of business is to get some DI into the app with a container to resolve the dependencies so I can start breaking this thing up and getting it under test.

This is my first WinForms app and I am learning the nuances of a non-request based application on a single thread.

I am using Simple Injector as the IoC container.

One of my use cases that I need to refactor is a UserControl that is extending a DevExpress XtraUserControl.

From the Simple Injector docs:

Note: It is not possible to use Constructor Injection in User Controls. User Controls are required to have a default constructor. Instead, pass on dependencies to your User Controls using Method Injection.

I am following the docs verbatim on how to set up property injection:

IPropertySelectionBehavior

Here is my configuration:

Private Shared Sub Bootstrap()

    Container = New Container()

    Container.Options.DefaultScopedLifestyle = New ThreadScopedLifestyle()
    Container.Options.PropertySelectionBehavior = New ImportPropertySelectionBehavior()

    Container.Register(Of ICommissionManager, CommissionManager)(Lifestyle.Singleton)
    Container.Register(Of frmMain, frmMain)(Lifestyle.Singleton)
    Container.Register(Of viewSalesCustomers, viewSalesCustomers)(Lifestyle.Transient)

    'https://stackoverflow.com/questions/38417654/winforms-how-to-register-forms-with-ioc-container/38421425
    Dim registration As Registration = Container.GetRegistration(GetType(viewSalesCustomers)).Registration
    registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent, "a reason")

    Container.Verify()


End Sub

The user control:

Public Class viewSalesCustomers
    Inherits DevExpress.XtraEditors.XtraUserControl

    <Import>
    Public Property CommissionManager As ICommissionManager

...redacted...

Private Sub viewSalesCustomers_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim commissions As ICollection(Of Commission)
    commissions = CommissionManager.Get(Function(commission) commission.CommissionId = 1) <-- Always nothing (null)

End Sub

I know the container is resolving for anything deriving from Form (I tested it through constructor injection).

Not sure what I missing here on the property injection for this control.

UPDATE:

@Steven was spot on. I found where they are instantiating the control.

Private Sub BarSubItem2_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarSubItem2.ItemClick
    Cursor = Cursors.WaitCursor
    'Dim mView as New viewSalesCustomers()
    Dim mView As viewSalesCustomers
    mView = Program.Container.GetInstance(Of viewSalesCustomers)
    showViewer(mView, "viewSalesCustomers")
    Cursor = Cursors.Default
End Sub
JDBennett
  • 1,323
  • 17
  • 45
  • Please show the code that shows how you resolve `viewSalesCustomers` from the `Container`. Property injection only works if you let Simple Injector create such user control (or initialize an existing one), e.g. using `Container.GetInstance(Of viewSalesCustomers)`. – Steven Mar 21 '19 at 15:36
  • @Steven - thanks! That did the trick. – JDBennett Mar 21 '19 at 16:41
  • As it seems, in your case, you might be able to use constructor injection after all. – Steven Mar 21 '19 at 16:47

0 Answers0