Hello. Faced such a problem - you need to implement singleton with the ability to work with different databases (well, let's say my connections are in the Setting class). Through DataManager (singleton) I work with repositories that are inherited from BaseRepository

public sealed class DataManager { public FabricRepository Fabric { get; set; } public FabricProductRepository FabricProduct { get; set; } public FittingRepository Fitting { get; set; } public FittingProductRepository FittingProduct { get; set; } public OrderRepository Order { get; set; } public ProductOrderRepository ProductOrder { get; set; } public ProductRepository Product { get; set; } public StockFabricRepository StockFabric { get; set; } public StockFittingRepository StockFitting { get; set; } public UserRepository User { get; set; } private DataManager() { Fabric = new FabricRepository(); FabricProduct = new FabricProductRepository(); Fitting = new FittingRepository(); FittingProduct = new FittingProductRepository(); Order = new OrderRepository(); ProductOrder = new ProductOrderRepository(); Product = new ProductRepository(); StockFabric = new StockFabricRepository(); StockFitting = new StockFittingRepository(); User = new UserRepository(); } static DataManager _active = null; static object _syncRoot = new object(); public static DataManager Instance { get { if (_active == null) lock (_syncRoot) if (_active == null) _active = new DataManager(); return _active; } } } abstract public class BaseRepository<T> where T : class, new() { public string TableName { get; set; } private List<T> List = new List<T>(); public BaseRepository() { this.TableName = typeof(T).Name; } public virtual ICollection<T> GetList() { FillList(); return List; } 

Here's an example of how I add data to the database: DataManager.Instance.User.Add (user); I think it is necessary to create the SqlConnection field in the singleton class?

  • no way - Igor
  • @Igor or pass a string to BaseRepository constructor? - Ari100krat

1 answer 1

I think it is necessary to create the SqlConnection field in the singleton class?

Answer:

It is not necessary to create a connection in the Singlton class because in this way you break the isolation of the repositories from each other. You can create a connection directly in each repository and thus implement the work with any number of databases. For example, you can pass a connection string to the constructor.

The use of ORM and models will be a good tone. Then create one context per repository.

UPD

As PashaPash rightly noted, you should not use one context all the time exactly as one instance of SqlConnection . In order to avoid problems, you should create a new SqlConnection or DbContext for each request. And be sure to remember to wrap it in using .

Also give useful links from PashaPash :

A concrete example of the consequences of a long-lived context.

Detailed answer to the same topic.

Additionally:

I would advise you to start reading about ORM (for example, Entity Framework). After that, you can read this interesting article about working with repositories.

Wording ORM from wikipedia:

ORM (English Object-Relational Mapping, Russian object-relational mapping, or transformation) is a programming technology that links databases with the concepts of object-oriented programming languages, creating a "virtual object database." There are both proprietary and free implementations of this technology.

The wording of the Entity Framework from Wikipedia:

The ADO.NET Entity Framework (EF), an object-oriented data access technology, is an object-relational mapping (ORM) solution for Microsoft's .NET Framework. Provides the ability to interact with objects both through LINQ in the form of LINQ to Entities, and using Entity SQL. To facilitate the construction of web solutions, ADO.NET Data Services (Astoria) is used, as well as a bunch of Windows Communication Foundation and Windows Presentation Foundation, which allows you to build multi-tier applications, implementing one of the MVC, MVP or MVVM design patterns.

  • It is not the answer to the question. To leave your comments or ask the author to clarify, leave a comment to the appropriate post. - From the queue of checks - aleksandr barakin
  • @alexander barakin, corrected the answer. - Sergey Ignakhin
  • @ PashaPash ♦ so. Stop. And where did you read that it says that a connection is created one and forever? It only says that you need to create a connection in the repository. It is written that you should not create it in singleton. Also all useful links are given. And the context itself is able to manage the connection pool. - Sergey Ignakhin
  • @ PashaPash ♦, well, on the next page. How do you think the connection pool works? Are there new ones being created all the time? Is a connection pool not a reuse of already created ones? And is it possible to say in such a situation that this is a short-lived object? You are now slapped a minus and wrote an angry comment without justifying your claim. - Sergey Ignakhin
  • Let's continue the discussion in the chat . - PashaPash