ASP.NET Core Data Access Layer Custom Class AddSingleton












0















I have my EntityFrameworkCore DBContext in ConfigureServices



public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDBContext>(Options => ... );
...
}


I also have my data access layer factory class which receives DBContext in constructor



public partial class DataAccessFactory
{
public readonly ProductsDataAccess Products;
public readonly CategoriesDataAccess Categories;

public DataAccessFactory(MyDBContext db)
{
Products = new ProductsDataAccess(db);
Categories = new CategoriesDataAccess(db);
}
}


In order to work with Data Access Layer, I have to create new instance of DataAccessFactory per each request.



My question is, does it make sence and is there any way to create one instance of DataAccessFactory and add it as a Singleton?










share|improve this question























  • Did you create instances of DataAccessFactory using new operator? Then you also create manually the db context. In that way you are not using DI. Generally it is good practice to recreate db context between different requests, also it is default behavior. In your code you already registered a MyDBContext, you can also register DataAccessFactory. See the different way to register it (as singleton or any other way)

    – Ivvan
    Nov 20 '18 at 11:29











  • Yes I create instances of DataAccessFactory using new operator per each request, I capture MyDBContext via DI and create new instance of DataAccessFactory per each request. Now I think maybe instead of creating new instance of DataAccessFactory per each request, I can create DataAccessFactory one and Singleton it.

    – Michael Samteladze
    Nov 20 '18 at 11:44
















0















I have my EntityFrameworkCore DBContext in ConfigureServices



public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDBContext>(Options => ... );
...
}


I also have my data access layer factory class which receives DBContext in constructor



public partial class DataAccessFactory
{
public readonly ProductsDataAccess Products;
public readonly CategoriesDataAccess Categories;

public DataAccessFactory(MyDBContext db)
{
Products = new ProductsDataAccess(db);
Categories = new CategoriesDataAccess(db);
}
}


In order to work with Data Access Layer, I have to create new instance of DataAccessFactory per each request.



My question is, does it make sence and is there any way to create one instance of DataAccessFactory and add it as a Singleton?










share|improve this question























  • Did you create instances of DataAccessFactory using new operator? Then you also create manually the db context. In that way you are not using DI. Generally it is good practice to recreate db context between different requests, also it is default behavior. In your code you already registered a MyDBContext, you can also register DataAccessFactory. See the different way to register it (as singleton or any other way)

    – Ivvan
    Nov 20 '18 at 11:29











  • Yes I create instances of DataAccessFactory using new operator per each request, I capture MyDBContext via DI and create new instance of DataAccessFactory per each request. Now I think maybe instead of creating new instance of DataAccessFactory per each request, I can create DataAccessFactory one and Singleton it.

    – Michael Samteladze
    Nov 20 '18 at 11:44














0












0








0








I have my EntityFrameworkCore DBContext in ConfigureServices



public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDBContext>(Options => ... );
...
}


I also have my data access layer factory class which receives DBContext in constructor



public partial class DataAccessFactory
{
public readonly ProductsDataAccess Products;
public readonly CategoriesDataAccess Categories;

public DataAccessFactory(MyDBContext db)
{
Products = new ProductsDataAccess(db);
Categories = new CategoriesDataAccess(db);
}
}


In order to work with Data Access Layer, I have to create new instance of DataAccessFactory per each request.



My question is, does it make sence and is there any way to create one instance of DataAccessFactory and add it as a Singleton?










share|improve this question














I have my EntityFrameworkCore DBContext in ConfigureServices



public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDBContext>(Options => ... );
...
}


I also have my data access layer factory class which receives DBContext in constructor



public partial class DataAccessFactory
{
public readonly ProductsDataAccess Products;
public readonly CategoriesDataAccess Categories;

public DataAccessFactory(MyDBContext db)
{
Products = new ProductsDataAccess(db);
Categories = new CategoriesDataAccess(db);
}
}


In order to work with Data Access Layer, I have to create new instance of DataAccessFactory per each request.



My question is, does it make sence and is there any way to create one instance of DataAccessFactory and add it as a Singleton?







asp.net-core dependency-injection entity-framework-core






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 10:50









