Property Injection for User Controls in WinForms - winforms

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

Related

Updating UI from another thread with VB in WPF

I am trying to use a timer to scan my Xbox 360 controller. But I cannot directly update my UI like the code I wrote below.
I would get a exception when I try to run this code.
An exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll but was not handled in user code
Additional information: The calling thread cannot access this object because a different thread owns it.
XButton is a radiobutton on the GUI that I want to toggle.
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Input
Imports System.Timers
Imports System.Windows.Threading
Public Class XboxControllerStatus
Friend WithEvents Timer1 As Timer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Elapsed
Dim currentState As GamePadState = GamePad.GetState(PlayerIndex.One)
If currentState.IsConnected Then
If currentState.Buttons.X.Pressed Then
XButton.IsChecked = True
Else
XButton.IsChecked = False
End If
End If
End Sub
End Class
This works for me, all the time
Control.Invoke(sub()
'Put code here
End Sub)
First you need to set up a delegate
Delegate Sub SetCheckBoxCallback(ByVal value As Boolean)
Friend Sub SetCheckBox(ByVal value As Boolean)
XButton.IsChecked = value
End Sub
after that all you need to do is call the following code from within your timer to invoke it:
Dim DesiredValue as Boolean = True
Me.Dispatcher.Invoke(New SetCheckboxCallback(AddressOf SetCheckbox), New
Object() {DesiredValue})

How to use another ioc container with mef?

