COA.EnterpriseServices/COA.EnterpriseServices.DataAccess/IDataAccess.cs

72 lines
2.7 KiB
C#
Raw Normal View History

2020-10-14 00:56:53 +00:00
using System;
using System.Collections.Generic;
2020-10-14 03:10:25 +00:00
using System.Linq;
2020-10-14 00:56:53 +00:00
using System.Linq.Expressions;
2020-10-14 03:10:25 +00:00
using System.Reflection;
2020-10-14 00:56:53 +00:00
namespace COA.EnterpriseServices.DataAccess
{
2020-10-14 01:24:27 +00:00
public interface IDataAccess<T> where T : class, IRecord
2020-10-14 00:56:53 +00:00
{
bool Add(T item);
bool Update(T item);
ICollection<T> Get(int id);
ICollection<T> Get(Expression<Func<T, bool>> query);
}
2020-10-14 03:10:25 +00:00
//public class AggregateDataAccess<T> : IDataAccess<T> where T : class, IRecord
//{
// private static ICollection<Type> dataAccessTypes;
// static AggregateDataAccess() => LoadInstances();
// /// <summary>
// /// Invokes a method for all instances of <typeparamref name="T"/>
// /// </summary>
// /// <typeparam name="TResult"></typeparam>
// /// <param name="func"></param>
// /// <returns></returns>
// protected TResult Invoke<TResult>(Func<IDataAccess<T>, TResult> func)
// {
// var results = new List<TResult>();
// foreach (var dataAccessType in dataAccessTypes)
// {
// var instance = Activator.CreateInstance(dataAccessType) as IDataAccess<T>;
// results.Add(func(instance));
// }
// // if the "EF" version invokes first, return that value
// return results.FirstOrDefault(r => !r.Equals(default(T)));
// }
// private static void LoadInstances()
// {
// if (dataAccessTypes == null)
// {
// var type = typeof(IDataAccess<T>);
// var aggregateType = typeof(AggregateDataAccess<>);
// // load all types except for 1) the interface itself, 2) any interface, and 3) is not implementing AggregateDataAccess<T>
// // NOTE: the "EF" version will load first, allowing for the "QuickBase" version to run last, in a separate thread if desired
// dataAccessTypes = AppDomain.CurrentDomain
// .GetAssemblies()
// .Where(a => a.FullName.StartsWith("COA.EnterpriseServices.DataAccess"))
// .SelectMany(a => a.ExportedTypes)
// //.Where(t => type.IsAssignableFrom(t)/* && !t.IsInterface && !aggregateType.IsAssignableFrom(t)*/)
// //.OrderBy(t => t.Name.StartsWith("QuickBase", StringComparison.OrdinalIgnoreCase))
// .ToList();
// }
// }
// public bool Add(T item) => Invoke(d => d.Add(item));
// public bool Update(T item) => Invoke(d => d.Update(item));
// public ICollection<T> Get(int id) => Invoke(d => d.Get(id));
// public ICollection<T> Get(Expression<Func<T, bool>> query) => Invoke(d => d.Get(query));
//}
2020-10-14 00:56:53 +00:00
}