Requirements icon indicating copy to clipboard operation
Requirements copied to clipboard

Replace `Import-Module` and `.` with `using module`

Open chriskuech opened this issue 6 years ago • 1 comments

We heavily rely on classes, which don't integrate well with Import-Module, which forces us to dot source a lot. using module should solve our import woes.

chriskuech avatar Nov 27 '19 01:11 chriskuech

Hi @chriskuech Be carefull, as that would expose all your classes to the end user, and they could end up polluting his session. It is possible to export classes by using a 'hybrid approach'. When you boil it down to the bare minimum, it is nothing more than wrapping the instantiation of a class in a function starting with the 'new-' keyword. Example, if you have a class called CustomRequirement you can do this.


Function New-CustomRequirement {
   Param(

    )
    return [CustomRequirement]::New() #possible to work with overloaded constructors and parameter sets.
}

Stephanevg avatar Feb 10 '20 22:02 Stephanevg