Share Web Part property to instance class
-
Monday, December 01, 2008 5:09 PMClass A has a property TicketDataSource
How do I get access to the instance property TicketDataSource in another class?
The problem is I have a web part that has:
[Personalizable(), WebBrowsable, WebDisplayName("TicketDataSource")]
public string TicketDataSource
{
get { return this._TicketDataSource; }
set { this._TicketDataSource = value; }
}
But the actual call to the database is done in a instance class elsewhere. I cannot pass the connection string when I make the call because I am using a WebPart ObjectDataSource.
I get the feeling I am going about this the wrong way, but I am resisting putting the connection string in the web.config, which I suspect I will end up doing.
thanks- Edited by Mattaniah Monday, December 01, 2008 5:10 PM correct error
All Replies
-
Monday, December 01, 2008 5:17 PMModerator
Where's the connection string stored right now, and why don't you want to put it in the web.config? This would be the recommended solution, so you don't compile you connection string into your assembly. Compiling connection strings into assemblies is a bad move, especially for web applications. IIS doesn't serve .config files at all, so if you're talking about a web application, you're actually safer putting the connection string in the web.config than you are putting it in the DLL file directly. As much as you're resisting putting the connection string in the web.config, to go about this the right way, that would be the direction to take.
David Morton - http://blog.davemorton.net/ -
Monday, December 01, 2008 5:22 PMO.K. Forget it is a connectionstring. How would you share a string property between two classes in the same assembly?
Thanks -
Monday, December 01, 2008 5:33 PMModerator
You'll have to pass an instance of the class to the other class. You can do this via the constructor or via a public property on the other class that would be set. Either way, you'll have to "give" the class that needs to access the object access to the class. The following example is for the constructor.
public class OtherClass { private ClassA _classA; public OtherClass(ClassA classA) { _classA = classA; } public void UseTicketDataSource() { Console.WriteLine(_classA.TicketDataSource); } }
Here's with the property:
public class OtherClass { public ClassA ClassAInstance { get; set; } public void UseTicketDataSource() { if (ClassAInstance != null) Console.WriteLine(ClassAInstance.TicketDataSource); else Console.WriteLine("You never set the ClassAInstance"); } }
I believe the constructor would be preferable to the property, as the local reference to the instance of ClassA is more than likely required for your purposes.
Another option is to use the singleton pattern, if you only need one instance of ClassA.
public class ClassA { private static ClassA _instance; private ClassA() { } public static ClassA Instance { get { if (_instance == null) _instance = new ClassA(); return _instance; } } public string TicketDataSource { get; set; } }
Then you would only have one instance of ClassA in your entire application, and you could call:
ClassA.Instance.TicketDataSource
to retrieve the value.
In any case, since TicketDataSource is instance, you're going to have to "give" the reference to the calling class somehow.
David Morton - http://blog.davemorton.net/- Marked As Answer by Mattaniah Monday, December 01, 2008 5:34 PM
-
Monday, December 01, 2008 5:40 PMThanks.
I believe a web part class will need to have more than one instance as hundreds of people are hitting the page. I assume they would have to share the same one class instance -
Friday, September 21, 2012 1:48 PM
I would not recommend passing instances around. That creeps way deep into your application structure and logical design.
I generally use threadlocal for this. The crude way would be to store like this
CallContext.SetData("YOURKEY", value);and retrieve it where you need it like this
CallContext.GetData("YOURKEY");------------------------------------------------------------
I generally implement a wrapper extension (or a least an wrapper class when working in lower .net versions) that implements an interface like this
public class MyAppExecutionContext implements MyAppExecutionContextAware
{ private const String ContextKey = "MyOrg.MyProj.blah.blah.MyAppExecutionContext.MyAppExecutionContextInfo"; private MyAppExecutionContext(){} public static MyAppExecutionContext Instance { get { return new MyAppExecutionContext(); } } public RCPExecutionContextInfo MyAppExecutionContextInfo { get { return (RCPExecutionContextInfo)CallContext.GetData(ContextKey);} set { CallContext.SetData(ContextKey, value); } } } public class MyAppExecutionContextInfo { public string ConnectionString { get; set; } // more property that are needed in the thread local public MyAppExecutionContextInfo(){} public MyAppExecutionContextInfo(string connectionStr) { ConnectionString = connectionStr; } }