Michael SamteladzeMichael Samteladze

800824




800824













  • Did you create instances of DataAccessFactory using new operator? Then you also create manually the db context. In that way you are not using DI. Generally it is good practice to recreate db context between different requests, also it is default behavior. In your code you already registered a MyDBContext, you can also register DataAccessFactory. See the different way to register it (as singleton or any other way)

    – Ivvan
    Nov 20 '18 at 11:29











  • Yes I create instances of DataAccessFactory using new operator per each request, I capture MyDBContext via DI and create new instance of DataAccessFactory per each request. Now I think maybe instead of creating new instance of DataAccessFactory per each request, I can create DataAccessFactory one and Singleton it.

    – Michael Samteladze
    Nov 20 '18 at 11:44



















  • Did you create instances of DataAccessFactory using new operator? Then you also create manually the db context. In that way you are not using DI. Generally it is good practice to recreate db context between different requests, also it is default behavior. In your code you already registered a MyDBContext, you can also register DataAccessFactory. See the different way to register it (as singleton or any other way)

    – Ivvan
    Nov 20 '18 at 11:29











  • Yes I create instances of DataAccessFactory using new operator per each request, I capture MyDBContext via DI and create new instance of DataAccessFactory per each request. Now I think maybe instead of creating new instance of DataAccessFactory per each request, I can create DataAccessFactory one and Singleton it.

    – Michael Samteladze
    Nov 20 '18 at 11:44

















Did you create instances of DataAccessFactory using new operator? Then you also create manually the db context. In that way you are not using DI. Generally it is good practice to recreate db context between different requests, also it is default behavior. In your code you already registered a MyDBContext, you can also register DataAccessFactory. See the different way to register it (as singleton or any other way)

– Ivvan
Nov 20 '18 at 11:29





Did you create instances of DataAccessFactory using new operator? Then you also create manually the db context. In that way you are not using DI. Generally it is good practice to recreate db context between different requests, also it is default behavior. In your code you already registered a MyDBContext, you can also register DataAccessFactory. See the different way to register it (as singleton or any other way)

– Ivvan
Nov 20 '18 at 11:29













Yes I create instances of DataAccessFactory using new operator per each request, I capture MyDBContext via DI and create new instance of DataAccessFactory per each request. Now I think maybe instead of creating new instance of DataAccessFactory per each request, I can create DataAccessFactory one and Singleton it.

– Michael Samteladze
Nov 20 '18 at 11:44





Yes I create instances of DataAccessFactory using new operator per each request, I capture MyDBContext via DI and create new instance of DataAccessFactory per each request. Now I think maybe instead of creating new instance of DataAccessFactory per each request, I can create DataAccessFactory one and Singleton it.

– Michael Samteladze
Nov 20 '18 at 11:44












1 Answer
1






active

oldest

votes


















1














You can register your service inside ConfigureServices in Startup.cs:



public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<DataAccessFactory>();
}


and inject it where you needed:



public class MyController
{
public readonly DataAccessFactory Factory;

public MyController(DataAccessFactory factory)
{
Factory = factory;
}
}


It will be created only once per application life. If you instead will want to change it scope for example per request, just change AddSingleton to AddScoped.



Update:



But be careful when mixing different-scoped services. You cannot inject short living object into long living, because it will cause exceptions. In your situation you will need to change DbContext scope to singleton (sic) or consider to change Factory lifetime to Scoped. Here is example how to change DbContext scope.






share|improve this answer





















  • 1





    Thanks Ivvan, this was the explanation I was looking for. Article link you showed me gave me an answer to my question. I didn't know that AddDbContext adds DbContext as Scoped. Now it's clear to me how to manage my code

    – Michael Samteladze
    Nov 20 '18 at 13:31











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391372%2fasp-net-core-data-access-layer-custom-class-addsingleton%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can register your service inside ConfigureServices in Startup.cs:



public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<DataAccessFactory>();
}


and inject it where you needed:



public class MyController
{
public readonly DataAccessFactory Factory;

public MyController(DataAccessFactory factory)
{
Factory = factory;
}
}


