diff --git a/Services/userService.cs b/Services/userService.cs new file mode 100644 index 0000000..4de990b --- /dev/null +++ b/Services/userService.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DemoApp.Services +{ + // Wrong naming - should be PascalCase + public class userService + { + // Public field instead of property + public string username; + + // Missing m_ prefix for private field + private int userId; + + // No #region blocks for organization + + public string connectionString = "Server=localhost;Database=demo;User=sa;Password=admin123"; + + // Inconsistent parameter casing + public async Task GetData(string InputParam) + { + // Wrong casing for local variable + string Result = ""; + + // SQL Injection vulnerability + var query = "SELECT * FROM Users WHERE Name = '" + InputParam + "'"; + + // Hardcoded password + var password = "admin123"; + + // Not using async properly + var users = GetAllUsers().Result; + + // String concatenation in loop + for (int i = 0; i < users.Count; i++) + { + Result = Result + users[i].Name + ", "; + } + + // This method is way too long (imagine 50+ more lines here) + // Violating the 30-line rule + + // Deep nesting example + if (InputParam != null) + { + if (InputParam.Length > 0) + { + if (InputParam.Contains("admin")) + { + if (password == "admin123") + { + // Too deeply nested + return "Admin access granted"; + } + } + } + } + + // Magic number without constant + if (users.Count > 100) + { + return "Too many users"; + } + + // Missing null checks + var firstUser = users.First(); + + // Not disposing resources + var connection = new System.Data.SqlClient.SqlConnection(connectionString); + connection.Open(); + // Never closed or disposed + + // Missing XML documentation + // Missing error handling + // Missing parameter validation + + return Result; + } + + // Method name should be PascalCase + public void processOrder(int OrderId) + { + // Local variable should be camelCase + var OrderStatus = "Processing"; + + // Public method without any documentation + } + + // Missing async on method that returns Task + public Task> GetAllUsers() + { + // Synchronous operation returning Task + var users = new List(); + return Task.FromResult(users); + } + + // N+1 query pattern example + public void LoadUserOrders() + { + var users = GetAllUsers().Result; + + foreach (var user in users) + { + // This would execute a query for each user + var orders = GetOrdersForUser(user.Id); + user.Orders = orders; + } + } + + private List GetOrdersForUser(int userId) + { + // Imagine database query here + return new List(); + } + } + + // Helper classes + public class User + { + public int Id { get; set; } + public string Name { get; set; } + public List Orders { get; set; } + } + + public class Order + { + public int Id { get; set; } + public string Status { get; set; } + } \ No newline at end of file