{"id":22640914,"url":"https://github.com/aliencube/entity-context-library","last_synced_at":"2026-03-03T21:32:10.404Z","repository":{"id":23173878,"uuid":"26529847","full_name":"aliencube/Entity-Context-Library","owner":"aliencube","description":"This provides a common and reusable interfaces for projects using Entity Framework","archived":false,"fork":false,"pushed_at":"2016-07-11T06:22:59.000Z","size":1553,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T05:40:07.505Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aliencube.png","metadata":{"files":{"readme":"README-1.x.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-12T10:02:18.000Z","updated_at":"2020-03-23T06:38:49.000Z","dependencies_parsed_at":"2022-07-25T09:47:05.438Z","dependency_job_id":null,"html_url":"https://github.com/aliencube/Entity-Context-Library","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliencube%2FEntity-Context-Library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliencube%2FEntity-Context-Library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliencube%2FEntity-Context-Library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliencube%2FEntity-Context-Library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aliencube","download_url":"https://codeload.github.com/aliencube/Entity-Context-Library/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248487668,"owners_count":21112191,"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","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":[],"created_at":"2024-12-09T04:14:55.288Z","updated_at":"2026-03-03T21:32:10.365Z","avatar_url":"https://github.com/aliencube.png","language":"C#","readme":"# Entity Context Library #\n\n**Entity Context Library (ECL)** provides a common and reusable interfaces for projects using [Entity Framework](http://www.asp.net/entity-framework).\n\n\n## Package Status ##\n\n[![Build status](https://ci.appveyor.com/api/projects/status/06bu85cjywdlfa51/branch/dev?svg=true)](https://ci.appveyor.com/project/justinyoo/entity-context-library/branch/dev) [![](https://img.shields.io/nuget/v/Aliencube.EntityContextLibrary.svg)](https://www.nuget.org/packages/Aliencube.EntityContextLibrary/) \n\n\n## Entity Framework Support ##\n\n**ECL** `1.x` supports Entity Framework 6.x.\n\n\n## Getting Started ##\n\n**ECL** provides of four distinctive interfaces \u0026ndash; `IDbContextFactory`, `IBaseRepository`, `IUnitOfWork` and `IUnitOfWorkManager`.\n\n\n### `IDbContextFactory` ###\n\n`IDbContextFactory` interface provides a simple interface to return a `DbContext` instance based on type. This can be useful when multiple database connection strings are used in one application. Here's a sample code snippet, assuming [`Autofac`](http://autofac.org) is used together, as an IoC container.\n\n```csharp\nusing Autofac;\n...\n\npublic static class Program\n{\n  private const string MY_DB_CONTEXT = \"MyDbContextName\";\n  private const string ANOTHER_DB_CONTEXT = \"AnotherDbContextName\";\n\n  public static void Main(string[] args)\n  {\n    var builder = new ContainerBuilder();\n\n    // Register MyDbContext with DbContextFactory.\n    builder.RegisterType\u003cDbContextFactory\u003cMyDbContext\u003e\u003e()\n           .Named\u003cIDbContextFactory\u003e(SERVICE_NAME)\n           .As\u003cIDbContextFactory\u003e();\n\n    // Register AnotherDbContext with DbContextFactory.\n    builder.RegisterType\u003cDbContextFactory\u003cAnotherDbContext\u003e\u003e()\n           .Named\u003cIDbContextFactory\u003e(SERVICE_NAME)\n           .As\u003cIDbContextFactory\u003e();\n    ...\n\n    _container = builder.Build();\n  }\n}\n\n```\n\n\n### `IBaseRepository` ###\n\n`IBaseRepository` interface provides a basic CRUD methods for each repository representing a table in a database. Therefore, each repository can just inherit the base repository class and use it. In addition to this, all methods like `Get`, `Add`, `AddRange`, `Update`, `UpdateRange`, `Delete` and `DeleteRange` methods are overrideable, so you can redefine your way of `SELECT`, `INSERT`, `UPDATE` and `DELETE` actions. Here's a sample usage.\n\n```csharp\n// Assuming that the contextFactory instance already exists.\nIBaseRepository\u003cProduct\u003e productRepository = new BaseRepository\u003cProduct\u003e(contextFactory);\n\nvar product = new Product() { ProductId = 1 };\nproductRepository.Add(product);\n```\n\nIf you want to extend more, you can do the following:\n\n```csharp\npublic interface IProductRepository : IBaseRepository\u003cProduct\u003e\n{\n  // You can put as many methods as you want here.\n}\n\npublic class ProductRepository : BaseRepository\u003cProduct\u003e, IProductRepository\n{\n  public ProductRepository(IDbContextFactory contextFactory)\n    : base(contextFactory)\n  {\n  }\n\n  // You can here implement methods defined in the interface above. \n}\n\n...\n\nIProductRepository productRepository = new ProductRepository(contextFactory);\n\nvar product = new Product() { ProductId = 1 };\nproductRepository.Add(product);\n```\n\n\n#### Async Methods ####\n\n`Add`, `AddRange`, `Update`, `UpdateRange`, `Delete` and `DeleteRange` have their corresponding async methods like `AddAsync`, `AddRangeAsync`, `UpdateAsync`, `UpdateRangeAsync`, `DeleteAsync` and `DeleteRangeAsync` in `IBaseRepository`. Therefore, you can get benefits from async programming.\n\n```csharp\nvar product = new Product() { ProductId = 1 };\nawait productRepository.AddAsync(product);\n```\n\n\n#### Stored Procedures ####\n\n`IBaseRepository` also provides methods to run stored procedures:\n\n* `ExecuteStoreQuery` is used mainly for `SELECT` statement.\n\n```csharp\nvar results = productRepository.ExecuteStoreQuery\u003cProduct\u003e(\"EXEC GetProduct @ProductId\", new { ProductId = 1 });\n```\n\n* `ExecuteStoreCommand` is used mainly for `INSERT`, `UPDATE` and `DELETE` statements.\n\n```csharp\nvar result = productRepository.ExecuteStoreCommand(\"EXEC AddProduct @Name, @Description, @Price\", new { Name = \"My Product\", Description = \"This is awesome\", Price = 10.00M });\n```\n\nWith `Autofac`, you can put a line of code into the IoC container:\n\n```csharp\n// Register Product Repository #1:\nbuilder.Register(p =\u003e new BaseRepository\u003cProduct\u003e(p.ResolveNamed\u003cIDbContextFactory\u003e(SERVICE_NAME)))\n       .As\u003cIBaseRepository\u003cProduct\u003e\u003e();\n\n// Register Product Repository #2:\nbuilder.Register(p =\u003e new ProductRepository(p.ResolveNamed\u003cIDbContextFactory\u003e(SERVICE_NAME)))\n       .As\u003cIProductRepository\u003e();\n```\n\n\n### `IUnitOfWorkManager` ###\n\n`IUnitOfWorkManager` interface only provides one method, `CreateInstance` to create and dispose `UnitOfWork` instance. With `Autofac`, you can put a line of code into the IoC container:\n\n```csharp\n// Register UnitOfWorkManager.\nbuilder.Register(p =\u003e new UnitOfWorkManager(p.ResolveNamed\u003cIDbContextFactory\u003e(MY_DB_CONTEXT)))\n       .As\u003cIUnitOfWorkManager\u003e();\n```\n\nIf you want to handle multiple `DbContext` instances, you can add as many `DbContext` instances as you want.\n\n```csharp\n// Register UnitOfWorkManager.\nbuilder.Register(p =\u003e new UnitOfWorkManager(p.ResolveNamed\u003cIDbContextFactory\u003e(MY_DB_CONTEXT),\n                                            p.ResolveNamed\u003cIDbContextFactory\u003e(ANOTHER_DB_CONTEXT)))\n       .As\u003cIUnitOfWorkManager\u003e();\n```\n\n### `IUnitOfWork` ###\n\n`IUnitOfWork` interface handles database transactions for `INSERT`, `UPDATE` and `DELETE`. Therefore it provides transaction related methods like `BeginTransaction`, `SaveChanges`, `Commit` and `Rollback`. You can use this within your database access layer like:\n\n```csharp\n// ProductQueryManager performs INSERT/UPDATE/DELETE actions.\npublic class ProductQueryManager\n{\n  private readonly IUnitOfWorkManager _uowm;\n  private readonly IProductRepository _product;\n\n  public ProductQueryManager(IUnitOfWorkManager uowm, IProductRepository product)\n  {\n    if (uowm == null)\n    {\n      throw new ArgumentNullException(\"uowm\");\n    }\n    this._uowm = uowm;\n\n    if (product == null)\n    {\n      throw new ArgumentNullException(\"product\");\n    }\n    this._product = product;\n  }\n\n  // Adds a product into the table.\n  public bool Add(Product product)\n  {\n    using (var uow = this._uowm.CreateInstance\u003cMyDbContext\u003e())\n    {\n      uow.BeginTransaction();\n\n      try\n      {\n        this._productRepository.Add(product);\n        uow.Commit();\n        return true;\n      }\n      catch (Exception ex)\n      {\n        uow.Rollback();\n\n        //\n        // Do some error handling logic here.\n        //\n\n        return false;\n      }\n    }\n  }\n\n  // Updates a product on the table.\n  public bool Update(Product product)\n  {\n    using (var uow = this._uowm.CreateInstance\u003cMyDbContext\u003e())\n    {\n      uow.BeginTransaction();\n\n      try\n      {\n        this._productRepository.Update(product);\n        uow.Commit();\n        return true;\n      }\n      catch (Exception ex)\n      {\n        uow.Rollback();\n\n        //\n        // Do some error handling logic here.\n        //\n\n        return false;\n      }\n    }\n  }\n\n  // Deletes a product from the table.\n  public bool Delete(Product product)\n  {\n    using (var uow = this._uowm.CreateInstance\u003cMyDbContext\u003e())\n    {\n      uow.BeginTransaction();\n\n      try\n      {\n        this._productRepository.Delete(product);\n        uow.Commit();\n        return true;\n      }\n      catch (Exception ex)\n      {\n        uow.Rollback();\n\n        //\n        // Do some error handling logic here.\n        //\n\n        return false;\n      }\n    }\n  }\n}\n```\n\n\n## Contribution ##\n\nYour contributions are always welcome! All your work should be done in your forked repository. Once you finish your work, please send us a pull request onto our `dev` branch for review.\n\n\n## License ##\n\n**Entity Context Library (ECL)** is released under [MIT License](http://opensource.org/licenses/MIT)\n\n\u003e The MIT License (MIT)\n\u003e\n\u003e Copyright (c) 2014 [aliencube.org](http://aliencube.org)\n\u003e \n\u003e Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\u003e \n\u003e The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\u003e \n\u003e THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faliencube%2Fentity-context-library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faliencube%2Fentity-context-library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faliencube%2Fentity-context-library/lists"}