2
public class AccountsMstController : Controller
{
    private AccountsMstRepository _AccountsMstRepository;
    public ErpOneLogRepository _ErpOneLogRepository;
    private DeleteRecordRepository _DeleteRecordRepository;
    private IHttpContextAccessor _contextAccessor;
    private HttpContext _context;
    private string username;
    private IHostingEnvironment _env;
    private string user;
    private string dbname;
    public AccountsMstController(IHttpContextAccessor contextAccessor, 
    IHostingEnvironment env)
    {
        _contextAccessor = contextAccessor;
        _context = _contextAccessor.HttpContext;
        username = _context.User.Identity.Name;
        _env = env;

        string[] Split = username.Split(new char[] { '@', '.' });
        dbname = Convert.ToString(Split[1]);
        user = Convert.ToString(Split[0]);
        _AccountsMstRepository = new AccountsMstRepository(dbname);
        _DeleteRecordRepository = new DeleteRecordRepository(dbname);
        _ErpOneLogRepository = new ErpOneLogRepository(dbname);
    }
 }

Trying to make this code reusable and can be called through function pointers as it is used in every Controller's class & constructors. I am trying to make this in a single method with can pass dynamic parameters and could be called from different classes.

Saurabh Nachankar
  • 320
  • 1
  • 2
  • 14
  • Which part of the code are you trying to make re-usable? Also, there are no function pointers in C#! Only delegates if that's what you mean. – Wheels73 Jul 18 '18 at 09:56
  • I wanted to reuse ihttpContextAccessor and ihostingenviornment as a single method but it gives me error stating service cannot pass as parameter. I am trying to reuse because my every controller calls this implementation! – Saurabh Nachankar Jul 18 '18 at 14:22
  • I was trying to create a single class that would implement ihttpContextAccessor and ihostingenviornment but it gave me error that service cannot be passed as paramter while instantiating. – Saurabh Nachankar Jul 18 '18 at 14:49
  • Ok. And I see your using .NET Core. have a look at this. https://stackoverflow.com/questions/38184583/how-to-add-ihttpcontextaccessor-in-the-startup-class-in-the-di-in-asp-net-core-1. Although it might not be relevant if you've no DI. – Wheels73 Jul 18 '18 at 14:55
  • I've not used Core yet to be honest. But you could also just make each controller instance the interface in a parameter less constructor. – Wheels73 Jul 18 '18 at 14:58

2 Answers2

0

I would start of with using an interface:

public interface IController {
    HttpContext Context{get; set; }
    string Username {get; set; }
    IHostingEnvironment Environment {get; set; }
    string User {get; set; }
    string DbName {get; set; }
    UnitOfWork ControllerWork {get; set;}
}

Assuming the repositories contain a DbContext object you could use the Repository - UnitOfWork pattern

public interface UnitOfWorkFactory {
    AccountsMstRepository AccountsMstRepository {get; set;}
    ErpOneLogRepository ErpOneLogRepository get; set;}
    DeleteRecordRepository DeleteRecordRepository {get; set;}
    DbContext DbContext {get; set;}

    // CRUD methods to update repository
}

public interface RepositoryFactory {
    DbContext context {get; set;}

    // CRUD methods to update repository
}

This way each different controller can implement the IController interface, so you can access the repositories on each controller using the intermediary UnitOfWork (you need to make a class that implements this)

If you want to make this even more reusable and you are using the Entity Framework you can use a BaseModel and implement the RepositoryFactory using basetype T, this way your UnitOfWorkFactory will not get cluttered. But for this, this is alright.

Here's a link on the pattern: microsoft-uow pattern implementation

Hoping to have answered your question.

Joachim.

JohnMcClane
  • 128
  • 6
0

How about using a BaseController class with constructor accepting IHttpContextAccessor and IHostingEnvironment and have all controllers inherit from it ?

itsbpk
  • 85
  • 6
  • I tried doing so, in order to invoke i need to pass parameter to the base controller which has IHttpContextAccessor and IHostingEnviornment soo it gave an error saying service cannot be passed as paramters eg : BaseController B =new BaseController(IHttpContextAccessor, IHostingEnviornment) – Saurabh Nachankar Jul 19 '18 at 06:52