Am I working on the right lines... (long) 
Author Message
 Am I working on the right lines... (long)

I'm not totally sure if this is the correct group for this... but the
following code is something I've put together, it was originally taken
from the example ISpy when I down loaded matrix, I've modded it to be
more extendable, and if this "looks ok" then my intention is to create
a higher level class that has some of the methods (such as
initialisesqlconnection) in it... then all of my data subclasses can
just inherit this functionality.

I'm quite chuffed that it actually works :-) but working on my own
means that I have no peers to review my code... so if anyone has a few
minutes to spare could they give me a few "well if i was doing it..."

The original ISpy code had no (or very little) re-use in the class
structures (in fact every class was extended from object) so i've
applied a lot of refactorings (Martin Fowler, GOF, et al.) as I am
also learning that!, to try and remove a mass of duplicated code and
long functions.

Quote:
>>>>>>>CODE BEGINS

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace I400S.{*filter*}s_R_Us {

    //*******************************************************
    //Genders Class: Gender type specific list
    //*******************************************************
    public class Genders: CollectionBase {

        public Gender this[int index] {
            get {
                return ((Gender)(this.List[index]));
            }
            set {
                this.List[index] = value;
            }
        }

        public int Add(Gender value) {
            return this.List.Add(value);
        }

        public void Insert(int index , Gender value) {
            this.List.Insert(index, value);
        }

        public int IndexOf(Gender value) {
            return this.List.IndexOf(value);
        }

        public bool Contains(Gender value) {
            return  this.List.Contains(value);
        }

        public void Remove(Gender value) {
            this.List.Remove(value);
        }

        public void CopyTo(Gender[] array, int index) {
            this.List.CopyTo(array, index);
        }    

    }

    //*******************************************************
    //Gender Class:
    //*******************************************************
    public class Gender {
        private Int32 objectIdentifier;
        private String gender;

        public Int32 ObjectIdentifier {
            get {
                return objectIdentifier;
            }
            set {
                objectIdentifier = value;
            }
        }

        public String Text {
            get {
                return gender;
            }
            set {
                gender = value;
            }
        }
    }

    // *******************************************************
    // GenderDB Class: Creates and returns Genders
    // *******************************************************
    public class GenderDB {
        private SqlDataReader sqlDataReader;
        private SqlCommand sqlCommand;
        private SqlConnection sqlConnection;

        private void InitialiseSqlConnection() {
            sqlConnection = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
        }

        private void InitialiseSqlCommand (String sqlString) {
            InitialiseSqlConnection();
            sqlCommand = new SqlCommand(sqlString, sqlConnection);
        }

        private void ExecuteSqlCommand() {
            sqlConnection.Open();
            sqlDataReader =
sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }

        private void FixUpObject (object value) {
            Gender gender = value as Gender;
            gender.ObjectIdentifier = sqlDataReader.GetInt32(0);
            gender.Text = sqlDataReader.GetString(2);
            value = gender;
        }

        public Genders GetGender() {

            InitialiseSqlCommand("SELECT * From WDBGender ORDER BY
Sequence");
            ExecuteSqlCommand();

            Genders result = new Genders();

            while (sqlDataReader.Read()) {
                Gender myGender = new Gender();
                FixUpObject(myGender);
                result.Add(myGender);
            }

            return result;
        }

        public Gender GetGender(int objectIdentifier) {

            InitialiseSqlCommand("SELECT * FROm WDBGender WHERE

            SqlParameter sqlparameter = new

            sqlparameter.Value = objectIdentifier;
            sqlCommand.Parameters.Add(sqlparameter);

            ExecuteSqlCommand();

            Gender result;

             if (sqlDataReader.Read()) {
                result = new Gender();
                FixUpObject(result);
             }
             else {
                result = null;
            }

            return result;
        }

    }

Quote:
}
>>>>>>> CODE ENDS

Jonathan Wilson,
AS/400 Consultant. (UK)
+44 (0)115 9192648
ASP/VBScript/Delphi/JScript Newbe


Sat, 12 Feb 2005 05:50:52 GMT  
 Am I working on the right lines... (long)
Jonathan,

Everything looks fine.  The only thing I can suggest is adding try blocks to
the SQL queries.

Ryan Dawson


Quote:
> I'm not totally sure if this is the correct group for this... but the
> following code is something I've put together, it was originally taken
> from the example ISpy when I down loaded matrix, I've modded it to be
> more extendable, and if this "looks ok" then my intention is to create
> a higher level class that has some of the methods (such as
> initialisesqlconnection) in it... then all of my data subclasses can
> just inherit this functionality.

> I'm quite chuffed that it actually works :-) but working on my own
> means that I have no peers to review my code... so if anyone has a few
> minutes to spare could they give me a few "well if i was doing it..."

> The original ISpy code had no (or very little) re-use in the class
> structures (in fact every class was extended from object) so i've
> applied a lot of refactorings (Martin Fowler, GOF, et al.) as I am
> also learning that!, to try and remove a mass of duplicated code and
> long functions.

> >>>>>>>CODE BEGINS

> using System;
> using System.Configuration;
> using System.Data;
> using System.Data.SqlClient;
> using System.Collections;

> namespace I400S.{*filter*}s_R_Us {

>     //*******************************************************
>     //Genders Class: Gender type specific list
>     //*******************************************************
>     public class Genders: CollectionBase {

>         public Gender this[int index] {
>             get {
>                 return ((Gender)(this.List[index]));
>             }
>             set {
>                 this.List[index] = value;
>             }
>         }

>         public int Add(Gender value) {
>             return this.List.Add(value);
>         }

>         public void Insert(int index , Gender value) {
>             this.List.Insert(index, value);
>         }

>         public int IndexOf(Gender value) {
>             return this.List.IndexOf(value);
>         }

>         public bool Contains(Gender value) {
>             return  this.List.Contains(value);
>         }

>         public void Remove(Gender value) {
>             this.List.Remove(value);
>         }

>         public void CopyTo(Gender[] array, int index) {
>             this.List.CopyTo(array, index);
>         }

>     }

>     //*******************************************************
>     //Gender Class:
>     //*******************************************************
>     public class Gender {
>         private Int32 objectIdentifier;
>         private String gender;

>         public Int32 ObjectIdentifier {
>             get {
>                 return objectIdentifier;
>             }
>             set {
>                 objectIdentifier = value;
>             }
>         }

>         public String Text {
>             get {
>                 return gender;
>             }
>             set {
>                 gender = value;
>             }
>         }
>     }

>     // *******************************************************
>     // GenderDB Class: Creates and returns Genders
>     // *******************************************************
>     public class GenderDB {
>         private SqlDataReader sqlDataReader;
>         private SqlCommand sqlCommand;
>         private SqlConnection sqlConnection;

>         private void InitialiseSqlConnection() {
>             sqlConnection = new
> SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
>         }

>         private void InitialiseSqlCommand (String sqlString) {
>             InitialiseSqlConnection();
>             sqlCommand = new SqlCommand(sqlString, sqlConnection);
>         }

>         private void ExecuteSqlCommand() {
>             sqlConnection.Open();
>             sqlDataReader =
> sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
>         }

>         private void FixUpObject (object value) {
>             Gender gender = value as Gender;
>             gender.ObjectIdentifier = sqlDataReader.GetInt32(0);
>             gender.Text = sqlDataReader.GetString(2);
>             value = gender;
>         }

>         public Genders GetGender() {

>             InitialiseSqlCommand("SELECT * From WDBGender ORDER BY
> Sequence");
>             ExecuteSqlCommand();

>             Genders result = new Genders();

>             while (sqlDataReader.Read()) {
>                 Gender myGender = new Gender();
>                 FixUpObject(myGender);
>                 result.Add(myGender);
>             }

>             return result;
>         }

>         public Gender GetGender(int objectIdentifier) {

>             InitialiseSqlCommand("SELECT * FROm WDBGender WHERE

>             SqlParameter sqlparameter = new

>             sqlparameter.Value = objectIdentifier;
>             sqlCommand.Parameters.Add(sqlparameter);

>             ExecuteSqlCommand();

>             Gender result;

>              if (sqlDataReader.Read()) {
>                 result = new Gender();
>                 FixUpObject(result);
>              }
>              else {
>                 result = null;
>             }

>             return result;
>         }

>     }
> }
> >>>>>>> CODE ENDS

> Jonathan Wilson,
> AS/400 Consultant. (UK)
> +44 (0)115 9192648
> ASP/VBScript/Delphi/JScript Newbe



Sat, 12 Feb 2005 10:00:07 GMT  
 Am I working on the right lines... (long)


Quote:
>Jonathan,

>Everything looks fine.  

*Phew! :-)

Its very difficult to know (as a newbe) if your code is ok, or if its
so unique to your personal style that any "normal-experienced"
programmer would not have a clue about modding it, and would instead
go for the re-write ;-)

Quote:
>The only thing I can suggest is adding try blocks to the SQL queries.

yeah, I guess I'm used to using a language that has in built exception
handling, so that even tho the user would have received an error they
would have been able to select a response (continue and ignore, close
program, produce memory dump, etc.) So i'm going to have to be more
careful about potential errors in C#

Quote:

>Ryan Dawson

Jonathan Wilson,
AS/400 Consultant. (UK)
+44 (0)115 9192648
ASP/VBScript/Delphi/JScript Newbe


Sat, 12 Feb 2005 17:51:20 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. char size (was long long long long long int)

2. Am I doing this right?

3. Am I heading right?

4. Window Classes: Am I doing this right?

5. Am I right wanting to learn C ?

6. CMutex - Am I doing this right?

7. CMutex - am I doing this right?

8. Customizing Open File Dialog -- am I right?

9. Customizing Open File Dialog -- am I right?

10. Am I right????

11. Am I using the right find algorithm?

12. long long long long integers

 

 
Powered by phpBB® Forum Software