أسئلة القسم
تدرب على أسئلة المقابلات في هذا القسم. اكتب إجابتك، قم بتقييمها، أو اضغط على "عرض الإجابة" بعد التفكير.
Implement the singleton, strategy, and command patterns. متوسط
Singleton:
public sealed class AppSettings {
private static readonly Lazy<AppSettings> _instance =
new Lazy<AppSettings>(() => new AppSettings());
public static AppSettings Instance => _instance.Value;
private AppSettings() {}
}
Strategy:
public interface IDiscountStrategy { decimal Apply(decimal price); }
public class NoDiscount : IDiscountStrategy { public decimal Apply(decimal p) => p; }
public class ChristmasDiscount : IDiscountStrategy { public decimal Apply(decimal p) => p * 0.8m; }
public class PriceCalculator {
private readonly IDiscountStrategy _strategy;
public PriceCalculator(IDiscountStrategy strategy) { _strategy = strategy; }
public decimal Calculate(decimal price) => _strategy.Apply(price);
}
Command:
public interface ICommand { void Execute(); }
public class SaveOrderCommand : ICommand {
public void Execute() { /* save order */ }
}
public class Invoker {
public void Run(ICommand command) => command.Execute();
}
What’s the difference between Repository and Query Object patterns? متوسط
Aspect | Repository Pattern | Query Object Pattern |
---|---|---|
Purpose | Abstracts persistence and CRUD operations. | Encapsulates a single complex query. |
Scope | Whole aggregate or entity set. | One-off or reusable query logic. |
Usage | IRepository<User> handles Add, Update, Delete, Get. |
UserByRoleQuery returns users by role. |
What are the advantages and disadvantages of using a Repository pattern? متوسط
Advantages:
- Centralized data access logic.
- Easier to mock/test.
- Decouples app from EF Core or other ORMs.
Disadvantages:
- Can become bloated with many methods.
- Sometimes duplicates EF Core features (extra layer).
- Harder to optimize queries if overly generic.