In memory or sql–part 27
When I want to perform tests , the SqlServer in memory is enough good. But , when in production, should have a proper connection string.
So I figured a simple way to understand this: if no connection string in appsettings.json, then use InMemory Sql Server . If there is, it should be a read connection string.
First, I have tried with
ConfigurationManager.ConnectionStrings[“DBWrite”];
– but this is the old XML way – does not work with appsettings.json
I did not want to have DI in the constructor to read the file – so I realized that I can do this:
configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile(“appsettings.json”)
.Build();
This is not so good – because the code for retrieving looks like this:
DbContextOptionsBuilder<InfoValutarContext> opt;
var ConnectionString = InMemoryDB.sing.GetConRead(“DBRead”);if (string.IsNullOrWhiteSpace(ConnectionString ))
{
opt = InMemoryDB.sing.MemoryOptions();
}
else
{
opt = new DbContextOptionsBuilder<InfoValutarContext>();
opt.UseSqlServer(ConnectionString);
}
However,adding save and retrieve code ( with interfaces) , adding tests , adding controllers and DI on the WebAPI takes 1 hour
The commit is https://github.com/ignatandrei/InfoValutar/commit/ddd6fdc38bd8eee46d7fea0c54603c3644046cf6 and has 11 files .
Infovalutar
And one hour passes...(This is the result of 1 hour per day auto-challenge as a full cycle developer for an exchange rates application)
( You can see the sources at https://github.com/ignatandrei/InfoValutar/ )
Leave a Reply