I'm trying to create only one instance over all my projects in solution. I want top prevent multiple database objects.
I have this organisation on project:
|-Solution:
|--- Core
|-------- Common
|------------ App.cs <singleton>
|------------ Model.cs
|------------ Database.cs
|
|--- Project 1
|-------- Models
|------------- Users.cs
|------------- Products.cs
|------------- Categories.cs
|-------- UI
|-------- etc...
|
|--- Project 2
|--- Project 3
|--- Project 4
Core
project is library project which has App
singleton class, Model
class is base class for models and database class for MySql
.
Base Model:
namespace Core.Common
{
public class Model : Database
{
public Database db;
public Model()
{
db = new Database();
if(db.IsConnected())
{
App.IsConnected = true;
}
}
}
}
And in all my models i create this:
namespace Core.Models
{
public class Products: Model
{
public Products() : base()
{
//..etc
}
public DataTable ShowAll()
{
return db.query("SELECT * FROM products");
}
}
}
So all my project has models for database and model must extend base model class wich already contain database object for connecting. But all models class must extend base
construcotr for establishing connection.
But am thinking this is litle bad practice.
What about to create object for database inside Main()
? than will be globaly over priject right ?
Any other approach to improve this project?