Re: [Mono-list] Questions about coding style

2012-08-21 Thread Jonathan Pryor
On Aug 20, 2012, at 3:28 PM, Philippe Grohrock philippe.grohr...@gmail.com 
wrote:
 Thanks for the reply already and I'm sorry, I should've added the lines of 
 code.
 
   static class GlobalVariables
   {
   public static MySqlConnection connection = new connection();
   }
 
 This way the whole program has access to it and can modify/query the DB when 
 needed (this is what I meant with global).

I believe that this is a Bad Idea™.

Firstly, this is counter to ~every MSDN example on using connections, which 
always scopes the Connection instance:

// 
http://msdn.microsoft.com/en-us/library/ff647768.aspx#scalenetchapt12_topic9
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
// ...
}

This implies that you should instead do:

static class Database {

internal static MySqlConnection CreateConnection ()
{
return new connection ();
}
}

And narrowly scope your use:

using (var c = Database.CreateConnection ()) {
c.Open ();
// ...
}

Now, _why_ should you do this? Unfortunately I can't find anything to confirm 
or deny the following, but this is my recollection from using SQL many years 
ago...

The reason why is connection-related errors: if (when) you hit a network 
interruption, the DbConnection instance is unusable afterward, even if the 
network came back. (Though maybe I needed to .Close() and .Open() to repair the 
instance? I no longer remember.) I found that the easiest/sanest way to go was 
to just recreate the Connection instance when needed, and Dispose() of it as 
soon as possible (relying on lower-level connection pooling if possible).

 - Jon

___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


Re: [Mono-list] Questions about coding style

2012-08-21 Thread Stifu
To add to what Jonathan said, trying to reuse the same connection for
everything can also lead to concurrent access issues, if more than one
thread accesses the connection at a given time. I've seen such issues when
using Entity Framework. Besides, creating a new connection is really cheap.


Jonathan Pryor wrote
 
 On Aug 20, 2012, at 3:28 PM, Philippe Grohrock lt;philippe.grohrock@gt;
 wrote:
 Thanks for the reply already and I'm sorry, I should've added the lines
 of code.
 
  static class GlobalVariables
  {
  public static MySqlConnection connection = new connection();
  }
 
 This way the whole program has access to it and can modify/query the DB
 when needed (this is what I meant with global).
 
 I believe that this is a Bad Idea™.
 
 Firstly, this is counter to ~every MSDN example on using connections,
 which always scopes the Connection instance:
 
   //
 http://msdn.microsoft.com/en-us/library/ff647768.aspx#scalenetchapt12_topic9
   using (SqlConnection conn = new SqlConnection(connString))
   {
   conn.Open();
   // ...
   }
 
 This implies that you should instead do:
 
   static class Database {
 
   internal static MySqlConnection CreateConnection ()
   {
   return new connection ();
   }
   }
 
 And narrowly scope your use:
 
   using (var c = Database.CreateConnection ()) {
   c.Open ();
   // ...
   }
 
 Now, _why_ should you do this? Unfortunately I can't find anything to
 confirm or deny the following, but this is my recollection from using SQL
 many years ago...
 
 The reason why is connection-related errors: if (when) you hit a network
 interruption, the DbConnection instance is unusable afterward, even if the
 network came back. (Though maybe I needed to .Close() and .Open() to
 repair the instance? I no longer remember.) I found that the
 easiest/sanest way to go was to just recreate the Connection instance when
 needed, and Dispose() of it as soon as possible (relying on lower-level
 connection pooling if possible).
 
  - Jon
 
 ___
 Mono-list maillist  -  Mono-list@.ximian
 http://lists.ximian.com/mailman/listinfo/mono-list
 




--
View this message in context: 
http://mono.1490590.n4.nabble.com/Questions-about-coding-style-tp4656301p4656333.html
Sent from the Mono - General mailing list archive at Nabble.com.
___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


Re: [Mono-list] Questions about coding style

2012-08-20 Thread Jonathan Pryor
On Aug 17, 2012, at 5:49 PM, Philippe Grohrock philippe.grohr...@gmail.com 
wrote:
 1. Is it bad/good style to have a public class that implements global 
 variables?

How do you define global variable? :-)

`public static` fields are Very Bad™, unless they're `readonly`. This is 
because there's no way to prevent modification of the field from anything else 
in your process.

`public static` properties are (generally) fine, and are common in the Base 
Class Library.

