Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions .dockerignore

This file was deleted.

36 changes: 36 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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}"
}
]
}
42 changes: 42 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
22 changes: 0 additions & 22 deletions Dockerfile

This file was deleted.

14 changes: 7 additions & 7 deletions MongoDB.GenericRepository/Context/MongoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ public async Task<int> 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;
}
Expand Down
6 changes: 5 additions & 1 deletion MongoDB.GenericRepository/Model/Product.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;

namespace MongoDB.GenericRepository.Model
{
Expand All @@ -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; }
}
}
12 changes: 6 additions & 6 deletions MongoDB.GenericRepository/MongoDB.GenericRepository.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<DockerTargetOS>Linux</DockerTargetOS>
<UserSecretsId>3ea642bd-6e7a-4df2-aa87-d3ee9b381e84</UserSecretsId>
Expand All @@ -12,11 +12,11 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.9" />
<PackageReference Include="MongoDB.Driver" Version="2.11.2" />
<PackageReference Include="ServiceStack.Core" Version="5.9.2" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.5.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="5.5.1" />
<PackageReference Include="MongoDB.Driver" Version="2.11.6" />
<PackageReference Include="ServiceStack.Core" Version="5.10.4" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="5.6.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace MongoDB.GenericRepository.Persistence
{
public static class MongoDbPersistence
{
[System.Obsolete]
public static void Configure()
{
ProductMap.Configure();
Expand Down
2 changes: 1 addition & 1 deletion MongoDB.GenericRepository/Persistence/ProductMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
Expand Down
9 changes: 4 additions & 5 deletions MongoDB.GenericRepository/Repository/BaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ protected BaseRepository(IMongoContext context)
DbSet = Context.GetCollection<TEntity>(typeof(TEntity).Name);
}

public virtual void Add(TEntity obj)
public virtual async Task<IEnumerable<TEntity>> GetAll()
{
Context.AddCommand(() => DbSet.InsertOneAsync(obj));
return await DbSet.Find(Builders<TEntity>.Filter.Empty).ToListAsync();
}

public virtual async Task<TEntity> GetById(Guid id)
Expand All @@ -30,10 +30,9 @@ public virtual async Task<TEntity> GetById(Guid id)
return data.SingleOrDefault();
}

public virtual async Task<IEnumerable<TEntity>> GetAll()
public virtual void Add(TEntity obj)
{
var all = await DbSet.FindAsync(Builders<TEntity>.Filter.Empty);
return all.ToList();
Context.AddCommand(() => DbSet.InsertOneAsync(obj));
}

public virtual void Update(TEntity obj)
Expand Down
20 changes: 12 additions & 8 deletions MongoDB.GenericRepository/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using MongoDB.GenericRepository.Repository;
using MongoDB.GenericRepository.UoW;
using System;
using System.IO;

namespace MongoDB.GenericRepository
{
Expand All @@ -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()
Expand All @@ -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")
//}
});
});

Expand Down Expand Up @@ -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");

});
}

Expand Down
16 changes: 8 additions & 8 deletions MongoDB.GenericRepository/appsettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
19 changes: 2 additions & 17 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
# ASP.NET Core - MongoDB Repository & Unit of Work Pattern
<img src="https://www.brunobrito.net.br/content/images/2019/02/capa-social.jpg" width="480" />

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
62 changes: 0 additions & 62 deletions docker-compose.yml

This file was deleted.