Let's assume I have a class like
public class DataService
{
public IList<T> All<T>() { ... }
public T Get<T>(int id) { ... }
...
}
I could use it in various ways...
var dataService = new DataService();
var customers = dataService.All<Customer>();
var order = dataService.Get<Order>(1);
... but if I had a bunch of operations with the same T, this would become cumbersome. Then it would be nice to have something like this:
dataService.TypeIs<Order>();
var order2 = dataService.Get(2);
var order2 = dataService.Get(3);
var allOrders = dataService.All();
How would a TypeIs<T>() method look like? I think it had to somehow convert DataService to DataService<T> and set T... Or is this utterly impossible?
Yes, it's possible using a clever proxy:
public class DataService
{
public IList<T> All<T>() { ... }
public T Get<T>(int id) { ... }
...
}
public class DataServiceProxy<T>
{
public DataServiceProxy(DataService ds)
{
this.ds = ds;
}
public IList<T> All()
{
return this.ds.All<T>();
}
public T Get(int id)
{
return this.ds.Get<T>(id);
}
}
The equivalent of your dataService.TypeIs<Order>(); is var dataServiceProxy = new DataServiceProxy<Order>(dataService).
Related
I have the following interface
public interface ICommand<TResult, TModel>
{
Task<TResult> DoWorkAsync(TModel model);
}
Which is implemented by one or more such Command classes:
public class MyCommand1 : ICommand<Response, Model>()
{
public async Task<Response> DoWorkAsync(Model model) {
// do something
}
}
public class MyCommand2 : ICommand<Response, Model>()
{
public async Task<Response> DoWorkAsync(Model model) {
// do something else
}
}
The Respose and Model classes are as follows:
public class Response
{
public bool IsSuccessful {get;set;}
}
public class Model
{
public Guid Id {get;set;}
public string Name {get;set;}
}
Then I have an orchestrator class that has a dependency on an IEnumerable of IEnumerable<ICommand<Response, Model>>
public class MyOrchestrator
{
private readonly IEnumerable<ICommand<Response, Model>> _commands;
public MyOrchestrator(IEnumerable<ICommand<Response, Model>> commands)
{
_commands = commands;
}
public async Task ExecuteAsync(Model model)
{
myCommand1_Response = await _commands
.OfType<MyCommand1>()
.First()
.DoWorkAsync(model);
myCommand2_Response = await _commands
.OfType<MyCommand2>()
.First()
.DoWorkAsync(model);
// other operations
}
}
Now in my test I'm trying to mock the MyOrchestrator class's dependency IEnumerable<ICommand<Response, Model>> for each of the MyCommand types. How can I achieve this?
The problem is that MyCommand2 and MyCommand1 are concrete classes. You'll either need to make them be IMyCommand1 and IMyCommand2 or make DoWorkAsync virtual.
I think you could simplify your orchestator which would make mocking trivial.
public class MyOrchestrator
{
...
public MyOrchestrator(ICommand<Response, Model> command1, ICommand<Response, Model> command2)
{
this.command1 = command1 ?? throw...;
this.command2 = command2 ?? throw...;
}
public async Task ExecuteAsync(Model model)
{
myCommand1_Response = await command1.DoWorkAsync(model);
myCommand2_Response = await command1.DoWorkAsync(model);
// other operations
}
}
Now mocking this is nothing special.
var mockCommand1 = new Mock<ICommand<Response, Model>>(MockBehaviour.Strict);
...
var tested = new MyOrchestrator(command1: mockCommand1.Object, ...);
This is my first question here and i'm not very familliar with the C# terminology, so if i get some terms or definitions mixed up i appologize in advance.
I have set up a generic EF data access layer;
public class BaseService<TObject> where TObject : class
{
private DbContext Context;
private static readonly Lazy<BaseService<TObject>> lazy = new Lazy<BaseService<TObject>>(() => new BaseService<TObject>());
public static BaseService<TObject> Instance => lazy.Value;
public BaseService()
{
Context = new evEntities();
}
public BaseService(DbContext context)
{
Context = context;
}
public ICollection<TObject> GetAll()
{
return Context.Set<TObject>().ToList();
}
public async Task<ICollection<TObject>> GetAllAsync()
{
return await Context.Set<TObject>().ToListAsync();
}
public TObject Get(int id)
{
return Context.Set<TObject>().Find(id);
}
}
Together with this;
public static class DA
{
public static DataAccess.Categories Categories => new DataAccess.Categories();
public static DataAccess.Tags Tags => new DataAccess.Tags();
public static DataAccess.Users Users => new DataAccess.Users();
}
public static class DA<T> where T : class
{
public static BaseService<T> Base => new BaseService<T>();
}
So in my Business Layer i can do this;
public class Categories
{
public Categories() { }
public ICollection<Database.Categories> GetAll()
{
return DA.Categories.GetAll().ToList();
}
public async Task<ICollection<Database.Categories>> GetAllAsync()
{
return await DA.Categories.GetAllAsync();
}
public Database.Categories Get(int id)
{
return DA.Categories.Get(id);
}
}
For clarity. My EF creates classes/entities like 'Database.Categories' and 'Database.Users' which i pass as 'TObject' to my BaseService to get a standard way of pulling data from my database for all my entities.
Now my question. In a similar way i want to create a generic Business Layer. Like;
public class BusinessLogicBase<TModel>
{
public ICollection<TDBModel> GetAll()
{
return null;
}
public async Task<ICollection<TDBModel>> GetAllAsync()
{
return await DA.Categories.GetAllAsync();
}
public TDBModel Get(int id)
{
return DA.Categories.Get(id);
}
}
I want to be able to call the DA with a TObject like Database.Categories but this has to be dynamic, based on the type passed to the BusinessLogicBase. So i want to do something like this (which doesn't work);
private ???? DetermineDatabaseModel()
{
switch(typeof(TModell))
{
case Models.Categories:
return Database.Categories;
case Models.Users:
return Database.Users;
}
}
So i can do this;
public ICollection<TDBModel> GetAll()
{
var databaseModel = DetermineDatabaseModel()
return DA<databaseModel>().GetAll();
}
I hope you understand my question and can help me.
Thnx!
Sorry for the long post, and for all you 9gaggers, here's a potato... No just kidding, this is serious.
Have you tried something like:
public class BusinessLogicBase<TDBModel> where TDBModel : class {
public ICollection<TDBModel> GetAll() {
return DA<TDBModel>.Base.GetAll();
}
}
UPDATE 1:
Maybe it would help You if you try to write it without generics first and then convert it into more general pattern using generics. There are some missteps that are easier to solve without generics.
Method public ICollection<TDBModel> GetAll() can't return ICollection<TDBModel> because type parameter TDBModel is not defined either in class signature or method signature. I makes more sense if you define it like this:
public class BusinessLogicBase<TModel>
{
public ICollection<TModel> GetAll()
{
return null;
}
}
UPDATE 2:
try this simple console app to demonstrate dynamic keyword and watch what is stored in variables categories and users.
UPDATE 3:
Based on fiddle - I've changed IBaseService<dynamic> to dynamic:
public class BL<TModel>
{
// This is what i want to make dynamic.
// how do i create a return type that i can use in DA<>..
private Type testDetermineDatabaseModel()
{
switch(typeof(TModel).Name){
case "Categories":
return typeof(Database.Categories);
case "Users":
return typeof(Database.Users);
}
return null;
}
public ICollection<TModel> testGetAll()
{
var databaseModel = testDetermineDatabaseModel();
// return DA<databaseModel>().Base.GetAll();
return new List<TModel>();
}
// NEW
// I have constructed the following.
private dynamic baseService;
public dynamic DetermineDatabaseModel()
{
switch (typeof(TModel).Name)
{
case "Categories":
return new BaseService<Database.Categories>();
case "Users":
return new BaseService<Database.Users>();
default:
return null;
}
}
private IBaseService<TDbModel> GetBase<TDbModel>() where TDbModel : class
{
return new BaseService<TDbModel>();
}
public ICollection<TModel> GetAll()
{
ICollection<TModel> returnValue = new List<TModel>();
// This works!!!
foreach (var item in GetBase<Database.Categories>().GetAll())
{
returnValue.Add((TModel)(object)item);
}
baseService = DetermineDatabaseModel();
// This doesn't!!! It's the same thing!! :(
foreach (var item in baseService.GetAll())
{
returnValue.Add((TModel)(object)item);
}
return returnValue;
}
}
But remember, this not solve the issue You are facing. That is map 2 generic types.
I've got a base class called DAL_Base for a large project that does most of the SQL lifting.
DAL_Base has fields for SELECT statements, a GetRecords() method, and a virtual FillData(IDataRecord).
public class DAL_Base<T> where T : IDisposable, new() {
private string connStr;
public DAL_Base() {
connStr = ConfigurationManager.ConnectionStrings["CompanyDatabaseConnStr"].ConnectionString;
}
internal string SP_GET { get; set; }
internal SqlConnection m_openConn {
get {
var obj = new SqlConnection(connStr);
obj.Open();
return obj;
}
}
internal virtual T FillDataRecord(IDataRecord record) {
return new T();
}
internal TList<T> Get() {
if (String.IsNullOrEmpty(SP_GET)) {
throw new NotSupportedException(string.Format("Get Procedure does not exist for {0}.", typeof(T)));
}
var list = new TList<T>();
using (var cmd = new SqlCommand(SP_GET, m_openConn)) {
cmd.CommandType = cmd.GetCommandTextType();
using (var r = cmd.ExecuteReader()) {
while (r.Read()) {
list.Add(FillDataRecord(r));
}
}
cmd.Connection.Close();
}
return list;
}
}
There is a lot more, but this should suffice for a single example.
TList is just a List<T> class:
internal class TList<T> : List<T> {
public TList() { }
}
When one of my classes inherits from it, I wanted it to be able to override the base class's FillDataRecord(IDataRecord).
For example, EmployeeDB ** inherits **DAL_BASE.
When I call EmployeeDB.GetEmployeeList(), it uses DAL_BASE to pull the records:
public class EmployeeDB : DAL_Base<Employee> {
private static EmployeeDB one;
static EmployeeDB() {
one = new EmployeeDB() {
SP_GET = "getEmployeeList",
};
}
private EmployeeDB() { }
internal override Employee FillDataRecord(IDataRecord record) {
var item = base.FillDataRecord(record);
item.Emp_Login = record.Str("Emp_Login");
item.Emp_Name = record.Str("Emp_Name");
item.Emp_Email = record.Str("Emp_Email");
item.Emp_Phone = record.Str("Emp_Phone");
item.Emp_Role = record.Str("Emp_Role");
return item;
}
public static EmployeeList GetEmployeeList() {
var list = new EmployeeList();
list.AddRange(one.Get());
return list;
}
}
In the code above, when GetEmployeeList() calls the DAL_Base method Get(), only DAL_Base::FillDataRecord(IDataRecord) is called.
I really need EmployeeDB::FillDataRecord(IDataRecord) to be called, but I can't make DAL_Base::FillDataRecord(IDataRecord) abstract.
What is the way around this?
All I know of right now is to create an EventHandler, which is what I just thought of, so I'm going to work towards that.
If anyone knows of a better route, please chime in!
One solution is to pass a delegate for Derived.FillDataRecord to the base class via the constructor.
public class DAL_Base<T> where T : IDisposable, new() {
private string connStr;
public DAL_Base() {
connStr = ConfigurationManager.ConnectionStrings["CompanyDatabaseConnStr"].ConnectionString;
}
private Func<IDataRecord, T> _fillFunc;
public DAL_Base(Func<IDataRecord, T> fillFunc) : this() {
_fillFunc = fillFunc;
}
// ...
internal TList<T> Get() {
if (String.IsNullOrEmpty(SP_GET)) {
throw new NotSupportedException(string.Format("Get Procedure does not exist for {0}.", typeof(T)));
}
var list = new TList<T>();
using (var cmd = new SqlCommand(SP_GET, m_openConn)) {
cmd.CommandType = cmd.GetCommandTextType();
using (var r = cmd.ExecuteReader()) {
while (r.Read()) {
list.Add(_fullFunc(r));
}
and in the derived class:
public class EmployeeDB : DAL_Base<Employee> {
public EmployeeDB() : base(r => FillDataRecord(r)) { }
private Employee FillDataRecord(IDataRecord record) {
var item = base.FillDataRecord(record);
item.Emp_Login = record.Str("Emp_Login");
item.Emp_Name = record.Str("Emp_Name");
item.Emp_Email = record.Str("Emp_Email");
item.Emp_Phone = record.Str("Emp_Phone");
item.Emp_Role = record.Str("Emp_Role");
return item;
}
}
I would like to just use RegistrationBuilder to create parts.
example:
public interface IModel
{
String Name { get; }
}
public interface IRepository
{
}
class ModelOne : IModel
{
public String Name { get { return "ModelOne"; } }
}
class ModelTwo : IModel
{
public String Name { get { return "ModelTwo"; } }
}
public interface IRepository<TModel> : IRepository where TModel : IModel
{
}
public class Repository<TModel> : IRepository<TModel> where TModel : IModel
{
}
static void Main(String[] args)
{
var builder = new RegistrationBuilder();
builder.ForTypesDerivedFrom<IModel>()
.Export()
.Export<IModel>();
builder.ForTypesDerivedFrom<IRepository>()
.ExportInterfaces();
var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly(), builder);
var container = new CompositionContainer(asmCatalog);
var one = container.GetExportedValue<IRepository<ModelOne>>();
var two = container.GetExportedValue<IRepository<ModelTwo>>();
}
I can not get the results(the one,the two) I want.
because when i use RegistrationBuilder the all type when wrapped in ProjectingType,but GetExportedValue method did not wrap Generic Parameters in ProjectingType.
My temporary solution is to delete the IsAssignableFrom method in ProjectingType. Now I can get the results(the one,the two) I want.
is there any good solution or correction?
I have the following Repository Pattern. Requirement is to “Find All accounts whose owner’s name is Lijo”. So, I need to write a FindAll function. How to write this function?
Constraints are:
1) The client “BankAccountService” should not use classes from 'DBML_Project'.
2) We should NOT use GetAll method to retireve complete list of accounts and then do a filter.
Note: I confronted this problem while working on the question Polymorphism: Is ORM entity a Domain Entity or Data Entity?
CODE
namespace ApplicationService_Bank
{
public class BankAccountService
{
RepositoryLayer.ILijosBankRepository accountRepository = new RepositoryLayer.LijosSimpleBankRepository();
public void FreezeAllAccountsForUser(string userName)
{
//Should not use assembly 'DBML_Project'.
IEnumerable<DomainEntitiesForBank.IBankAccount> accountsForUserWithNameLIJO = null;
//accountsForUserWithNameLIJO = accountRepository.FindAll(p => p.BankUser.Name == "Lijo");
}
}
}
namespace RepositoryLayer
{
public interface ILijosBankRepository
{
List<DomainEntitiesForBank.IBankAccount> GetAll();
IEnumerable<DBML_Project.BankAccount> FindAll(System.Func<DBML_Project.BankAccount, bool> predicate);
void SubmitChanges();
}
public class LijosSimpleBankRepository : ILijosBankRepository
{
private IBankAccountFactory bankFactory = new MySimpleBankAccountFactory();
public System.Data.Linq.DataContext Context
{
get;
set;
}
public virtual List<DomainEntitiesForBank.IBankAccount> GetAll()
{
List<DBML_Project.BankAccount> allItems = Context.GetTable<DBML_Project.BankAccount>().ToList();
List<DomainEntitiesForBank.IBankAccount> bankAccounts = new List<DomainEntitiesForBank.IBankAccount>();
foreach (DBML_Project.BankAccount acc in allItems)
{
DomainEntitiesForBank.IBankAccount theAccount = bankFactory.CreateAccount(acc.AccountType, acc.BankAccountID, acc.Status, acc.OpenedDate, acc.AccountOwnerID);
bankAccounts.Add(theAccount);
}
return bankAccounts;
}
public IEnumerable<DBML_Project.BankAccount> FindAll(System.Func<DBML_Project.BankAccount, bool> predicate)
{
//Where
var results = Context.GetTable<DBML_Project.BankAccount>().Where(predicate);
return results;
}
public virtual void SubmitChanges()
{
Context.SubmitChanges();
}
}
}
READING:
Returning IEnumerable<T> vs. IQueryable<T>
how to design Repository pattern to be easy switch to another ORM later?
A simple approach is to just build the query by hand:
public class SearchCriteria
{
public string Name { get; set; }
// ...more
}
public IEnumerable<Entity> FindAll(SearchCriteria criteria)
{
IQueryable<Entity> entities = _datasource.Entities; // replace with your L2S equivalent
if (criteria.Name != null)
entities = entities.Where(e => e.Name == criteria.Name);
// ...more
return entities;
}
If you don't want to return the generated objects directly, map to something else before you return:
return Map(entities); // IEnumerable<CustomObject> Map(IEnumerable<Entity> entities)