Maintaining Connection Strings Across Modules in a WPF MVVM Pattern
-
sexta-feira, 13 de julho de 2012 18:53I have a WPF application using MVVM with Prism and Unity. My loging screen obtains connection strings from the module's app.config and puts them into a combox so that the user can select the connection . I need to persist the string so that it is discoverable by Views that are injected into the main screen. From my reading, I THINK that I have learned that using a Singleton is the best way to accomplish this, but I am not sure. I have read about Singletons in an abstract sense, but I don't have a handle on how it would be used to accomplish my specific goal. So I have these questions: 1) Is a Singleton the best way to persist a string value across injected views/view modes?; 2) If not, then what?; 3) Can you point me to an example that is solves a problem similar to the one I have presented?
REvans
- Editado REvans611 sexta-feira, 13 de julho de 2012 19:14 added detail
Todas as Respostas
-
sábado, 14 de julho de 2012 15:48
-
sábado, 14 de julho de 2012 16:30I think it has more to do with general .NET since there are no global values in C#, but I asked a similar question in the Prism forum and was told that since it related to SQL Server, I should post it here.
REvans
-
domingo, 15 de julho de 2012 08:08Why don't you save the connection string in the application settings, it's easy to use and to maintain, if e.g. the server name changes?
Olaf Helper
* cogito ergo sum * errare humanum est * quote erat demonstrandum *
Wenn ich denke, ist das ein Fehler und das beweise ich täglich
Blog Xing -
domingo, 15 de julho de 2012 10:47Moderador
Hello REvans611,
I am surprised by what you have written "there are no global values in C#". It is false : it is called static in VC# and Shared in VB. You have only to declare in a class with a public attribute.
You should see :
for C# : http://msdn.microsoft.com/en-us/library/98f28cdx.aspx
for VB : http://msdn.microsoft.com/en-us/library/zc2b427x.aspx
I gave the link towards VB as it could be useful for developers in VB ( i think it is not the 1st time that your question is asked in this forum )
About to save the connection string in the application settings , i am absolutely against : it is too easy to see this connection string , to modify/hijack it.It is breaking a basic rule : to avoid to show a connection string.
For myself, i am always using a connection form a little like the connection form used to connect in the SQL Server Management Studio (SSMS). I fill a listbox of available SQL Server instances with a call to the SqlDataSourceEnumerator class..
http://msdn.microsoft.com/en-us/library/system.data.sql.sqldatasourceenumerator(VS.90).aspx
http://msdn.microsoft.com/en-us/library/a6t1z9x2(VS.90).aspx
2 advices :
- for some editions of SQL Server ( mainly Express ), a single call to GetDataSources() is not enough, it is why in my programs, i code twice this call
- check whether the Version column is not set to empty or null.Usually, it means that the related SQL Server Service is not running.
For myself, i prefer to use http://msdn.microsoft.com/en-us/library/ms210350(SQL.100).aspx ( surely because i am mainly writing programs needing SMO ). The returned datatable is more "complete" ( with especially the IsClustered and Locale columns ). Moreover, it seems ( according my own experience ) that the returned datable contains usually more rows with EnumAvailableSqlServers than with GetDataSources, but the SMO code is slower and i am not sure that WPF is authorizing the call to SMO classes ( maybe a problem with .Net Framework 2.0 )
Have a nice day
Mark Post as helpful if it provides any help.Otherwise,leave it as it is.
- Marcado como Resposta Iric WenModerator segunda-feira, 23 de julho de 2012 05:53
-
domingo, 15 de julho de 2012 13:44
I am surprised by what you have written "there are no global values in C#". It is false
Hello Papy,
I hope you enjoyed your vacation.
He didn't wrote about a "value", he mean a "global variable". In VB.NET you can define a global variable and you can use/access it in every class
Public Modul TestModul Public MyGlobalVar as String = "Hello World" End ModulBut this is an old relict from VB 6.0 version. Internal .NET map this to a shared class with a static variable.
In C# (as well as VB.NET) you can use a Singleton (from patter&pratice group) to accomplish the same behaviour. See also Implementing Singleton in C#
And why not storing a connection string into appilcation settings, you can easily decryt/encryt it.
Olaf Helper
* cogito ergo sum * errare humanum est * quote erat demonstrandum *
Wenn ich denke, ist das ein Fehler und das beweise ich täglich
Blog Xing- Marcado como Resposta Iric WenModerator segunda-feira, 23 de julho de 2012 05:53
-
domingo, 15 de julho de 2012 15:20
Hmmm. Seems like you have to have a global variable in order to have a global value. I have read various posts saying that there are no global values in C# while suggesting a look into a static variable. Maybe this is just an inadvertant exercise in symantics, and maybe I am confusing myself by overthinking this because of the MVVM/Prism/Unity context. I will follow the suggested links and experiment with a static variable and a singleton and report back my findings when I am successful. Maybe it will help some other forum searchers. Thank you, all. I am very appreciative of your input.
REvans

