Skip to content

Instantly share code, notes, and snippets.

View PaulDuffy3's full-sized avatar

Paul D PaulDuffy3

  • Artifex Technology Consulting, Inc.
  • Lincoln, RI
View GitHub Profile
@PaulDuffy3
PaulDuffy3 / IUserRepository.cs
Created August 12, 2016 14:22
ASP.NET Core 1 NPoco Repository Interface
using System.Collections.Generic;
namespace NPOCOTestProject.DataLayer
{
public interface IUserRepository
{
Models.User Find(int id);
List<Models.User> GetAll();
Models.User Add(Models.User user);
Models.User Update(Models.User user);
@PaulDuffy3
PaulDuffy3 / UserRepository.cs
Created August 9, 2016 02:52
ASP.NET Core 1 NPoco Add
public User Add(User user)
{
db.BeginTransaction();
db.Insert(user);
db.CompleteTransaction();
return user;
}
@PaulDuffy3
PaulDuffy3 / Address.cs
Created August 9, 2016 02:21
ASP.Net Core 1 NPOCO Sample Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NPoco;
namespace NPOCOTestProject.Models
{
[TableName("ADDR_Address")]
[PrimaryKey("ADDR_ID")]
@PaulDuffy3
PaulDuffy3 / Address.cs
Created July 31, 2016 02:32
NPOCO - Address Class for ASP.NET Core 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NPoco;
namespace NPOCOTestProject.Models
{
[TableName("ADDR_Address")]
[PrimaryKey("ADDR_ID")]
@PaulDuffy3
PaulDuffy3 / PetaPoco.cs
Created July 23, 2016 01:09
PetaPoco Micro ORM for ASP.Net Core 1 with Fixes
/* PetaPoco - A Tiny ORMish thing for your POCO's.
* Copyright © 2011-2012 Topten Software. All Rights Reserved.
*
* Apache License 2.0 - http://www.toptensoftware.com/petapoco/license
*
* Special thanks to Rob Conery (@robconery) for original inspiration (ie:Massive) and for
* use of Subsonic's T4 templates, Rob Sullivan (@DataChomp) for hard core DBA advice
* and Adam Schroder (@schotime) for lots of suggestions, improvements and Oracle support
*/
@PaulDuffy3
PaulDuffy3 / PetaPoco.cs
Created July 23, 2016 00:59
PetaPoco Fix for ASP.Net Core 1
public Database(IDbConnection connection)
{
_sharedConnection = connection;
_connectionString = connection.ConnectionString;
_sharedConnectionDepth = 0;
_providerName = connection.GetType().Namespace;
CommonConstruct();
}
@PaulDuffy3
PaulDuffy3 / HomeController.cs
Created July 23, 2016 00:52
Save PetaPoco object to database
var user = new Models.User
{
USER_LastName = "Smith",
USER_FirstName = "Jim",
USER_FK_USST_ID = 1,
USER_FK_USTY_ID = 1,
USER_Password = "Pass",
USER_DateCreated = DateTime.Now,
USER_DateModified = DateTime.Now,
USER_EmailAddress = "JimSmith@gmail.com"
@PaulDuffy3
PaulDuffy3 / HomeController.cs
Created July 23, 2016 00:48
Inject Repository into Controller
private readonly Repositories.IUserRepository repository;
public HomeController(Repositories.IUserRepository iRepository)
{
repository = iRepository;
}
@PaulDuffy3
PaulDuffy3 / UserRepository.cs
Last active July 31, 2016 04:43
Insert/Update (Upsert) method for repository using PetaPoco
public void Save(User user)
{
using (var connection = new SqlConnection(Startup.ConnectionStrings.DefaultConnection))
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
if (user.IsNew)
@PaulDuffy3
PaulDuffy3 / UserRepository.cs
Created July 22, 2016 04:43
Load Repository with multiple items using PetaPoco
public User GetFullContact(int id)
{
var user = this.Find(id);
var addresses = this.db.Query<Address>("WHERE ADDR_ID = @0", id).ToList();
if (user != null && addresses != null)
{
user.Addresses.AddRange(addresses);
}