C
C#β€’6mo ago
gwon8266

Is using GetAwaiter().GetResult() for async methods in constructor a hack!

So I am writing my datalayer for a database, however I want to use one instance of that database for all the read and write methods in my datalayer class. In order for me to initialize the database, I need to use the "await" keyword because it's an async function. I planned to initialize the database at the constructor of my base datalayer class. but I can't use "await" in the constructor. So I decided to use GetAwaiter().GetResult(). It works, but I don't see this often. So I don't think it's appropriate. What do you think?
11 Replies
Angius
Angiusβ€’6mo ago
It is a hack, yes, and it is blocking A better idea would be a private constructor and an factory method
class MyClass
{
public Database Db { get; private set; }
private MyClass(Database db)
{
Db = db;
}

public static async Task<MyClass> CreateInstance()
{
var db = await MakeDatabase();
return new MyClass(db);
}
}
class MyClass
{
public Database Db { get; private set; }
private MyClass(Database db)
{
Db = db;
}

public static async Task<MyClass> CreateInstance()
{
var db = await MakeDatabase();
return new MyClass(db);
}
}
gwon8266
gwon8266OPβ€’6mo ago
Interesting, Thank you. I will use this
VYRUS
VYRUSβ€’6mo ago
Why don't you use dependency injection instead of creating a new instance of your db class? That way if you scope it to be a singleton, you can use it throughout your data layer and it'll keep that single instance of the database connection for you to use for read/write, executing stored procedures etc
gwon8266
gwon8266OPβ€’6mo ago
This sounds more efficient. I will work on it
VYRUS
VYRUSβ€’6mo ago
The way I usually do it is I'll create a DIConfig class that is called in a Startup.cs class I build (which gets called from Program.cs/Main.cs depending on your setup). That allows me to keep things centralised in a way that's easy to understand. https://positiwise.com/blog/dependency-injection-in-net-core-with-example This is an example of dependency injection but he does his dependency injection in his Program.cs rather than seperating it further and passing it down like I do
Parag Mehta
Positiwise
What is Dependency Injection in .NET Core? - Explained with Code Ex...
Explain dependency injection in .NET Core with code examples. Learn why DI is needed and steps to implement it in your .NET applications.
VYRUS
VYRUSβ€’6mo ago
It gets you into a good habit of creating interfaces, thinking about inheritance and base classes etc. It helped me start framing things in a way of how can I build something so I can extend it in the future without creating tech debt whilst also avoiding duplicate code.
Angius
Angiusβ€’6mo ago
Assuming you use ASP, then yeah, DI comes built-in and is the preferred way of getting a DbContext injected
gwon8266
gwon8266OPβ€’6mo ago
I have fixed it now I used your method and the dependency injection method
VYRUS
VYRUSβ€’6mo ago
That's awesome news! Glad you managed to pick it up so quickly πŸ˜„
gwon8266
gwon8266OPβ€’6mo ago
It seemed logical, so I just decided to test it. Thank you very muchπŸ™
VYRUS
VYRUSβ€’6mo ago
We love logic πŸ˜‰
Want results from more Discord servers?
Add your server