locked
to add a method in repository class with integer data type RRS feed

  • Question

  • User-1355965324 posted

    I am using the following   code for basic interface IDataRepository. I want to add  another method to an existing interface return value as int  type. how is it possible please help

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace MyProject.GO.WebAPI.Handlers
    {
        public interface IDataRepository<TEntity, U> where TEntity : class
        {
            IEnumerable<TEntity> GetAll();
            TEntity Get(U id);
            long GetLong(U id);
            int GetInt(U id)  // it is not working
            long Add(TEntity b);
            long Update(U id, TEntity b);
            long Delete(U id);
        }
    }
    

    Sunday, November 3, 2019 7:37 AM

Answers

  • User711641945 posted

    Hi polachan,

    Could you share how did you implement `GetInt` method?

    Here is a simple demo like below:

    1.Model:

    public class GoUserDepotLink
    {
        [Key]
        public int id { get; set; }
        public int UserID { get; set; }
        public int DepotID { get; set; }
    }

    2.Interface:

    public interface IDataRepository<TEntity, U> where TEntity : class
    {
        int GetInt(U id); 
    }

    3.Implement:

    public class UserDepotManager : IDataRepository<GoUserDepotLink, long>
    {
        private readonly MyContext ctx;
    
        public UserDepotManager(MyContext context)
        {
            ctx = context;
        }
        public int GetInt(long id)
        {
           //be sure type of b.id(what you want to select) is int
            int Id = (from b in ctx.goUserDepotLink
                        where b.UserID == id 
                        select b.id).SingleOrDefault();
            return Id;
        }
    }

    4.Controller:

    public class GoUserDepotLinksController : Controller
    {
        private readonly IDataRepository<GoUserDepotLink, long> _t;
        public GoUserDepotLinksController(IDataRepository<GoUserDepotLink, long> t)
        {
            _t = t;
        }
    
        // GET: GoUserDepotLinks
        public async Task<IActionResult> Index()
        {
            var data = _t.GetInt(1);
            return View();
        }
    }

    5.Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        //...
        services.AddScoped<IDataRepository<GoUserDepotLink, long>, UserDepotManager>();
        //...
    }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 4, 2019 2:40 AM

All replies

  • User1289604957 posted

    Hello,

    you can use Extension Methods https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

    Best regards,

    Maher

    Sunday, November 3, 2019 11:15 AM
  • User711641945 posted

    Hi polachan,

    Could you share how did you implement `GetInt` method?

    Here is a simple demo like below:

    1.Model:

    public class GoUserDepotLink
    {
        [Key]
        public int id { get; set; }
        public int UserID { get; set; }
        public int DepotID { get; set; }
    }

    2.Interface:

    public interface IDataRepository<TEntity, U> where TEntity : class
    {
        int GetInt(U id); 
    }

    3.Implement:

    public class UserDepotManager : IDataRepository<GoUserDepotLink, long>
    {
        private readonly MyContext ctx;
    
        public UserDepotManager(MyContext context)
        {
            ctx = context;
        }
        public int GetInt(long id)
        {
           //be sure type of b.id(what you want to select) is int
            int Id = (from b in ctx.goUserDepotLink
                        where b.UserID == id 
                        select b.id).SingleOrDefault();
            return Id;
        }
    }

    4.Controller:

    public class GoUserDepotLinksController : Controller
    {
        private readonly IDataRepository<GoUserDepotLink, long> _t;
        public GoUserDepotLinksController(IDataRepository<GoUserDepotLink, long> t)
        {
            _t = t;
        }
    
        // GET: GoUserDepotLinks
        public async Task<IActionResult> Index()
        {
            var data = _t.GetInt(1);
            return View();
        }
    }

    5.Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        //...
        services.AddScoped<IDataRepository<GoUserDepotLink, long>, UserDepotManager>();
        //...
    }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 4, 2019 2:40 AM