It will be created only once per application life. If you instead will want to change it scope for example per request, just change AddSingleton to AddScoped.



Update:



But be careful when mixing different-scoped services. You cannot inject short living object into long living, because it will cause exceptions. In your situation you will need to change DbContext scope to singleton (sic) or consider to change Factory lifetime to Scoped. Here is example how to change DbContext scope.






share|improve this answer





















  • 1





    Thanks Ivvan, this was the explanation I was looking for. Article link you showed me gave me an answer to my question. I didn't know that AddDbContext adds DbContext as Scoped. Now it's clear to me how to manage my code

    – Michael Samteladze
    Nov 20 '18 at 13:31
















1














You can register your service inside ConfigureServices in Startup.cs:



public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<DataAccessFactory>();
}


and inject it where you needed:



public class MyController
{
public readonly DataAccessFactory Factory;

public MyController(DataAccessFactory factory)
{
Factory = factory;
}
}


It will be created only once per application life. If you instead will want to change it scope for example per request, just change AddSingleton to AddScoped.



Update:



But be careful when mixing different-scoped services. You cannot inject short living object into long living, because it will cause exceptions. In your situation you will need to change DbContext scope to singleton (sic) or consider to change Factory lifetime to Scoped. Here is example how to change DbContext scope.






share|improve this answer





















  • 1





    Thanks Ivvan, this was the explanation I was looking for. Article link you showed me gave me an answer to my question. I didn't know that AddDbContext adds DbContext as Scoped. Now it's clear to me how to manage my code

    – Michael Samteladze
    Nov 20 '18 at 13:31














1












1








1







You can register your service inside ConfigureServices in Startup.cs:



public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<DataAccessFactory>();
}


and inject it where you needed:



public class MyController
{
public readonly DataAccessFactory Factory;

public MyController(DataAccessFactory factory)
{
Factory = factory;
}
}


It will be created only once per application life. If you instead will want to change it scope for example per request, just change AddSingleton to AddScoped.



Update:



But be careful when mixing different-scoped services. You cannot inject short living object into long living, because it will cause exceptions. In your situation you will need to change DbContext scope to singleton (sic) or consider to change Factory lifetime to Scoped. Here is example how to change DbContext scope.






share|improve this answer















You can register your service inside ConfigureServices in Startup.cs:



public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<DataAccessFactory>();
}


and inject it where you needed:



public class MyController
{
public readonly DataAccessFactory Factory;

public MyController(DataAccessFactory factory)
{
Factory = factory;
}
}


It will be created only once per application life. If you instead will want to change it scope for example per request, just change AddSingleton to AddScoped.



Update:



But be careful when mixing different-scoped services. You cannot inject short living object into long living, because it will cause exceptions. In your situation you will need to change DbContext scope to singleton (sic) or consider to change Factory lifetime to Scoped. Here is example how to change DbContext scope.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 12:04

























answered Nov 20 '18 at 11:54









IvvanIvvan

405512




405512








  • 1





    Thanks Ivvan, this was the explanation I was looking for. Article link you showed me gave me an answer to my question. I didn't know that AddDbContext adds DbContext as Scoped. Now it's clear to me how to manage my code

    – Michael Samteladze
    Nov 20 '18 at 13:31














  • 1





    Thanks Ivvan, this was the explanation I was looking for. Article link you showed me gave me an answer to my question. I didn't know that AddDbContext adds DbContext as Scoped. Now it's clear to me how to manage my code

    – Michael Samteladze
    Nov 20 '18 at 13:31








1




1





Thanks Ivvan, this was the explanation I was looking for. Article link you showed me gave me an answer to my question. I didn't know that AddDbContext adds DbContext as Scoped. Now it's clear to me how to manage my code

– Michael Samteladze
Nov 20 '18 at 13:31





Thanks Ivvan, this was the explanation I was looking for. Article link you showed me gave me an answer to my question. I didn't know that AddDbContext adds DbContext as Scoped. Now it's clear to me how to manage my code

– Michael Samteladze
Nov 20 '18 at 13:31




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391372%2fasp-net-core-data-access-layer-custom-class-addsingleton%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word