rewrite DAM to be non-generic
This commit is contained in:
parent
79b3b5be1d
commit
3ce8679d80
@ -9,6 +9,11 @@
|
||||
this.creditorLibrary = creditorLibrary;
|
||||
}
|
||||
|
||||
public Creditor GetCreditor(int creditorId)
|
||||
{
|
||||
return creditorLibrary.GetCreditor(creditorId);
|
||||
}
|
||||
|
||||
public string GetCreditorStatus(int creditorId)
|
||||
{
|
||||
return creditorLibrary.GetCreditorStatus(creditorId);
|
||||
|
@ -11,6 +11,11 @@ namespace COA.EnterpriseServices.Creditors
|
||||
this.creditorHelper = creditorHelper;
|
||||
}
|
||||
|
||||
public Creditor GetCreditor(int creditorId)
|
||||
{
|
||||
return creditorHelper.GetCreditor(creditorId);
|
||||
}
|
||||
|
||||
public string GetCreditorStatus(int creditorId)
|
||||
{
|
||||
var creditor = creditorHelper.GetCreditor(creditorId);
|
||||
|
125
COA.EnterpriseServices.DataAccess.EntityFramework/Migrations/20201016134528_add-creditor-ref.Designer.cs
generated
Normal file
125
COA.EnterpriseServices.DataAccess.EntityFramework/Migrations/20201016134528_add-creditor-ref.Designer.cs
generated
Normal file
@ -0,0 +1,125 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using COA.EnterpriseServices.DataAccess.EntityFramework;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.EntityFramework.Migrations
|
||||
{
|
||||
[DbContext(typeof(QuickBaseContext))]
|
||||
[Migration("20201016134528_add-creditor-ref")]
|
||||
partial class addcreditorref
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "3.1.9")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128)
|
||||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
|
||||
|
||||
modelBuilder.Entity("COA.EnterpriseServices.DataAccess.Entities.Client", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
|
||||
|
||||
b.Property<string>("Address")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("City")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<DateTime>("Created")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("FirstName")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("LastName")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<DateTime>("Modified")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<long>("Phone")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<string>("State")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Zip")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Clients");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("COA.EnterpriseServices.DataAccess.Entities.Creditor", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
|
||||
|
||||
b.Property<string>("AccountNumber")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("ClientFirstName")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("ClientLastName")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<DateTime>("Created")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("CurrentCreditorProfileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("Modified")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("OriginalCreditorProfileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("ReferenceNumber")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Status")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Creditors");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("COA.EnterpriseServices.DataAccess.Entities.SettlementAttempt", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
|
||||
|
||||
b.Property<DateTime>("Created")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<DateTime>("Modified")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("SettlementAttempts");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.EntityFramework.Migrations
|
||||
{
|
||||
public partial class addcreditorref : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "ReferenceNumber",
|
||||
table: "Creditors",
|
||||
nullable: true);
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "ReferenceNumber",
|
||||
table: "Creditors");
|
||||
}
|
||||
}
|
||||
}
|
@ -89,6 +89,9 @@ namespace COA.EnterpriseServices.DataAccess.EntityFramework.Migrations
|
||||
b.Property<int>("OriginalCreditorProfileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("ReferenceNumber")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Status")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
|
@ -5,7 +5,10 @@ using System.Linq.Expressions;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess
|
||||
{
|
||||
public class DataAccessManager2
|
||||
/// <summary>
|
||||
/// Provides a wrapper around <see cref="IDataAccess{T}"/> as well as additional convenience CRUD methods
|
||||
/// </summary>
|
||||
public class DataAccessManager
|
||||
{
|
||||
private static readonly IDictionary<string, string[]> enabledDataAccess = new Dictionary<string, string[]>
|
||||
{
|
||||
@ -105,108 +108,4 @@ namespace COA.EnterpriseServices.DataAccess
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides a wrapper around <see cref="IDataAccess{T}"/> as well as additional convenience CRUD methods
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class DataAccessManager<T> where T : class, IRecord
|
||||
{
|
||||
private static readonly IDictionary<string, string[]> enabledDataAccess = new Dictionary<string, string[]>
|
||||
{
|
||||
#region Creditor
|
||||
|
||||
["COA.EnterpriseServices.DataAccess.Entities.Creditor"] = new[]
|
||||
{
|
||||
"COA.EnterpriseServices.DataAccess.QuickBase.QuickBaseDataAccess",
|
||||
"COA.EnterpriseServices.DataAccess.EntityFramework.EntityDataAccess"
|
||||
},
|
||||
|
||||
#endregion
|
||||
|
||||
#region Client
|
||||
|
||||
["COA.EnterpriseServices.DataAccess.Entities.Client"] = new[]
|
||||
{
|
||||
"COA.EnterpriseServices.DataAccess.QuickBase.QuickBaseDataAccess",
|
||||
"COA.EnterpriseServices.DataAccess.EntityFramework.EntityDataAccess"
|
||||
}
|
||||
|
||||
#endregion
|
||||
};
|
||||
|
||||
public bool Add(T item)
|
||||
{
|
||||
return Invoke(d => d.Add(item));
|
||||
}
|
||||
|
||||
public bool Update(T item)
|
||||
{
|
||||
item.Modified = DateTime.Now;
|
||||
|
||||
return Invoke(d => d.Update(item));
|
||||
}
|
||||
|
||||
public bool Update(int id, Action<T> update)
|
||||
{
|
||||
var item = Get(id);
|
||||
|
||||
update(item);
|
||||
|
||||
return Update(item);
|
||||
}
|
||||
|
||||
public T Get(int id)
|
||||
{
|
||||
var item = Invoke(d => d.Get(i => i.Id == id));
|
||||
|
||||
if (item != null && item.Any())
|
||||
{
|
||||
return item.FirstOrDefault();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public ICollection<T> Get(Expression<Func<T, bool>> query)
|
||||
{
|
||||
return Invoke(d => d.Get(query));
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private TResult Invoke<TResult>(Func<IDataAccess<T>, TResult> func)
|
||||
{
|
||||
var results = new List<TResult>();
|
||||
var dataAccessInstances = GetEnabledDataAccess();
|
||||
|
||||
foreach (var dataAccessInstance in dataAccessInstances)
|
||||
{
|
||||
results.Add(func(dataAccessInstance));
|
||||
}
|
||||
|
||||
// if the "EF" version invokes first, return that value
|
||||
return results.FirstOrDefault(r => r != null && !r.Equals(default(TResult)));
|
||||
}
|
||||
|
||||
private ICollection<IDataAccess<T>> GetEnabledDataAccess()
|
||||
{
|
||||
var type = typeof(T);
|
||||
|
||||
// load data access based on active configuration
|
||||
return Dependencies.Container
|
||||
.GetAllInstances<IDataAccess<T>>()
|
||||
.Select(i => new
|
||||
{
|
||||
Instance = i,
|
||||
Type = i.GetType()
|
||||
})
|
||||
.Where(i => enabledDataAccess.ContainsKey(type.FullName) && enabledDataAccess[type.FullName].Any(a => i.Type.FullName.StartsWith(a)))
|
||||
.OrderBy(i => i.Type.Name.StartsWith("QuickBase", StringComparison.OrdinalIgnoreCase))
|
||||
.Select(i => i.Instance)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -15,17 +15,15 @@ namespace COA.EnterpriseServices.DataAccess
|
||||
c.Scan(s =>
|
||||
{
|
||||
s.AddAllTypesOf(typeof(IDataAccess<>));
|
||||
s.AddAllTypesOf(typeof(DataAccessManager<>));
|
||||
|
||||
s.WithDefaultConventions();
|
||||
s.AssembliesFromApplicationBaseDirectory();
|
||||
});
|
||||
|
||||
c.For(typeof(IDataAccess<>)).Singleton();
|
||||
c.For(typeof(DataAccessManager<>)).Singleton();
|
||||
|
||||
c.For<DataAccessManager>().Singleton();
|
||||
c.For<IMapper>().Singleton().Use(() => Mapping.GetMapper());
|
||||
|
||||
c.For<ClientHelper>().Singleton();
|
||||
c.For<CreditorHelper>().Singleton();
|
||||
});
|
||||
|
@ -0,0 +1,25 @@
|
||||
using AutoMapper;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess
|
||||
{
|
||||
public static class AutoMapperExtensions
|
||||
{
|
||||
private static readonly IMapper mapper;
|
||||
|
||||
static AutoMapperExtensions()
|
||||
{
|
||||
mapper = Dependencies.Container.GetInstance<IMapper>();
|
||||
}
|
||||
|
||||
public static T MapTo<T>(this object item)
|
||||
{
|
||||
return mapper.Map<T>(item);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> MapTo<T>(this IEnumerable<object> item)
|
||||
{
|
||||
return mapper.Map<IEnumerable<T>>(item);
|
||||
}
|
||||
}
|
||||
}
|
16
COA.EnterpriseServices.DataAccess/Helpers/BaseHelper.cs
Normal file
16
COA.EnterpriseServices.DataAccess/Helpers/BaseHelper.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using AutoMapper;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
{
|
||||
public class BaseHelper
|
||||
{
|
||||
protected DataAccessManager DataAccess { get; }
|
||||
protected IMapper Mapper { get; }
|
||||
|
||||
public BaseHelper(DataAccessManager dataAccess, IMapper mapper)
|
||||
{
|
||||
DataAccess = dataAccess;
|
||||
Mapper = mapper;
|
||||
}
|
||||
}
|
||||
}
|
@ -3,18 +3,13 @@ using System.Collections.Generic;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
{
|
||||
public class ClientHelper
|
||||
public class ClientHelper : BaseHelper
|
||||
{
|
||||
private readonly DataAccessManager<Entities.Client> clientDataAccess;
|
||||
|
||||
public ClientHelper(DataAccessManager<Entities.Client> clientDataAccess, IMapper mapper)
|
||||
{
|
||||
this.clientDataAccess = clientDataAccess;
|
||||
}
|
||||
public ClientHelper(DataAccessManager dataAccess, IMapper mapper) : base(dataAccess, mapper) { }
|
||||
|
||||
public ICollection<Entities.Client> FindByEmail(string email)
|
||||
{
|
||||
var client = clientDataAccess.Get(c => c.Email.Equals(email));
|
||||
var client = DataAccess.Get<Entities.Client>(c => c.Email.Equals(email));
|
||||
|
||||
return client;
|
||||
}
|
||||
|
@ -1,50 +1,36 @@
|
||||
using AutoMapper;
|
||||
using COA.EnterpriseServices.Creditors;
|
||||
using COA.EnterpriseServices.DataAccess.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
{
|
||||
public class CreditorHelper
|
||||
public class CreditorHelper : BaseHelper
|
||||
{
|
||||
private readonly DataAccessManager<Entities.Creditor> creditorDataAccess;
|
||||
private readonly DataAccessManager<Entities.SettlementAttempt> settlementAttemptDataAccess;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public CreditorHelper(DataAccessManager<Entities.Creditor> creditorDataAccess, DataAccessManager<Entities.SettlementAttempt> settlementAttemptDataAccess, IMapper mapper)
|
||||
public CreditorHelper(DataAccessManager dataAccess, IMapper mapper) : base(dataAccess, mapper) { }
|
||||
|
||||
public Creditor GetCreditor(int id)
|
||||
{
|
||||
this.creditorDataAccess = creditorDataAccess;
|
||||
this.settlementAttemptDataAccess = settlementAttemptDataAccess;
|
||||
this.mapper = mapper;
|
||||
return DataAccess
|
||||
.Get<Entities.Creditor>(id)
|
||||
.MapTo<Creditor>();
|
||||
}
|
||||
|
||||
public Creditors.Creditor GetCreditor(int id)
|
||||
public SettlementAttempt GetSettlementAttempt(int id)
|
||||
{
|
||||
var creditor = creditorDataAccess.Get(id);
|
||||
|
||||
// map EF => domain type
|
||||
return mapper.Map<Creditors.Creditor>(creditor);
|
||||
return DataAccess
|
||||
.Get<Entities.SettlementAttempt>(id)
|
||||
.MapTo<SettlementAttempt>();
|
||||
}
|
||||
|
||||
public Creditors.SettlementAttempt GetSettlementAttempt(int id)
|
||||
public ICollection<SettlementAttempt> GetSettlements(SettlementAttemptFilter filter)
|
||||
{
|
||||
var attempt = settlementAttemptDataAccess.Get(id);
|
||||
|
||||
return mapper.Map<Creditors.SettlementAttempt>(attempt);
|
||||
}
|
||||
|
||||
public ICollection<Creditors.SettlementAttempt> GetSettlements(SettlementAttemptFilter filter)
|
||||
{
|
||||
// we may use this non-generic throughout instead
|
||||
var dataAccess = new DataAccessManager2();
|
||||
|
||||
// More info on "FromSqlRaw" => https://docs.microsoft.com/en-us/ef/core/querying/raw-sql
|
||||
var attempts = dataAccess.Raw<Entities.SettlementAttempt>("uspCreditorPortal_SettlementAttemptGet @SettlementAttemptID = {0}, @CreditorProfileID = {1}", filter.SettlementAttemptId, filter.CreditorProfileId);
|
||||
|
||||
// TODO: write extension to map list of T to destination type
|
||||
return attempts
|
||||
.Select(a => mapper.Map<Creditors.SettlementAttempt>(a))
|
||||
return DataAccess
|
||||
.Raw<Entities.SettlementAttempt>("uspCreditorPortal_SettlementAttemptGet @SettlementAttemptID = {0}, @CreditorProfileID = {1}", filter.SettlementAttemptId, filter.CreditorProfileId)
|
||||
.MapTo<SettlementAttempt>()
|
||||
.ToList();
|
||||
|
||||
//return DBWrapper.ExecuteProcedure(QuickbaseConnectionString, "uspCreditorPortal_SettlementAttemptGet", cmd =>
|
||||
@ -78,41 +64,41 @@ namespace COA.EnterpriseServices.DataAccess.Helpers
|
||||
//});
|
||||
}
|
||||
|
||||
public bool AddCreditor(Creditors.Creditor creditor)
|
||||
public bool AddCreditor(Creditor creditor)
|
||||
{
|
||||
var creditorEntity = mapper.Map<Entities.Creditor>(creditor);
|
||||
|
||||
return creditorDataAccess.Add(creditorEntity);
|
||||
return DataAccess.Add(creditorEntity);
|
||||
}
|
||||
|
||||
public bool UpdateCreditor(Creditors.Creditor creditor)
|
||||
public bool UpdateCreditor(Creditor creditor)
|
||||
{
|
||||
var creditorEntity = mapper.Map<Entities.Creditor>(creditor);
|
||||
|
||||
return creditorDataAccess.Update(creditorEntity);
|
||||
return DataAccess.Update(creditorEntity);
|
||||
}
|
||||
|
||||
public bool SetCreditorStatus(int creditorId, string status)
|
||||
{
|
||||
return creditorDataAccess.Update(creditorId, c => c.Status = status);
|
||||
return DataAccess.Update<Entities.Creditor>(creditorId, c => c.Status = status);
|
||||
}
|
||||
|
||||
public void SetOriginalCreditorAsPrimary(int creditorId)
|
||||
{
|
||||
var creditor = creditorDataAccess.Get(creditorId);
|
||||
var creditor = DataAccess.Get<Entities.Creditor>(creditorId);
|
||||
|
||||
creditor.ReferenceNumber = string.Empty;
|
||||
|
||||
creditorDataAccess.Update(creditor);
|
||||
DataAccess.Update(creditor);
|
||||
}
|
||||
|
||||
public void ClearCreditorReferenceNumber(int creditorId)
|
||||
{
|
||||
var creditor = creditorDataAccess.Get(creditorId);
|
||||
var creditor = DataAccess.Get<Entities.Creditor>(creditorId);
|
||||
|
||||
creditor.CurrentCreditorProfileId = creditor.OriginalCreditorProfileId;
|
||||
|
||||
creditorDataAccess.Update(creditor);
|
||||
DataAccess.Update(creditor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ namespace COA.EnterpriseServices.DataAccess
|
||||
#region EF => Domain
|
||||
|
||||
CreateMap<Entities.Creditor, Creditor>()
|
||||
.ForMember(d => d.creditorId, o => o.MapFrom(s => s.Id))
|
||||
.ForMember(d => d.creditorStatus, o => o.MapFrom(s => s.Status));
|
||||
|
||||
CreateMap<Entities.Client, Client.Client>();
|
||||
@ -21,6 +22,7 @@ namespace COA.EnterpriseServices.DataAccess
|
||||
#region Domain => EF
|
||||
|
||||
CreateMap<Creditor, Entities.Creditor>()
|
||||
.ForMember(d => d.Id, o => o.MapFrom(s => s.creditorId))
|
||||
.ForMember(d => d.Status, o => o.MapFrom(s => s.creditorStatus));
|
||||
|
||||
CreateMap<Client.Client, Entities.Client>();
|
||||
|
@ -13,6 +13,8 @@ namespace COA.EnterpriseServices.Sandbox
|
||||
//Console.WriteLine(creditorController.GetCreditorStatus(1));
|
||||
Console.WriteLine(creditorController.SetCreditorStatus(1, "Active"));
|
||||
|
||||
var creditor = creditorController.GetCreditor(1);
|
||||
|
||||
//creditorController.AddOfferResponse(new OfferResponse
|
||||
//{
|
||||
// CreditorId = 1
|
||||
|
Loading…
x
Reference in New Issue
Block a user