Can I create an overloaded function call for an interface I created?

Jesse Knott 686 Reputation points
2021-02-07T21:02:27.87+00:00

I'm trying to make a free version of my app. The app saves records to a database, and I have Repository classes for all of the tables of data.
In the free version I want to limit the number of records that can be saved/retrieved. The easiest way I can think of to do this, is to overload the database functions in the instance of the free app.
I need to figure out how to validate the purchased state of my app, then store that flag in the database. If the user requests a refund, I can then verify that and change the flag in the DB to go back to the free version. (these flags could be recorded as blob types and (en/de)crypted at runtime when being loaded)

I could then make an overloaded function that would limit the DB access.

My template declaration looks like this

  public interface IDataBaseRecord<T>
  {
      Task<ObservableCollection<T>> GetItems();
  }

then in the current implementation of my MaintenanceDataRepository.cs

  public class MaintenanceDataRepository : IDataBaseRecord
  {
    public async Task<ObservableCollection<MaintenanceData>> GetItems()
    {
        CreateConnection();
        return new ObservableCollection<MaintenanceData>(await connection.Table<MaintenanceData>().ToListAsync());
    }
  }

For instance I could make the following GetItems() overloaded function.

  public overload ObservableCollection<MaintenanceData> GetItems()
  {
    ObservableCollection<MaintenanceData> returnData = new ObservableCollection<MaintenanceData>();
    var items =  new ObservableCollection<MaintenanceData>(await connection.Table<MaintenanceData>().ToListAsync());

    for (var i = 0; i < _FreeResults_ ; i++)
    {
      if(items[i] != null)
          returnData.Add(items[i]);
      else
          break;
    }

    return returnData;
  }

But, I have about 15 different data classes I would need to do this to...

Is there a way I can implement the above code as an overloaded function that get's called instead of the automatic GetItems()? I would need to make it conditional, since there are some tables that specify things like manufacturers so I want all of those records to be called, but want to limit the number of records that a regular call can get.

At the moment the only way I can think to do this is to implement the checks and implement the limiting code in each class in each call that needs to be limited. What I would like is some sort of additional interface, or function I can just pass all of the calls made by all the other data repositories through to handle this. Since not all of my repositories need to have this limiting I would like it to be selective, but I could always just create a separate interface class to handle the limitable records vs the others that don't get limited.

I would then be able to store info in the database, such as number of RewardVideos they've watched and change the number of records based on that, and so on.

Cheers!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,690 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,936 Reputation points
    2021-02-08T09:57:32.093+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    But, I have about 15 different data classes I would need to do this to...

    Try using generics to get the data like below:

       void testMothod()  
       {  
           var collection = GetData<TodoItem>();  
       }  
         
       ObservableCollection<T> GetData<T>()  
       {  
           return new ObservableCollection<T>();  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful