Home > OS >  Uniform how data is fetched by sharing implementation in nuget package
Uniform how data is fetched by sharing implementation in nuget package

Time:08-03

How do I abstract the connection to my database away?

I am currently working on a nuget package, that sort of unify the way data is retrieved from the database, thus ensuring that data is collected in a similar manner, regardless which service inherits it.

I though seem to be a bit confused of whether my inherited class, and the properties will be assessible for MyService? and instantiated correctly?

Here is an example:

public class DataService :  IDataservice
{

    private readonly CosmosClient client;

    public TestClass(CosmosClient client)
    {
        var kvUrl = Environment.GetEnvironmentVariable("KEY_VAULT_URL");
        var secretclient = new SecretClient(new Uri(kvUrl), new DefaultAzureCredential());
        var accountEndpoint = secretclient.GetSecret("AccountEndpoint-CosmosDb");
        var accountKey = secretclient.GetSecret("AccountKey-CosmosDb");
        this.client = new CosmosClientBuilder(accountEndpoint.Value.Value, accountKey.Value.Value).Build();
    }

    public IDictionary<string, object> GetForm(Guid id)
    {
        //Contains custom code for fetching form
        //Processing fetched form'
        //Return form
    }

    public object GetFormField(string propertyName)
    {   
        //Contains custom code for fetching field
        //Processing fetched field
        //Return field
    }

    public string TestString()
    {
        return "Hello web";
    }
}


public class MyService : DataService
{
    public MyService()
    {
    }

    public IDictionary<string, object> GetForm(Guid id) =>  return parent.GetForm(Guid id)
    
    public object GetFormField(string propertyName) =>  return parent.GetFormField(string propertyName)
    
    public override object GetFormField(string propertyName)
    {
        return "Hello new world";
    }


}

does Myservice have an instantiated version of the CosmosClient? Is GetForm and GetFormField accessible to MyService - and is it possible to explictly state when an inherited method is overridden as above? and is this even a good idea? - I feel like I am creating an unessary layer, to ensure that data is fetched uniformly by making a class that everyone can inherit.

CodePudding user response:

I think you could do something like this:

public class TestClass
    {
        private CosmosClient _client;
        private SecretClient _secretClient;

        public string kvUrl { get => Environment.GetEnvironmentVariable("KEY_VAULT_URL"); }
        public SecretClient secretClient { 
            get
            {
                return _secretClient ?? (_secretClient = new SecretClient(new Uri(kvUrl), new DefaultAzureCredential()));
            }
        }
        public KeyVaultSecret accountEndpoint {
            get
            {
                return _secretClient.GetSecret("AccountEndpoint-CosmosDb");
            }
        }
        public KeyVaultSecret accountKey {
            get
            {
                return _secretClient.GetSecret("AccountKey-CosmosDb");
            }
        }
        public CosmosClient client {
            get
            {
                return _client ?? (_client = new CosmosClientBuilder(accountEndpoint.Value, accountKey.Value).Build());
            }
        }
    }

CodePudding user response:

Why dont you make an abstract class for DataService, and set the secrets/connections from the derived classes. as

    public abstract class DataService
    {
        public DataService(CosmosClient client)
        {
            var kvUrl = Environment.GetEnvironmentVariable("KEY_VAULT_URL");
            var secretclient = new SecretClient(new Uri(kvUrl), new DefaultAzureCredential());
            var accountEndpoint = secretclient.GetSecret("AccountEndpoint-CosmosDb");
            var accountKey = secretclient.GetSecret("AccountKey-CosmosDb");
            this.client = new CosmosClientBuilder(accountEndpoint.Value.Value, accountKey.Value.Value).Build();
        }
    
    // your abstract methods here to be implementd by derived classes
}

class some_derived_class:DataService{
some_derived_class:DataService(CosmosClient client):base(client){}
}
  • Related