S
S
Sergey Grigorov2017-05-09 11:58:43
WPF
Sergey Grigorov, 2017-05-09 11:58:43

"Global" class in C#?

Hello. I know, in the last couple of days I've been tormenting the community with my questions, but...:D
Wrote a class to work with MySQL (a kind of model) from C#.
The application itself is built like this: application form->frame. Pages are already loaded into the frame.
But the problem is that in these same pages, the class for working with MySQL needs to be re-declared - and this is the creation of a new connection, extra code and other hassle.
How to make a class "global" - for example, to declare it once when initializing the application, and then just use it everywhere (in all pages and other classes of the application), without declaring and creating new connections to the DBMS?
I know that there is no concept of a global variable in C#, but still I think that people have already come up with something :)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Artem, 2017-05-09
@Serjio-Grig

First, take a look at Dapper instead of inventing your own ORMs for MySQL.
Secondly, static properties of the class can be used as "global" variables .

T
Tsiren Naimanov, 2017-05-09
@ImmortalCAT

Dependency Injection -
Google Ninject
Autofac

A
Aler, 2017-05-09
@Aler

Judging by the description of the problem, a "global" instance of the class is needed?
There is an antipattern - singleton . It has a number of significant drawbacks, but is easy to use and implement.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question