diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index df2e0fe..0000000 --- a/.dockerignore +++ /dev/null @@ -1,9 +0,0 @@ -.dockerignore -.env -.git -.gitignore -.vs -.vscode -*/bin -*/obj -**/.toolstarget \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ae43283 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,36 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/MongoDB.GenericRepository/bin/Debug/netcoreapp3.1/MongoDB.GenericRepository.dll", + "args": [], + "cwd": "${workspaceFolder}/MongoDB.GenericRepository", + "stopAtEntry": false, + // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..d90cba6 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/MongoDB.GenericRepository/MongoDB.GenericRepository.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/MongoDB.GenericRepository/MongoDB.GenericRepository.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/MongoDB.GenericRepository/MongoDB.GenericRepository.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index b028a86..0000000 --- a/Dockerfile +++ /dev/null @@ -1,22 +0,0 @@ -#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. - -FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base -WORKDIR /app -EXPOSE 80 -EXPOSE 443 - -FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build -WORKDIR /src -COPY ["MongoDB.GenericRepository/MongoDB.GenericRepository.csproj", "MongoDB.GenericRepository/"] -RUN dotnet restore "MongoDB.GenericRepository/MongoDB.GenericRepository.csproj" -COPY . . -WORKDIR "/src/MongoDB.GenericRepository" -RUN dotnet build "MongoDB.GenericRepository.csproj" -c Release -o /app/build - -FROM build AS publish -RUN dotnet publish "MongoDB.GenericRepository.csproj" -c Release -o /app/publish - -FROM base AS final -WORKDIR /app -COPY --from=publish /app/publish . -ENTRYPOINT ["dotnet", "MongoDB.GenericRepository.dll"] \ No newline at end of file diff --git a/MongoDB.GenericRepository/Context/MongoContext.cs b/MongoDB.GenericRepository/Context/MongoContext.cs index eeb143e..908a07a 100644 --- a/MongoDB.GenericRepository/Context/MongoContext.cs +++ b/MongoDB.GenericRepository/Context/MongoContext.cs @@ -28,16 +28,16 @@ public async Task SaveChanges() { ConfigureMongo(); - using (Session = await MongoClient.StartSessionAsync()) - { - Session.StartTransaction(); + //using (Session = await MongoClient.StartSessionAsync()) + //{ + // Session.StartTransaction(); - var commandTasks = _commands.Select(c => c()); + var commandTasks = _commands.Select(c => c()); - await Task.WhenAll(commandTasks); + await Task.WhenAll(commandTasks); - await Session.CommitTransactionAsync(); - } + // await Session.CommitTransactionAsync(); + //} return _commands.Count; } diff --git a/MongoDB.GenericRepository/Model/Product.cs b/MongoDB.GenericRepository/Model/Product.cs index 0a04a3f..8ca91fc 100644 --- a/MongoDB.GenericRepository/Model/Product.cs +++ b/MongoDB.GenericRepository/Model/Product.cs @@ -1,4 +1,6 @@ -using System; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using System; namespace MongoDB.GenericRepository.Model { @@ -16,7 +18,9 @@ public Product(Guid id, string description) Description = description; } + [BsonId] public Guid Id { get; private set; } + public string Description { get; private set; } } } \ No newline at end of file diff --git a/MongoDB.GenericRepository/MongoDB.GenericRepository.csproj b/MongoDB.GenericRepository/MongoDB.GenericRepository.csproj index 8b87182..61a2202 100644 --- a/MongoDB.GenericRepository/MongoDB.GenericRepository.csproj +++ b/MongoDB.GenericRepository/MongoDB.GenericRepository.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net5.0 InProcess Linux 3ea642bd-6e7a-4df2-aa87-d3ee9b381e84 @@ -12,11 +12,11 @@ - - - - - + + + + + diff --git a/MongoDB.GenericRepository/Persistence/MongoDbPersistence.cs b/MongoDB.GenericRepository/Persistence/MongoDbPersistence.cs index d716fc6..a99559a 100644 --- a/MongoDB.GenericRepository/Persistence/MongoDbPersistence.cs +++ b/MongoDB.GenericRepository/Persistence/MongoDbPersistence.cs @@ -5,6 +5,7 @@ namespace MongoDB.GenericRepository.Persistence { public static class MongoDbPersistence { + [System.Obsolete] public static void Configure() { ProductMap.Configure(); diff --git a/MongoDB.GenericRepository/Persistence/ProductMap.cs b/MongoDB.GenericRepository/Persistence/ProductMap.cs index 55e4fad..64861ca 100644 --- a/MongoDB.GenericRepository/Persistence/ProductMap.cs +++ b/MongoDB.GenericRepository/Persistence/ProductMap.cs @@ -12,7 +12,7 @@ public static void Configure() map.AutoMap(); map.SetIgnoreExtraElements(true); map.MapIdMember(x => x.Id); - map.MapMember(x => x.Description).SetIsRequired(true); + map.MapMember(x => x.Description); }); } } diff --git a/MongoDB.GenericRepository/Repository/BaseRepository.cs b/MongoDB.GenericRepository/Repository/BaseRepository.cs index 5466899..76b502d 100644 --- a/MongoDB.GenericRepository/Repository/BaseRepository.cs +++ b/MongoDB.GenericRepository/Repository/BaseRepository.cs @@ -19,9 +19,9 @@ protected BaseRepository(IMongoContext context) DbSet = Context.GetCollection(typeof(TEntity).Name); } - public virtual void Add(TEntity obj) + public virtual async Task> GetAll() { - Context.AddCommand(() => DbSet.InsertOneAsync(obj)); + return await DbSet.Find(Builders.Filter.Empty).ToListAsync(); } public virtual async Task GetById(Guid id) @@ -30,10 +30,9 @@ public virtual async Task GetById(Guid id) return data.SingleOrDefault(); } - public virtual async Task> GetAll() + public virtual void Add(TEntity obj) { - var all = await DbSet.FindAsync(Builders.Filter.Empty); - return all.ToList(); + Context.AddCommand(() => DbSet.InsertOneAsync(obj)); } public virtual void Update(TEntity obj) diff --git a/MongoDB.GenericRepository/Startup.cs b/MongoDB.GenericRepository/Startup.cs index 6ec1461..39c7c5e 100644 --- a/MongoDB.GenericRepository/Startup.cs +++ b/MongoDB.GenericRepository/Startup.cs @@ -11,6 +11,7 @@ using MongoDB.GenericRepository.Repository; using MongoDB.GenericRepository.UoW; using System; +using System.IO; namespace MongoDB.GenericRepository { @@ -24,6 +25,7 @@ public Startup(IConfiguration configuration) } // This method gets called by the runtime. Use this method to add services to the container. + [Obsolete] public void ConfigureServices(IServiceCollection services) { services.AddControllers() @@ -48,15 +50,16 @@ public void ConfigureServices(IServiceCollection services) Description = "Swagger surface", Contact = new OpenApiContact { - Name = "Bruno Brito", - Email = "bhdebrito@gmail.com", - Url = new Uri("http://www.brunobrito.net.br") - }, - License = new OpenApiLicense - { - Name = "MIT", - Url = new Uri("https://github.com/brunohbrito/MongoDB-RepositoryUoWPatterns/blob/master/LICENSE") + Name = "Nguyễn Ngọc Vinh", + Email = "Nguyenngocvinh@cdktcnqn.edu.vn", + Url = new Uri("http://cdktcnqn.edu.vn") } + //, + //License = new OpenApiLicense + //{ + // Name = "MIT", + // Url = new Uri("https://github.com/brunohbrito/MongoDB-RepositoryUoWPatterns/blob/master/LICENSE") + //} }); }); @@ -93,6 +96,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseSwaggerUI(s => { s.SwaggerEndpoint("/swagger/v1/swagger.json", "Repository Pattern and Unit of Work API v1.0"); + }); } diff --git a/MongoDB.GenericRepository/appsettings.json b/MongoDB.GenericRepository/appsettings.json index 65ff7fa..dfe1742 100644 --- a/MongoDB.GenericRepository/appsettings.json +++ b/MongoDB.GenericRepository/appsettings.json @@ -1,12 +1,12 @@ { - "Logging": { - "LogLevel": { - "Default": "Warning" - } - }, - "AllowedHosts": "*", + "Logging": { + "LogLevel": { + "Default": "Warning" + } + }, + "AllowedHosts": "*", "MongoSettings": { - "Connection": "mongodb://root:password123@localhost:27017", - "DatabaseName": "Products" + "Connection": "mongodb://localhost:27017", + "DatabaseName": "test" } } diff --git a/README.MD b/README.MD index a3a069a..de5506a 100644 --- a/README.MD +++ b/README.MD @@ -1,26 +1,11 @@ -# ASP.NET Core - MongoDB Repository & Unit of Work Pattern - - -Example App - This demo shows how to simple implement repository and unit of work patterns for MongoDB .Net driver. - * **Server Side**: ASP.NET Core Pre reqs -* Net.Core 3.1 +* Net 5 Techs: * ASP.NET Core * Swagger -* ServiceStack.Core - -# How to run - -1. Download code - * Open folder - * Open a terminal - * docker-compose up --build - - -More about on [ASP.NET Core - MongoDB Repository Pattern & Unit Of Work](https://www.brunobrito.net.br/aspnet-core-mongodb-unit-of-work/) - Portuguese only +* ServiceStack.Core \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 83939eb..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,62 +0,0 @@ -# Use root/example as user/password credentials -version: '3.8' - -services: - - generic-repo-uow: - image: "generic-repo-uow" - build: - context: . - dockerfile: Dockerfile - ports: - - "5005:5005" - links: - - mongodb-primary - depends_on: - - mongodb-primary - environment: - MONGOCONNECTION: "mongodb://root:password123@mongodb-primary:27017" - DATABASENAME: "Products" - ASPNETCORE_ENVIRONMENT: "Development" - ASPNETCORE_URLS: http://+:5005 - - mongodb-primary: - image: 'bitnami/mongodb:latest' - ports: - - "27017:27017" - environment: - - MONGODB_ADVERTISED_HOSTNAME=mongodb-primary - - MONGODB_REPLICA_SET_MODE=primary - - MONGODB_ROOT_PASSWORD=password123 - - MONGODB_REPLICA_SET_KEY=replicasetkey123 - - # volumes: - # - 'mongodb_master_data:/bitnami' - - mongodb-secondary: - image: 'bitnami/mongodb:latest' - depends_on: - - mongodb-primary - environment: - - MONGODB_ADVERTISED_HOSTNAME=mongodb-secondary - - MONGODB_REPLICA_SET_MODE=secondary - - MONGODB_INITIAL_PRIMARY_HOST=mongodb-primary - - MONGODB_INITIAL_PRIMARY_PORT_NUMBER=27017 - - MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=password123 - - MONGODB_REPLICA_SET_KEY=replicasetkey123 - - mongodb-arbiter: - image: 'bitnami/mongodb:latest' - depends_on: - - mongodb-primary - environment: - - MONGODB_ADVERTISED_HOSTNAME=mongodb-arbiter - - MONGODB_REPLICA_SET_MODE=arbiter - - MONGODB_INITIAL_PRIMARY_HOST=mongodb-primary - - MONGODB_INITIAL_PRIMARY_PORT_NUMBER=27017 - - MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=password123 - - MONGODB_REPLICA_SET_KEY=replicasetkey123 - -# volumes: -# mongodb_master_data: -# driver: local \ No newline at end of file