Sample Interview 1 Flashcards

(5 cards)

1
Q

✅ Main Question (EF Core – deeper system design level)

What are the differences between eager, lazy, and explicit loading in Entity Framework Core, and when would you prefer each one?

🔍 Follow-Up Question (challenging)

If your application suddenly starts running slow because of N+1 queries coming from lazy loading, how would you diagnose and fix the issue?

A

Absolutely! Let me summarize my responses for both questions:

  1. Differences between Eager, Lazy, and Explicit Loading in EF Core:

Eager Loading: All related entities are loaded at the same time using the Include method. This is useful when you know you’ll need all related data upfront, reducing multiple database calls. However, it can result in fetching more data than needed, which may impact performance.

Lazy Loading: Related entities are loaded only when they are accessed. This can be beneficial if you don’t need all the data at once, but it can lead to multiple round-trips to the database, potentially causing performance issues.

Explicit Loading: You manually load related entities when needed, giving you fine-grained control. This is useful when you want to load data conditionally and avoid unnecessary queries.

  1. Diagnosing and Fixing N+1 Query Issues:

Diagnosing: Start by using tools like SQL profilers or Entity Framework logging to identify the multiple database calls. You can also review the SQL queries generated by EF Core to see if unnecessary queries are being executed.

Fixing: To address N+1 issues, you can switch to eager loading using Include to load all related entities in a single query. Another approach is to use explicit loading to fetch only the data you need. You can also consider optimizing the database by adding indexes or restructuring queries to reduce the number of calls.

I hope that covers it well! Let me know if you’d like to dive deeper into any of these points or if you’re ready to move on to something new!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In ASP.NET Core, how does the dependency injection system handle the different lifetimes—transient, scoped, and singleton—and what are some potential pitfalls when mixing them?

A

Answer to the Main Question:

In .NET Core’s dependency injection, the different lifetimes are handled as follows:

Transient: A new instance of the service is created each time it’s requested. This is useful for lightweight, stateless services.

Scoped: A single instance of the service is created per request (or per scope). This is ideal for services that need to maintain state within a single request but not beyond.

Singleton: A single instance of the service is created for the entire application’s lifetime. It’s ideal for services that are thread-safe and can be reused throughout the app.

When you mix these lifetimes, such as a singleton depending on a scoped service, you run into issues because the singleton lives longer than the scoped service. This can lead to null references because the scoped service might be disposed before the singleton tries to use it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are some patterns or techniques you can use, such as factory methods or the use of IServiceProvider, to safely inject scoped or transient dependencies into singleton services?

This should give us a nice challenge and allow us to explore some advanced DI patterns!

A

To resolve these issues, you can use the IServiceProvider to create a new scope within the singleton’s method. This way, each time the singleton needs the scoped service, it creates a fresh scope, ensuring the scoped service is alive and valid. Another approach is to use a factory pattern that encapsulates the logic for creating and managing lifetimes, so the dependencies are always resolved correctly and safely.

In summary, the key is to carefully manage the lifetime boundaries and use patterns like IServiceScopeFactory or factory methods to ensure that each service is resolved within the appropriate scope.

I hope that helps clarify everything! If you have more questions or want to dive deeper, just let me know!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In ASP.NET Core, how do middleware and filters differ in terms of their execution order and use cases? And how would you decide when to use one over the other?

A

Middleware vs. Filters:

Middleware: It’s part of the request pipeline and runs early, before the request reaches the MVC framework. Middleware is great for cross-cutting concerns like authentication, logging, and error handling at a global level. It’s executed in order, and you can short-circuit the pipeline if needed.

Filters: They are specific to the MVC pipeline and can run before or after action methods, allowing you to handle things like authorization, validation, or custom logic at the action or controller level. This gives you more control over specific parts of the request handling.

The choice between them often depends on the scope and the level of control you need. Middleware is more about the global pipeline, and filters are more about fine-tuning specific controller or action behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If you have a service provided at the component level, what are some potential pitfalls you might encounter, and how would you mitigate them to ensure optimal performance and memory management?

Feel free to jump in with your thoughts, and we’ll explore these topics in detail!

A

Consistent Exception Handling:
When you have both global middleware and action filters for exception handling, it’s important to ensure that they complement each other. One approach is to let action filters handle more specific, granular exceptions and then rely on the global middleware to catch any exceptions that aren’t handled at the action level. This layering ensures that you don’t miss any errors and that you can apply different strategies for logging, user-friendly error messages, or other custom logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly