Home > Mobile >  I would like to add code to all my c# services
I would like to add code to all my c# services

Time:02-02

How do I add code to all the c# classes. I have a multitenants system and when the user logs in I store the user company name I use this company name as the name of my database. I need to replace the name of the default schema with the name of the company of the signed in user. I need to do this in every services. How can I repeat the code in every service without copy pasting it everywhere.

This code is the same in all services is there is smart way of getting the same code in all my services without copy pasting the same code everywhere?

public class MyService : IMyService
{
    private readonly erp_colombiaContext _context;
    private readonly IHttpContextAccessor _httpContextAccessor;

    public MyService(erp_colombiaDbContext context, IHttpContextAccessor httpContextAccessor)
    {
        _context = context;
        _httpContextAccessor = httpContextAccessor;

        if (httpContextAccessor.HttpContext.User.Claims.Count() != 0)
        {
            var companyName = httpContextAccessor.HttpContext.User.Claims.ToList().Where(x => x.Type == "CompanyName").FirstOrDefault().Value;

            string connectionString = _context.Database.GetDbConnection().ConnectionString;
            connectionString = connectionString.Replace("erp_colombia", companyName);
            _context.Database.GetDbConnection().ConnectionString = connectionString;
        }
    }

}

CodePudding user response:

What I would do is to create a base class (parent) called something like BaseService that contains all the common code that all your services have. eg:

public class BaseService {
    private readonly erp_colombiaContext _context;
    private readonly IHttpContextAccessor _httpContextAccessor;

    public BaseService(erp_colombiaDbContext context, IHttpContextAccessor httpContextAccessor)
    {
        _context = context;
        _httpContextAccessor = httpContextAccessor;

        if (httpContextAccessor.HttpContext.User.Claims.Count() != 0)
        {
            var companyName = httpContextAccessor.HttpContext.User.Claims.ToList().Where(x => x.Type == "CompanyName").FirstOrDefault().Value;

            string connectionString = _context.Database.GetDbConnection().ConnectionString;
            connectionString = connectionString.Replace("erp_colombia", companyName);
            _context.Database.GetDbConnection().ConnectionString = connectionString;
        }
    }
}

Then, I would inherit from this class in all my interfaces. eg:

public class MyService : BaseService, IMyService
{
    public MyService(erp_colombiaDbContext context, IHttpContextAccessor httpContextAccessor) : base (context, httpContextAccessor)
    {
    }
}

Things to keep in mind is that a class can implement multiple interfaces but it can only inherit from one class. That means that C# doesn't support multiple inheritance.

EDIT: If you are using some kind of a Dependency injection framework, then the best would be that you register the service to your dependency injection container and then do constructor injection of the service to the services that need it. eg:

public class MyService : IMyService
{
    SomeService _someService;

    public MyService(SomeService someService)
    {
        _someService = someService;
    }
    
    public void TestMethod() {
        // use the injected service.
        // _someService.blah_blah();
    }
}
  •  Tags:  
  • c#
  • Related