Question for PowerShell classes

This topic contains 2 replies, has 2 voices, and was last updated by  Andy Mishechkin 3 weeks, 2 days ago.

  • Author
    Posts
  • #82835

    Andy Mishechkin
    Participant

    Hello, All!

    I want to create the PowerShell class for working with SQL Server (primarily for investigation of this technology).
    I have defined the next class properties:

    class SqlExec
    {
        [string] [ValidateNotNullOrEmpty()] $Server
        [string] [ValidateNotNullOrEmpty()] $Db 
        [string] [AllowNull()] $User 
        [string] [AllowNull()] $Password
        [int] [AllowNull()] $Port = 1433
        ...
    }
    

    But I can have the different sets of SQL Server parameters:
    – the full set
    – all parameters except port (will be using the default value 1433)
    – Server and Db only (will be using 'Integrated Security=True' and default port instead of SQL Server authentication )
    – Server, DB and port (will be using 'Integrated Security=True' and specified port)
    What is the best way to create handling of these parameter sets ? Should I create four constructors for each case (it seems to me cumbersome) or to create class without any constructors (IMHO it's not right) ?

  • #82840

    Jeremy Murrah
    Participant

    So the thing with classes is to think of them not as a way to encapsulate a task, like a cmdlet would do, but a way to encapsulate and object. In the case of your SQL example, you might try a class to represent a database or an instance. In order to retrieve a database or instance you would need to perform a connect task, so that could be represented by a connect() method on the database class. If you organize it like that your methods can be treated alot more like the traditional task-based functions, and for the multiple connection options you can do overloads. An overload is basically just the same method defined twice, but with different parameter sets and possibly different execution code, but that returns more or less the same kind of thing. So something kind of like this:

    class SQLDatabase
    {
        [String] $Server
        [String] $DB
    
        static [SQLDatabase] Connect(
            [string] $Server,
            [string] $db
        ){
            #use integrated security
            #use default value for port
            #build connection string and connect to database
    
    
        }
    
        static [SQLDatabase] Connect(
            [string] $Server,
            [string] $db,
            [string] $User,
            [String] $Password
        ){
            #use default value for port
            #build connection string and connect to database
    
        }
        static [SQLDatabase] Connect(
            [string] $Server,
            [string] $db,
            [string] $User,
            [String] $Password,
            [String] $Port
        ){
            #build connection string and connect to database
        }
    #constructor
        SQLDatabase(
            [String] $Server,
            [String] $DB
        ){
            $this.Server = $Server
            $this.DB = $DB
        }
    }
    

    You could also define a class for the actual connection object, but the takeaway here is that the constructors are just for the instantiation of the class into an object locally in your powershell session. Use a method or methods to do any actual work or connection out to something else. Atleast that's my thought on it anyway.

You must be logged in to reply to this topic.