Attempting some namespace shenanigans

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
28,055
Reaction score
17,807
Hi gentleman and ladies. I have a small question which requires some thinking hats and mine is not working today. It's not a train smash this but it is a nice to have (I think). I am attempting to select namespaces of the Database used at runtime. One is SQL the other Postgre

This is I have.

C#:
string configValue = ConfigurationManager.AppSettings["DatabaseType"]; //Either SQLDatabase or PostgreSQL
DatabaseType DBType = (DatabaseType)Enum.Parse(typeof(DatabaseType), configValue);

switch (DBType)
{
    case DatabaseType.SqlServer:
        builder.RegisterType<[B]SQLDatabase[/B].Repositories.VehicleRepository>().As<IVehicleRepository>();
        break;
    case DatabaseType.Postgress:
        builder.RegisterType<[B]PostgreSQL[/B].Repositories.VehicleRepository>().As<IVehicleRepository>();
        break;
}

See the bold part. I am hoping to be able to change this with something like this.

C#:
string configValue = ConfigurationManager.AppSettings["DatabaseType"]; //Either SQLDatabase or PostgreSQL
builder.RegisterType<configValue.Repositories.VehicleRepository>().As<IVehicleRepository>();

Is this possible? :unsure:
 
Possibly possible using some sort of reflection, but I don’t see the point and would probably comment on a PR to do the first method.

With the first method you can more explicitly throw an exception for an invalid configuration value, while in the second instance you are relying on an unexpected exception.
 
Possibly possible using some sort of reflection, but I don’t see the point and would probably comment on a PR to do the first method.

With the first method you can more explicitly throw an exception for an invalid configuration value, while in the second instance you are relying on an unexpected exception.

Ok thanks yeah that makes sense. Sorry what is a PR?
 
Pull request

It works great. I have added another project for EF just to really test how robust the above is. I can now alternative between each back db. The application/service layer is non the wiser, swagger results looking great and everyone is happy! Thanks for the advice!!
 
Top
Sign up to the MyBroadband newsletter
X