{"id":29669994,"url":"https://github.com/marvinklein1508/dbcontroller","last_synced_at":"2026-04-16T05:31:07.710Z","repository":{"id":153832625,"uuid":"604597319","full_name":"MarvinKlein1508/DbController","owner":"MarvinKlein1508","description":"A database wrapper library.","archived":false,"fork":false,"pushed_at":"2024-11-14T11:09:53.000Z","size":62,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-22T07:26:28.353Z","etag":null,"topics":["database","mysql","sqlserver"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MarvinKlein1508.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-02-21T11:55:42.000Z","updated_at":"2024-11-14T10:54:21.000Z","dependencies_parsed_at":"2023-11-08T11:29:28.592Z","dependency_job_id":"ab2f6e61-90f7-4e5e-898b-ba5602f6d931","html_url":"https://github.com/MarvinKlein1508/DbController","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/MarvinKlein1508/DbController","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarvinKlein1508%2FDbController","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarvinKlein1508%2FDbController/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarvinKlein1508%2FDbController/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarvinKlein1508%2FDbController/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MarvinKlein1508","download_url":"https://codeload.github.com/MarvinKlein1508/DbController/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarvinKlein1508%2FDbController/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266554434,"owners_count":23947333,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","mysql","sqlserver"],"created_at":"2025-07-22T19:06:00.543Z","updated_at":"2026-04-16T05:31:07.703Z","avatar_url":"https://github.com/MarvinKlein1508.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DbController\n\nAn easy to use database wrapper which uses Dapper.\n\n## What does this package do?\nThis package has been created to minify the effort to write database queries in C#. \n\nFor example take a look on this block of code:\n```csharp\nDataTable result = new DataTable();\nMySqlConnection connection = new MySqlConnection(\"YOUR_CONNECTION_STRING\");\nconnection.Open();\nMySqlCommand command = connection.CreateCommand();\ncommand.CommandText = \"SELECT * FROM customers\";\nMySqlDataAdapter adapter = new MySqlDataAdapter(command);\nadapter.Fill(result);\nconnection.Close();\ncommand.Dispose();\nconnection.Dispose();\n```\n\nWith this package this code can be reduced to:\n```csharp\nusing MySqlController dbController = await MySqlController.CreateAsync(\"YOUR_CONNECTION_STRING\");\nList\u003cCustomer\u003e customer = await dbController.SelectDataAsync\u003cCustomer\u003e(\"SELECT * FROM customers\");\n```\n\nIt can even be more reduced when passing the `IDbController` around.\n```\npublic Task\u003cList\u003cCustomer\u003e\u003e GetCustomers(IDbController dbController)\n{\n\treturn dbController.SelectDataAsync\u003cCustomer\u003e(\"SELECT * FROM customers\");\n}\n```\n\nOnce initialized, the connection to the database will be opened automatically and it will kept open until the DbController gets disposed. In addition it keeps track of the active transaction with no addition work. \n\n## Installation\n\nWe offer different packages for this library. \n\nFor MySql use:\n\n`Install-Package DbController.MySql`\n\nFor SqlServer use:\n\n`Install-Package DbController.SqlServer`\n\nIf you want to implement your own DbController then you should use:\n\n`Install-Package DbController`\n\n## Usage\n\nStart by creating an instance of the DbController based on the package you want to use:\n\nWhen using the `DbController.MySql` package a new controller can be created like this:\n``` csharp\nusing MySqlController dbController = await MySqlController.CreateAsync(\"YOUR_CONNECTION_STRING\");\n``` \n\nFor the `DbController.SqlServer` package:\n``` csharp\nusing SqlController dbController = await SqlController.CreateAsync(\"YOUR_CONNECTION_STRING\");\n``` \n\nYou can also use the interface to change the used DbController at runtime with dependency injection.\n``` csharp\nusing IDbController dbController = await MySqlController.CreateAsync(\"YOUR_CONNECTION_STRING\");\n``` \n\n`DbController` is using [Dapper](https://github.com/DapperLib/Dapper) under the hood for object mapping. \n\n## Extension to Dappers mapping\nThis package provides you with an attribute to map columns in the database to the corresponding property from your object. For example your customers table has a column with the name `customer_id`�. Your C# class uses a slightly different name to match the C# naming conventions. \n\nNow you can add an attribute to your property like this:\n```csharp\npublic class Customer\n{\n\t[CompareField(\"customer_id\")]\n\tpublic int CustomerId { get; set; }\n}\n```\n\nBoth packages will create a `TypeAttributeCache` mapping all `CustomFieldAttributes` to the corresponding properties.\n\nThis will be done in the static constructor for the IDbController implementation. If you want to add this functionality to your own implementation of IDbController you'll should add a static constructor like this:\n```csharp\n/// \u003csummary\u003e\n/// Static constructor to initialize the TypeAttributeCache\n/// \u003c/summary\u003e\nstatic MySqlController()\n{\n    // INIT Dapper for CompareField\n    foreach (Type type in SingletonTypeAttributeCache.CacheAll\u003cCompareFieldAttribute\u003e((att) =\u003e att.FieldNames))\n    {\n        SqlMapper.SetTypeMap(type, new CustomPropertyTypeMap(\n            type,\n            (type, columnName) =\u003e\n            {\n                PropertyInfo? prop = SingletonTypeAttributeCache.Get(type, columnName);\n\n                return prop is null ? type.GetProperty(columnName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) : prop;\n\n            }\n        ));\n    }\n}\n```\n\n## Methods\nEvery IDbController provides the following methods to work with:\n\n|Method|Description|Returns\n|------|-----------|-------\n|QueryAsync|Executes SQL|---\n|GetFirstAsync\\\u003cT\\\u003e|Executes SQL and returns maps the result to the corresponding object|Returns an instance of the object, null when no row is found\n|SelectDataAsync\\\u003cT\\\u003e|Executes SQL and maps all results into a list|If no object is found it will return an empty list\n|ExecuteReaderAsync|Executes SQL and allows to process each row individually\n|StartTransactionAsync|Starts a new transaction|Nothing will be returned\n|CommitChangesAsync|Commits and writes all data from the current transaction|---\n|RollbackChangesAsync|Rolls back all changes from the current transaction|---\n|GetLastIdSql|Generates a valid SQL string to fetch the last inserted id|string\n|GetPaginationSyntax|Generates a valid SQL string for pagination within SQL statements|string\n\n\n## Best practices\nIt's generally recommended to pass an instance of IDbController to each function which needs to execute some SQL. For example:\n```csharp\npublic static async Task Main(string[] args)\n{\n    using IDbController dbController = await MySqlController.CreateAsync(\"YOUR_CONNECTIONSTRING\");\n\n    List\u003cCustomer\u003e customers = await GetCustomers(dbController);\n    List\u003cEmployee\u003e employees = await GetEmployees(dbController);\n}\n\npublic Task\u003cList\u003cCustomer\u003e\u003e GetCustomers(IDbController dbController)\n{\n\treturn dbController.SelectDataAsync\u003cCustomer\u003e(\"SELECT * FROM customers\");\n}\n\npublic Task\u003cList\u003cEmployee\u003e\u003e GetEmployees(IDbController dbController)\n{\n\treturn dbController.SelectDataAsync\u003cEmployee\u003e(\"SELECT * FROM employees\");\n}\n```\n\nThe main benefit is that every function will be able to read from the current transaction state.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarvinklein1508%2Fdbcontroller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarvinklein1508%2Fdbcontroller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarvinklein1508%2Fdbcontroller/lists"}