[VB.NET] Unity Register All Form

Nguyen Minh 1 Reputation point
2020-12-21T10:03:28.147+00:00

Hi all,

I have something like this when try to register all type without specify each class (using AllClasses, WithMappings from Unity.RegistrationByConvention ):

Dim injections As IEnumerable(Of InjectionMember) = New List(Of InjectionMember) From {
         New Interceptor(Of InterfaceInterceptor),
         New InterceptionBehavior(Of PolicyInjectionBehavior)
        }

Dim allTypes = AllClasses.FromAssemblies(GetType(MyBaseService).Assembly)
unity.RegisterTypes(allTypes, AddressOf WithMappings.FromMatchingInterface, getInjectionMembers:=Function(t) injections)

It works so far. But when I try the same thing to register with all Form:

Dim allForm = AllClasses.FromAssemblies(GetType(Form).Assembly)
unity.RegisterTypes(allForm, AddressOf WithMappings.None, getInjectionMembers:=Function(t) injections)

There is an Exception:

ArgumentException: Type passed must be an interface.

Currently, I have to register for each Form like this:
unity.RegisterType(Of MyFormA)
unity.RegisterType(Of MyFormB)

Is there anyway I could register all form without specify each of them ?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,753 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2020-12-22T07:46:00.583+00:00

    I guess if you used the MVP UI design pattern that implements an Interface for each form, you could instance the forms with unity all at once through the IoC.

    https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

    I suggest that you watch all the shows and get a basic understanding before you try to implement the Windows form VB.NET usage of MVP.

    http://polymorphicpodcast.com/shows/mv-patterns/

    Other than that, I don't see how you can do it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.