i am using mef with prism. i can use mef because i like the export, import, metadata attributes and mostly aggregate cagalog usage. so i want to use mef with prism project.
In my plan, my solution projects must be use autofac or castle windsor ioc container and i implement like that except prism project(wpf). In case, i am not prefer to use autofac or castle windsor instead of mef's default di/ioc but too many alternative usage of personal experimantals are failed.
Is there a any stable sample project i can use? I want to change only ioc of mef with all mef functionalty.
My classic mef bootstrapper code is bellow
Imports System.ComponentModel.Composition.Hosting
Imports Microsoft.Practices.Prism.MefExtensions
Imports Microsoft.Practices.ServiceLocation
Public Class Bootstrapper2
Inherits MefBootstrapper
Protected Overrides Sub ConfigureContainer()
MyBase.ConfigureContainer()
Dim ag As New AggregateCatalog()
ag.Catalogs.Add(New AssemblyCatalog(GetType(Bootstrapper2).Assembly))
ag.Catalogs.Add(New DirectoryCatalog("..\..\modules\", "Prism.Sample.Modules.*.dll"))
Me.AggregateCatalog.Catalogs.Add(ag)
End Sub
Protected Overrides Function CreateShell() As DependencyObject
Dim s As Shell = ServiceLocator.Current.GetInstance(Of Shell)()
Return s
End Function
Protected Overrides Sub InitializeShell()
Application.Current.MainWindow = Shell()
Application.Current.MainWindow.Show()
End Sub
End Class
Shell's code is bellow:
Imports System.ComponentModel.Composition
<Export()> _
Public Class Shell
Sub New()
InitializeComponent()
End Sub
<Import(AllowRecomposition:=False)> _
Public Property ViewModel() As ShellViewModel
Get
Return CType(Me.DataContext, ShellViewModel)
End Get
Set(value As ShellViewModel)
Me.DataContext = value
End Set
End Property
End Class
Now, everythings working like an expected.
modified/overrided bootstrapper's ConfigureServiceLocator() method is bellow.
Private autofacBuilder As New Autofac.ContainerBuilder
Protected Overrides Sub ConfigureServiceLocator()
Dim autofacContainer = autofacBuilder.Build()
Dim autofacSL = New Prism.AutofacExtension.AutofacServiceLocatorAdapter(autofacContainer)
ServiceLocator.SetLocatorProvider(Function() autofacSL)
End Sub
then i have got an too many resolving exception for example:
exception message:
Activation error occured while trying to get instance of type RegionAdapterMappings, key "".
Prism or another code base trying to resolve IRegionAdapterMappings from the servicelocator but currentservice locator not knowns what is this.Because mef is allready registered this types((ConfigureContainer) before CreateServiceLocator.
So, then i trying to add mef's aggregate catalog to register autofac container with Autofac.Integration.Mef project like this:
Private autofacBuilder As New Autofac.ContainerBuilder
Protected Overrides Sub ConfigureServiceLocator()
autofacBuilder.RegisterComposablePartCatalog(Me.AggregateCatalog)
Dim autofacContainer = autofacBuilder.Build()
Dim autofacSL = New Prism.AutofacExtension.AutofacServiceLocatorAdapter(autofacContainer)
ServiceLocator.SetLocatorProvider(Function() autofacSL)
End Sub
Then i have got a diffrent exception: IServiceLocator not registered etc...
I have not a complately solutions for changing mef's ioc container because its own container types and uses her own extensibility. Tried to use Autofac.Integration.Mef but maybe it not future compatible. maybe not developep when mef' new releases.
I am a big blakc hole i think. Is there a any way can't i see?
Thanks.

DispatcherObject cast woes and Async / ObservableCollection issues in WPF

The code below pulls out a bunch of records from an Access 2010 database; hence rolling my own connector bits. I've succeeded in doing the observablecollection and made it all bind up with nice drag and drop data sources, from my own objects. However, like a daft person, I want to do this Asynchronously. Yet, I've got a small cast monster problem, and I don't know what to feed it! Can anyone advise me - I've tried a lot of reading around, but the concepts are just a little too many at once on a Friday afternoon and I'm struggling to make any real headway.
The line I'm having trouble with is:
Dim dispatcherObject As DispatcherObject = CType (handler.Target, DispatcherObject )
The exception is:
Unable to cast object of type '_Closure$__2[SomeRecord_Viewer.SomeRecord]' to type 'System.Windows.Threading.DispatcherObject'.
I've managed to make a WPF listbox populate via the code below, however only by commenting out a part of the ObservableCollectionEx class. This causes synchronisation problems and a crash after a few hundred records are entered.
Class that builds the threaded list of entities - in this case, an ObservableCollectionEx(Of SomeRecord):
Class SomeRecordSet
Inherits ObservableCollectionEx( Of SomeRecord)
Private Shared Property _SomeRecordList As New ObservableCollectionEx(Of SomeRecord )
Public Shared ReadOnly Property SomeRecordList As ObservableCollectionEx(Of SomeRecord )
Get
If _SomeRecordList.Count = 0 Then BuildSomeRecordListAsync()
Return _SomeRecordList
End Get
End Property
Public Shared ReadOnly Property ReturnSingleSomeRecord(id As Integer) As SomeRecord
Get
Return ( From SomeRecord In _SomeRecordList Where SomeRecord.id = id Select SomeRecord).First()
End Get
End Property
Private Shared Async Sub BuildSomeRecordListAsync()
Await Task.Run( Sub() BuildSomeRecordList())
Return
End Sub
Private Shared Sub BuildSomeRecordList()
Db.newcmd( "Select * from RecordList ")
While Db.read
Dim SomeRecord As New SomeRecord
With SomeRecord
.id = Db.dbint( "ID")
.type = Db.dbin( "type")
End With
_SomeRecordList.Add(SomeRecord)
End While
End Sub`
Partial code for the SomeRecord class:
Class SomeRecord
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged( ByVal info As String)
RaiseEvent PropertyChanged(Me , New PropertyChangedEventArgs (info))
End Sub
...'lots of simple properties.
End Class
The threaded collection class code - translated from another online source.
'I use PostSharp for try catch stuff.
`
Public Class ObservableCollectionEx (Of T )
Inherits ObservableCollection( Of T)
' Override the event so this class can access it
Public Shadows Event CollectionChanged As System.Collections.Specialized.NotifyCollectionChangedEventHandler
Protected Overrides Sub OnCollectionChanged( ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs )
Using BlockReentrancy()
Dim eventHandler As System.Collections.Specialized.NotifyCollectionChangedEventHandler = Sub () RaiseEvent CollectionChanged(Me , e)
If (eventHandler Is Nothing) Then Return
Dim delegates() As [Delegate] = eventHandler.GetInvocationList
*******If I comment this out I can populate the Listbox via a CollectionView, however it dies with issues to do with the list not staying synchronised :).
'Walk thru invocation list
For Each handler As System.Collections.Specialized.NotifyCollectionChangedEventHandler In delegates
Dim dispatcherObject As DispatcherObject = CType (handler.Target, DispatcherObject)
' If the subscriber is a DispatcherObject and different thread
If (( Not (dispatcherObject) Is Nothing) AndAlso (dispatcherObject.CheckAccess = False )) Then
' Invoke handler in the target dispatcher's thread
dispatcherObject.Dispatcher.Invoke(DispatcherPriority .DataBind, handler, Me, e)
Else
handler( Me, e)
End If
Next
*******End of stuff I comment out to get working partially***
End Using
End Sub
End Class
From what I can see, you have two problems.
You're assigning the local variable eventHandler to an anonymous method, rather than the actual event handler. It should be:
Dim eventHandler As NotifyCollectionChangedEventHandler = CollectionChangedEvent
NB: You need to use CollectionChangedEvent in VB, not CollectionChanged.
You're using CType to cast the target to a DispatcherObject, which won't work if the target isn't a DispatcherObject. Use TryCast instead:
Dim dispatcherObject As DispatcherObject = TryCast(handler.Target, DispatcherObject)
You can also tidy up the test on the next line by using IsNot:
If dispatcherObject IsNot Nothing AndAlso Not dispatcherObject.CheckAccess Then
WARNING - The code below acts differently to the C# version. The key difference seems to be that in VB you can't Override an Event (Why on earth not?) yet in C# you can.
The result is the Handler is Nothing in VB but not in C# :(.
So the syntax builds without error but the VB version doesn't ever do anything.
Redone with the updated answer in VB. Thank you!
Note I cannot make this work with Entity Framework, yet. But I think that a me and EF issue, not the collection.
The code itself is here for anyone interested. My list DOES populate perfectly fine now. However, I would take this answer of mine with a small pinch of salt until I update saying how I've extensively tested perhaps :)
However the omens are good - here is the original C# author's site: Original Site
Public Class ObservableCollectionEx(Of T)
Inherits ObservableCollection(Of T)
'Override the event so this class can access it
Public Shadows Event CollectionChanged As NotifyCollectionChangedEventHandler
Protected Overrides Sub OnCollectionChanged(ByVal e As NotifyCollectionChangedEventArgs)
Using BlockReentrancy()
Dim eventHandler As System.Collections.Specialized.NotifyCollectionChangedEventHandler = CollectionChangedEvent
If eventHandler Is Nothing Then
Return
End If
Dim delegates() As [Delegate] = CollectionChangedEvent.GetInvocationList
'Walk thru invocation list
For Each handler As NotifyCollectionChangedEventHandler In delegates
Dim dispatcherObject As DispatcherObject = TryCast(handler.Target, DispatcherObject)
' If the subscriber is a DispatcherObject and different thread
If dispatcherObject IsNot Nothing AndAlso Not dispatcherObject.CheckAccess Then
' Invoke handler in the target dispatcher's thread
dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, Me, e)
Else
handler(Me, e)
End If
Next
End Using
End Sub
End Class

WinForms: How to get a reference to a Loaded Form

I have a Windows Forms Application, that starts a Thread when loading. The thread starts a ServiceHost which exposes a WCF service.
Here the code who start the Thread:
Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim th As New Thread(AddressOf (New Ecoute).Ecouter)
th.Start()
End Sub
Here the code who create the service Host
Sub Ecouter()
host = New ServiceHost(GetType(ServiceEmulateur3270))
host.AddServiceEndpoint(GetType(IEmulateur3270), New NetNamedPipeBinding(), "net.pipe://localhost")
host.Open()
End Sub
When I receive a WCF call, in the service's called method, I want to update a control in the main form of my application. How can I get a reference to this Form ?
I found this Winforms, get form instance by form name but this show how to create a new instance of the form, but I want to get the currently loaded form instead.
OrElse, is it possible for the Thread to pass a reference of the form to the WCF service class ?
You should use the ParameterizedThreadStart constructor and pass the Form instance for example, something like this:
Dim th As New Thread(AddressOf (New Ecoute).Ecouter)
th.Start(this)
Sub Ecouter(ByVal data As Object)
' you can safely cast data as a Form
End Sub

How to Implement Entity Framework on this example?

I have been working thru this WPF example and am trying to hook it up to my database using Entity Framework but am confused on how to do this. Can someone please offer some guidance on how this would be done?
The code has the following in CustomerRepository.cs
static List<Customer> LoadCustomers(string customerDataFile)
{
// In a real application, the data would come from an external source,
// but for this demo let's keep things simple and use a resource file.
using (Stream stream = GetResourceStream(customerDataFile))
using (XmlReader xmlRdr = new XmlTextReader(stream))
return
(from customerElem in XDocument.Load(xmlRdr).Element("customers").Elements("customer")
select Customer.CreateCustomer(
(double)customerElem.Attribute("totalSales"),
(string)customerElem.Attribute("firstName"),
(string)customerElem.Attribute("lastName"),
(bool)customerElem.Attribute("isCompany"),
(string)customerElem.Attribute("email")
)).ToList();
}
which is where I assume the hook to the database would happen but not sure how. I can create the Model.edmx file to connect to the database but not sure how to physically get the list of customers from the database.
Also, this example uses a List of Customers but most examples I have gone through use ObservableCollection for this type of data. Is one preferable over the other and why?
TIA,
Brian Enderle
My MVVM/EF projects typically load entities directly into the ViewModels or Into light collections in the view models. I don't create any kind of custom repository to sit between them.
Generally my ViewModels do one of two things,
Retrieves data on instancing
Takes an entity as a constructor argument.
When I retrieve data on instance, I generally use a background worker class, which queries the context, puts the results in a list, and passes the list out. The Work Completed method then puts the entities into viewmodels and puts the ViewModels in a ObservableCollection.
Similar to this:
Private WithEvents GetProjectsBackgroundWorker As BackgroundWorker
Private _Projects As New ObservableCollection(Of ProjectViewModel)
Public Sub New()
GetProjectsBackgroundWorker = New BackgroundWorker
GetProjectsBackgroundWorker.RunWorkerAsync()
End Sub
Public Property Projects() As ObservableCollection(Of ProjectViewModel)
Get
Return _Projects
End Get
Set(ByVal value As ObservableCollection(Of ProjectViewModel))
_Projects = value
End Set
End Property
#Region " GetProjects"
Private Sub GetProjectsBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles GetProjectsBackgroundWorker.DoWork
Dim results = From proj As Project In Context.Projects Where proj.flgActive = True And proj.flgReview = True Select proj
Dim ProjList As New List(Of Project)(results)
e.Result = ProjList
End Sub
Private Sub GetProjectsBackgroundWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles GetProjectsBackgroundWorker.RunWorkerCompleted
If e.Error Is Nothing Then
For Each p As Project In e.Result
Projects.Add(New ProjectViewModel(p, Context))
Next
Else
End If
End Sub
#End Region
In ViewModels that take an entity as an argument, they often take a context as argument, especially if something is a short time-span operation. Otherwise I detach entities from the context in the even something goes hay-wire with the database or the connection is lost or something.
To answer your second question, ObservableCollections are Enumerable collections that have collection change notification implemented. The collection will notify the UI when a new item is added/removed/moved. Typically any time I have entities that are going to be viewed or displayed in the UI, I host them in an Observable Collection. Otherwise I use something simpler, a List normally.

Resources