locked
to call a method from the repository class in controller RRS feed

  • Question

  • User-1355965324 posted

    I have the following two model

    namespace MyProject.GO.Common.Models
    public class EmployeeModel { [Key] public int EmployeeID { get; set; } public string FirstName { get; set; } } public class GoUserDepotLink { [Key] public int id{get;set;} public int UserID{ get; set; } public int DepotID{ get; set; } }

    I have the following  repository interface

    namespace MyProject.GO.WebAPI.Handlers
    {
        public interface IDataRepository<TEntity, U> where TEntity : class
        {
            TEntity Get(U id);
            long GetUserDepot(U id);
        }
    }
    

    Then I have created two services

    UserDepotManage.cs file
    
    namespace MyProject.GO.WebAPI.Services
    {
        public class UserDepotManager : IDataRepository<GoUserDepotLink, long>
        {
            GoContext ctx;
            public UserDepotManager(GoContext c)
            {
                ctx = c;
            }
            public GoUserDepotLink Get(long id)
            {
                var gouserdepots = ctx.goUserDepotLink.FirstOrDefault(b => b.UserID == id && b.IsDefaultDepot==1);
                return gouserdepots;
            }
    
            public long GetUserDepot(long id)
            {
              
                long userdepot = (from b in ctx.goUserDepotLink
                                  where b.UserID == id && b.IsDefaultDepot == 1
                                  select b.DepotNo).SingleOrDefault();
    
                
                return userdepot;
            }
    }
    
    EmployeeManager.cs file
    
    namespace MyProject.GO.WebAPI.Services
    {
        public class EmployeeManager : IDataRepository<EmployeeModel, long>
        {
            GoContext ctx;
            public EmployeeManager(GoContext c)
            {
                ctx = c;
            }
            public EmployeeModel Get(long id)
            {
                var employee = ctx.goEmployee.FirstOrDefault(b => b.EmployeeID == id);
                return employee;
            }
    
           
    
            public long GetUserDepot(long id)
            {
    
                long userdepot = (from b in ctx.goUserDepotLink
                                  where b.UserID == id && b.IsDefaultDepot == 1
                                  select b.DepotNo).SingleOrDefault();
                return userdepot;
            }
    
    
    

    How can I call the method GetUserDepot from gouserdepotmanager service class

      public IActionResult Index()
            {
                 // How can I call the method  GetUserDepot()  from the  UserDepotManager services class
    // Also I want to call the Get method from EmployeeManager service class
    return View(); }

    If am doing stupid way of coding please  advise the best way of implementation with suggested code

    With Many Thanks

    Pol

    Friday, November 1, 2019 2:44 PM

Answers

  • User475983607 posted

    If am doing stupid way of coding please  advise the best way of implementation with suggested code

    The official documentation illustrates ASP.NET Core Dependency Injection programming pattern.   

    Register the service in ConfigureServices.

    services.AddScoped<IDataRepository<GoUserDepotLink, long>, UserDepotManager>();

    Constructor injection pattern.

    private readonly IDataRepository<GoUserDepotLink, long> _repo;
    public HomeController(IDataRepository<GoUserDepotLink, long> repo)
    {
        _repo = repo;
    }

    Keep in mind, the code sample above assumes the DbContext is registered. 

    Dependency Injection Reference Documentation

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 2, 2019 11:43 AM

All replies

  • User1120430333 posted

    // How can I call the method GetUserDepot() from the UserDepotManager services class
    // Also I want to call the Get method from EmployeeManager service class

    You do it by implementing an Interface on the class and dependency injecting the object instance  into the controller so that the action method can make the call to the method on the injected object. 

     

    Friday, November 1, 2019 4:39 PM
  • User475983607 posted

    If am doing stupid way of coding please  advise the best way of implementation with suggested code

    The official documentation illustrates ASP.NET Core Dependency Injection programming pattern.   

    Register the service in ConfigureServices.

    services.AddScoped<IDataRepository<GoUserDepotLink, long>, UserDepotManager>();

    Constructor injection pattern.

    private readonly IDataRepository<GoUserDepotLink, long> _repo;
    public HomeController(IDataRepository<GoUserDepotLink, long> repo)
    {
        _repo = repo;
    }

    Keep in mind, the code sample above assumes the DbContext is registered. 

    Dependency Injection Reference Documentation

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 2, 2019 11:43 AM