Best, though, is to keep things `internal` or `private` unless they _really_ 
need to be public.

 My program readys a DB connection right at the start which is queried by 
 multiple different GTK windows.

This is a case where `internal` should normally be preferred over `public`, 
unless another assembly in your app needs access to the value.

 - Jon

___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


Re: [Mono-list] Questions about coding style

2012-08-20 Thread Philippe Grohrock
Thanks for the reply already and I'm sorry, I should've added the lines of
code.



This way the whole program has access to it and can modify/query the DB when
needed (this is what I meant with global). At the moment I have 2 of my
windows using that connection, but there might be more in the future. So is
this a bad way of doing it? Would it be better to have the connection be
internal static? Should I declare it once after the start of the program and
then hand it over to the windows?

I think I also need to open a new topic about assemblies ;)



--
View this message in context: 
http://mono.1490590.n4.nabble.com/Questions-about-coding-style-tp4656301p4656321.html
Sent from the Mono - General mailing list archive at Nabble.com.___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


Re: [Mono-list] Questions about coding style

2012-08-20 Thread James Wright


 I'd create ConnectionFactory class, although feel free to ignore me, 
just thinking off the top of my head! :-)


public class ConnectionFactory
{
private static MySqlConnection _connection;
public MySqlConnection GetConnection()
{
if (_connection == null)
{
_connection = new MySqlConnection();
}
return _connection;
}
}



On 20/08/2012 20:28, Philippe Grohrock wrote:
Thanks for the reply already and I'm sorry, I should've added the 
lines of code.

static class GlobalVariables
{
public static MySqlConnection connection = new connection();
}
This way the whole program has access to it and can modify/query the 
DB when needed (this is what I meant with global). At the moment I 
have 2 of my windows using that connection, but there might be more in 
the future. So is this a bad way of doing it? Would it be better to 
have the connection be internal static? Should I declare it once after 
the start of the program and then hand it over to the windows? I think 
I also need to open a new topic about assemblies ;)


View this message in context: Re: Questions about coding style 
http://mono.1490590.n4.nabble.com/Questions-about-coding-style-tp4656301p4656321.html
Sent from the Mono - General mailing list archive 
http://mono.1490590.n4.nabble.com/Mono-General-f1490591.html at 
Nabble.com.



___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


Re: [Mono-list] Questions about coding style

2012-08-20 Thread IBBoard
Just in case no-one else spots it, you'll need a static keyword on 
that GetConnection() declaration, otherwise you have to create an 
instance to get a reference to the private static variable, which is 
just odd :)




On 20/08/12 20:45, James Wright wrote:


  I'd create ConnectionFactory class, although feel free to ignore me,
just thinking off the top of my head! :-)

public class ConnectionFactory
{
 private static MySqlConnection _connection;
 public MySqlConnection GetConnection()
 {
 if (_connection == null)
 {
 _connection = new MySqlConnection();
 }
 return _connection;
 }
}



On 20/08/2012 20:28, Philippe Grohrock wrote:

Thanks for the reply already and I'm sorry, I should've added the
lines of code.
static class GlobalVariables
{
public static MySqlConnection connection = new connection();
}
This way the whole program has access to it and can modify/query the
DB when needed (this is what I meant with global). At the moment I
have 2 of my windows using that connection, but there might be more in
the future. So is this a bad way of doing it? Would it be better to
have the connection be internal static? Should I declare it once after
the start of the program and then hand it over to the windows? I think
I also need to open a new topic about assemblies ;)

View this message in context: Re: Questions about coding style
http://mono.1490590.n4.nabble.com/Questions-about-coding-style-tp4656301p4656321.html
Sent from the Mono - General mailing list archive
http://mono.1490590.n4.nabble.com/Mono-General-f1490591.html at
Nabble.com.


___
Mono-list maillist  -Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list




___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


Re: [Mono-list] Questions about coding style

2012-08-20 Thread Gabriel Ibanez
Be carefull about this way of working with DB. You must free the connections as 
fast as you can and I'm not sure you can do like this way. 

What happen if you dispose the connection ? That would be the usual way of 
freeing the queue (or thru the 'using' blocks).

/Gabriel

-Original Message-
From: Philippe Grohrock philippe.grohr...@gmail.com
Date: Mon, 20 Aug 2012 19:28:07 
To: mono-list@lists.ximian.com
Subject: Re: [Mono-list] Questions about coding style

