Questions? Feedback?powered byOlark live chat software
Bug 2800

datastore caching 12 June, 2019

Marco Meoni
04 June, 2019
Product: PowerBuilder Category: .NET DataStore
Version: 2019 Publishing: Public
Status: Fixing Priority: P3
Classification: Resolution:
Logan Liu 12 June, 2019
Hi Marco,

Thank you for reporting this bug.

In the next version of SnapObjects, we will extend another AddDataContext method with a new parameter for specifying the lifetime.

Regards,
Logan
Logan Liu 05 June, 2019
Hi Marco,

Sorry, I didn't understand your opinion about this bug before.
We extend the IServiceCollection using the AddDataContext <TAdoDbContext>() method, but the DataContext is always scoped here.
I will get back to you after getting the response from the development team.

You can temporarily extend the static method (e.g.: AddDataContextSingleton) on the IServiceCollection to bypass this problem, and then use AddDataContextSingleton to register a single DataContext instead of AddDataContext.

Code Example:
    public static class DataExtension
    {
        public static IServiceCollection AddDataContextSingleton<TAdoDbContext>(
        this IServiceCollection services,
        Func<IDataContextOptions<TAdoDbContext>, IDataContextOptions<TAdoDbContext>> contextOptions)
        where TAdoDbContext : DataContext
        {
            IDataContextOptions<TAdoDbContext> options = null;

            options = contextOptions(options);

            services.AddSingleton(options);

            services.AddSingleton<TAdoDbContext>();

            return services;
        }
    }

Regards,
Logan
Marco Meoni 05 June, 2019
Hi Logan,
apologize for misplacing the question, it was indeed meant to be posted on Community.

However please note that I cannot use services.AddSingleton because DataContext is scoped and cannot be consumed from singleton.
AddTransient is even less suitable then AddScoped because it is executed for each request instead of each session.

Workaround is to declare a static datastore in the Service class
 
private static IDataStore cacheDatastore

and instantiate the datastore only at first request (e.g. the first time Retrieve() is called).
But I rather preferred to have Dependency Injection to make the job instead of using a static field.
Best,
.m
Logan Liu 04 June, 2019
Hi Marco,

This is not a bug. 
But we appreciate that you want to know more .NET DataStore in PB2019.

In an Asp.NET Core prject, I think the "AddSingleton" method is the best practice for your question.
Refer to Microsoft's document:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1#lifetime-and-registration-options

Then you will know more about the differences between the following three methods:
    services.AddTransient<IOperationTransient, Operation>();
    services.AddScoped<IOperationScoped, Operation>();
    services.AddSingleton<IOperationSingleton, Operation>();

We recommend posting tech how-to/advice questions to the Appeon Community:  https://community.appeon.com  
Questions posted to the Appeon Community may be responded to by Appeon support staff, MVPs, or other product users.

Regards,
Logan
Marco Meoni 04 June, 2019
*Phenomenon:
For performance reasons, it would be desirable to cache data on a server side component.
This applies to datastore, structure array... (and transaction objects too).

PB .NET Web Service targets cannot cache datastore/structure on the server because each request does not keep memory of previous data (stateless request).
I cannot even have a global counter that counts the number of requests.
No matter if variables are declared as global or instance.

What about C# Web API?
Can I cache a C# Datastore for next requests?
Can you advise on best strategy for data caching, in order to come up with something similar to DataWindow caching functionality in PowerServer?

Best,
.m
OS:
All 
Platform:
 
Database Type:
 
Database Version: