Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic Repo and Context #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions RepoDB.Trials.Business/Interfaces/IStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using RepoDB.Trials.Business.Repositories;
using RepoDB.Trials.Core;
using System;
using System.Collections.Generic;
using System.Text;

namespace RepoDB.Trials.Business.Interfaces
{
public interface IStorage
{
BasicRepository<T> GetRepository<T>() where T : BaseEntity;
}
}
19 changes: 19 additions & 0 deletions RepoDB.Trials.Business/Repositories/BasicRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.Data.SqlClient;
using RepoDb;
using RepoDB.Trials.Business.Interfaces;
using RepoDB.Trials.Core;
using System;
using System.Collections.Generic;
using System.Text;

namespace RepoDB.Trials.Business.Repositories
{
public class BasicRepository<T> : BaseRepository<T, SqlConnection> where T:BaseEntity
{
IAppSettings settings;
public BasicRepository(IAppSettings settings) : base(settings.ConnectionString, settings.CommandTimeout)
{
this.settings = settings;
}
}
}
36 changes: 36 additions & 0 deletions RepoDB.Trials.Business/Repositories/SqlDbStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Data.SqlClient;
using RepoDb;
using RepoDB.Trials.Business.Interfaces;
using RepoDB.Trials.Core;
using System;
using System.Collections.Generic;
using System.Text;

namespace RepoDB.Trials.Business.Repositories
{
public class SqlDbStorage:DbRepository<SqlConnection>,IStorage
{
private Dictionary<Type, object> repositories;

private readonly IAppSettings settings;
public SqlDbStorage(IAppSettings settings): base(settings.ConnectionString, settings.CommandTimeout)
{
this.settings = settings;

if (this.repositories == null)
{
this.repositories = new Dictionary<Type, object>();
}
}
public BasicRepository<T> GetRepository<T>() where T:BaseEntity
{
var type = typeof(T);
if (!this.repositories.ContainsKey(type))
{
this.repositories[type] = new BasicRepository<T>(settings);
}

return (BasicRepository<T>)this.repositories[type];
}
}
}
13 changes: 13 additions & 0 deletions RepoDB.Trials.Core/Store.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace RepoDB.Trials.Core
{
public class Store : BaseEntity
{
public string Code { get; set; }
public string Name { get; set; }
public int AuditStatus { get; set; }
}
}
13 changes: 8 additions & 5 deletions RepoDB.Trials.Web/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using RepoDB.Trials.Business.Implementations;
using RepoDB.Trials.Business.Interfaces;
using RepoDB.Trials.Business.Repositories;
using RepoDB.Trials.Core;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace RepoDB.Trials.Web.Controllers
{
Expand All @@ -17,17 +17,20 @@ namespace RepoDB.Trials.Web.Controllers
public class ProductsController : ControllerBase
{
private IOptions<AppSettings> settings;

public ProductsController(IOptions<AppSettings> settings)
private IStorage storage;
public ProductsController(IOptions<AppSettings> settings, IStorage storage)
{
this.settings = settings;
this.storage = storage;
}

[HttpGet]
public ActionResult Get()
{
var r = new ProductRepository(settings.Value);
return new ObjectResult(r.QueryAll());
//var r = new ProductRepository(settings.Value);
//return new ObjectResult(r.QueryAll());

return new ObjectResult(storage.GetRepository<Product>().QueryAll());
}

[HttpGet("{id}")]
Expand Down
57 changes: 57 additions & 0 deletions RepoDB.Trials.Web/Controllers/StandardController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using RepoDB.Trials.Business.Implementations;
using RepoDB.Trials.Business.Interfaces;
using RepoDB.Trials.Core;

namespace RepoDB.Trials.Web.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StandardController<T> : ControllerBase where T:BaseEntity
{
private IOptions<AppSettings> settings;
private IStorage storage;

public StandardController(IOptions<AppSettings> settings, IStorage storage)
{
this.settings = settings;
this.storage = storage;
}

[HttpGet]
public ActionResult Get()
{
return new ObjectResult(storage.GetRepository<T>().QueryAll());
}
[HttpGet("{id}")]
public ActionResult Get(long id)
{
return new ObjectResult(storage.GetRepository<T>().Query(p => p.Id == id));
}

[HttpPost]
public void Post([FromBody] T value)
{
storage.GetRepository<T>().Insert<long>(value);
}

[HttpPut("{id}")]
public void Put(long id, [FromBody] T value)
{
storage.GetRepository<T>().Update(value);
}

[HttpDelete("{id}")]
public void Delete(long id)
{
storage.GetRepository<T>().Delete(id);
}

}
}
22 changes: 22 additions & 0 deletions RepoDB.Trials.Web/Controllers/StoresController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using RepoDB.Trials.Business.Implementations;
using RepoDB.Trials.Business.Interfaces;
using RepoDB.Trials.Core;

namespace RepoDB.Trials.Web.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StoresController : StandardController<Store>
{
public StoresController(IOptions<AppSettings> settings, IStorage storage) : base(settings, storage)
{
}
}
}
8 changes: 5 additions & 3 deletions RepoDB.Trials.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using RepoDB.Trials.Business.Implementations;
using RepoDB.Trials.Business.Interfaces;
using RepoDB.Trials.Business.Repositories;

namespace RepoDB.Trials.Web
{
Expand All @@ -28,7 +30,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddControllers().AddControllersAsServices();

services.AddMvc();
services.AddOptions();
Expand All @@ -37,8 +39,8 @@ public void ConfigureServices(IServiceCollection services)
c.SwaggerDoc("v1", new OpenApiInfo { Title = "RepoDB API", Version = "v1" });
});

services.AddOptions<AppSettings>().Bind(Configuration.GetSection("SettingsFromApi"));

services.AddSingleton<IAppSettings>(s => Configuration.GetSection("SettingsFromApi").Get<AppSettings>());
services.AddSingleton<IStorage, SqlDbStorage>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
Loading