Thanks for the reply already and I'm sorry, I should've added the lines of 
code.static class GlobalVariables{   public static 
MySqlConnection connection = new connection();} This way the whole program 
has access to it and can modify/query the DB when needed (this is what I meant 
with global). At the moment I have 2 of my windows using that connection, but 
there might be more in the future. So is this a bad way of doing it? Would it 
be better to have the connection be internal static? Should I declare it once 
after the start of the program and then hand it over to the windows? I think I 
also need to open a new topic about assemblies ;)   


View this message in context: Re: Questions about coding style 
http://mono.1490590.n4.nabble.com/Questions-about-coding-style-tp4656301p4656321.html
 
Sent from the Mono - General mailing list archive 
http://mono.1490590.n4.nabble.com/Mono-General-f1490591.html  at Nabble.com.
___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


Re: [Mono-list] Questions about coding style

2012-08-20 Thread Philippe Grohrock
 Be carefull about this way of working with DB. You must free the
 connections as fast as you can and I'm not sure you can do like this way.


Basically the connection gets initialised when the application starts and
every time I need a query, the connection gets opened, queried, closed, but
not disposed. Connection.dispose() happens right before the application
quits


 What happen if you dispose the connection ? That would be the usual way of
 freeing the queue (or thru the 'using' blocks).


My idea was to never dispose the connection to make the code a bit lighter.
That way I don't have to re-initialise the connection each time I need a
single query. Instead I have one connection with pooling enabled that gets
opened and closed when I need it.

Now my problem is, that even there I'm not sure if this is a good way of
dealing with that problem, it's just the idea to not create and dispose the
connection for each query that lead me to do things this way. But in the
end, does that even make a difference in terms of connection speed and
resource usage?

Forgive me for asking such basic questions, but when it comes to
organisation of code and using resources wisely I'm still learning.




--
View this message in context: 
http://mono.1490590.n4.nabble.com/Questions-about-coding-style-tp4656301p4656325.html
Sent from the Mono - General mailing list archive at Nabble.com.___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


Re: [Mono-list] Questions about coding style

2012-08-20 Thread James Wright


  Unless I'm mistaken .NET connections are pooled for you, so I don't 
think you'll be gaining much from keeping one instance of the connection 
always open as the framework is already doing just that. My advice is 
don't worry about it until you have to... chances are it won't be a 
problem in the real world!



On 20/08/2012 22:01, Philippe Grohrock wrote:


Be carefull about this way of working with DB. You must free the
connections as fast as you can and I'm not sure you can do like
this way. 



Basically the connection gets initialised when the application starts 
and every time I need a query, the connection gets opened, queried, 
closed, but not disposed. Connection.dispose() happens right before 
the application quits


What happen if you dispose the connection ? That would be the
usual way of freeing the queue (or thru the 'using' blocks).


My idea was to never dispose the connection to make the code a bit 
lighter. That way I don't have to re-initialise the connection each 
time I need a single query. Instead I have one connection with pooling 
enabled that gets opened and closed when I need it.


Now my problem is, that even there I'm not sure if this is a good way 
of dealing with that problem, it's just the idea to not create and 
dispose the connection for each query that lead me to do things this 
way. But in the end, does that even make a difference in terms of 
connection speed and resource usage?


Forgive me for asking such basic questions, but when it comes to 
organisation of code and using resources wisely I'm still learning.



View this message in context: Re: Questions about coding style 
http://mono.1490590.n4.nabble.com/Questions-about-coding-style-tp4656301p4656325.html
Sent from the Mono - General mailing list archive 
http://mono.1490590.n4.nabble.com/Mono-General-f1490591.html at 
Nabble.com.



___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


Re: [Mono-list] Questions about coding style

2012-08-20 Thread Philippe Grohrock

  Unless I'm mistaken .NET connections are pooled for you, so I don't think
 you'll be gaining much from keeping one instance of the connection always
 open as the framework is already doing just that. My advice is don't worry
 about it until you have to... chances are it won't be a problem in the real
 world!


Connections only get pooled when pooling=true is set, but as far as I
understood it, this only works as long as the connection doesn't get
disposed.




--
View this message in context: 
http://mono.1490590.n4.nabble.com/Questions-about-coding-style-tp4656301p4656327.html
Sent from the Mono - General mailing list archive at Nabble.com.___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list