ProjectBaseCore (PBC) has to be injected to use in .net core project with version 2.x and later. After version 2.x, DatabaseFactory and QueryGeneratorFactory classes are no longer static classes. As a result, PBC version 2.x is not compatible with older versions, because you must inject DatabaseFactory and QueryGeneratorFactory classes or instantiate them with using new keyword.
First of all, in startup.js file or in a dependency injection container, injection must be defined:
services.AddSingleton<IDatabaseFactory, DatabaseFactory>(); services.AddSingleton<IQueryGeneratorFactory, QueryGeneratorFactory>();In startup file, we write these codes in "ConfigureServices" function. After these declarations, we can use these services in controllers or middleware classes.
public class HomeController : Controller { private readonly IDatabaseFactory _databaseFactory; private readonly IQueryGeneratorFactory _queryGeneratorFactory; public HomeController(IDatabaseFactory databaseFactory, IQueryGeneratorFactory queryGeneratorFactory) { _databaseFactory = databaseFactory; _queryGeneratorFactory = queryGeneratorFactory; } public IActionResult Index() { var db = _databaseFactory.GetDbObject(); var qg = _queryGeneratorFactory.GetDbObject(); qg.SelectText = "select * from product"; var dt = db.ExecuteQueryDataTable(qg.GetSelectCommandBasic()); return View(); } }
No comments:
Post a Comment