1

I've created an injectable dbcontext

Startup.cs:

 public void ConfigureServices(IServiceCollection services) { services.AddScoped<IUnitOfWork, UnitOfWork>(); services.AddDbContext<DBContext>(options => options.UseSqlServer("Server=localhost;Database=mydb;Trusted_Connection=True;")); } 

UnitOfWork:

 public class UnitOfWork : IUnitOfWork { private readonly DBContext _context; public UnitOfWork(DBContext context) { _context = context; } 

The injection working fine in the controller:

 public class UserController : ControllerBase { private readonly IUnitOfWork unitOfWork; public UserController(IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; } } 

How can I create custom class that takes IUnitOfWork in the constructor and call it from main program?

CustomClass :

 public class CustomClass { private readonly IUnitOfWork unitOfWork; public CustomClass(IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; } } 

Main program:

 public class Program { public static void Main(string[] args) { var unitOfWork=new UnitOfWork() // Here I don't want to pass new DBContext I want to reach the same injected DBContext var customClass =new CustomClass (unitOfWork); } } 
2
  • You shouldn't need anything in Program.cs. Other than that, you should just be able to inject as you have in public CustomClass(IUnitOfWork unitOfWork) and then use unitOfWork in that Class. Commented Oct 23, 2020 at 17:52
  • How do you use the Startup.cs file with the ConfigureServices() method from your Main() method? Commented Oct 23, 2020 at 17:54

3 Answers 3

4

Firstly, move out your code from ConfigureServices to some shared library that can be used both by Web and Console project. Create extension method to configure all your services.

using Microsoft.Extensions.DependencyInjection; namespace ConsoleApp13 { public static class ConfigureServicesExtensions { public static void ConfigureMyServices(this IServiceCollection serviceCollection) { serviceCollection.AddDbContext<ApplicationDbContext>(); serviceCollection.AddScoped<IUnitOfWork, UnitOfWork>(); serviceCollection.AddScoped<CustomClass>(); } } } 

This is how your Console app will look like

using Microsoft.Extensions.DependencyInjection; namespace ConsoleApp13 { class Program { static void Main(string[] args) { var serviceCollection = new ServiceCollection(); serviceCollection.ConfigureMyServices(); using var serviceProvider = serviceCollection.BuildServiceProvider(); using var scope = serviceProvider.CreateScope(); var myService = scope.ServiceProvider.GetService<CustomClass>(); } } } 

And your web project

public void ConfigureServices(IServiceCollection services) { services.ConfigureMyServices(); } 
Sign up to request clarification or add additional context in comments.

4 Comments

Where CustomClass is registered?
@GuruStron nice question!
Could you please also post the code that that injects the Database Connection configuration settings (i.e, the database connection string)?
@crazyTech options.UseSqlServer injects connection string into db context - it is used in OP question. Or what do you mean?
0

What you can do is just add your class to the dependecy injection container and inject it into your constructor, you wouldn't need to inject the IUnitOfWork in you controller.

services.AddScoped<CustomClass>(); 

and then in your constroller constructor

public class UserController : ControllerBase { private readonly CustomClass _CustomClass; public UserController(CustomClass customClass) { _CustomClass = customClass; } } 

after that you are able to use this class in your class methods

1 Comment

Why do you assume there is any controller?
0

I would try something like this:

public class Program { public static void Main(string[] args) { var optionsBuilder = new DbContextOptionsBuilder<DbContext>(); optionsBuilder.UseSqlServer(connectionstring); using(DbContext dbContext = new DbContext(optionsBuilder.Options)) { var unitOfWork=new UnitOfWork(dbContext) var customClass =new CustomClass (unitOfWork); ..... } } } 

5 Comments

not really scalable, I was doing the same once, every service had multiple dependencies, they had dependencies too, and so on. it becomes a nightmare really fast
Thank you for the advice. I am agree with you that in common case a dependency injection is better but i am not sure what is better in this particular case. This is a web application not a desktop one and since addscoped creates a new object every request I don't thinkt it's going to affect the performance significantly.
Perfomance is not such an issue. I mean having to write all the chains of instances before reaching the class you need. sometimes you don't even know all the code on the project, so you need to lookup what implementation you need for interface .instead you could just 'copy' DI container from web and use it in similar ways in console/cloud function or other app
I don't understand why Sam needs this code in a main procedure at all. I never seen that anybody puts any code in a main procedure of a web application. IMHO the main procedure is only needed to create a host and called only once. So if Sam needed to run some code only ones he could call this code from startup. It would be much easier.
I don't think it is web application, could be just some tool that needs to use services that are being used in web project

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.