I have two generic classes in the DAL assembly and both are read only repositories.
The lower level model entity repository (MER) interacts with the DBContext (Model First) and the model entities there-in. The higher level common entity repository (CER) has the task of converting the model entities to common entities and exposing the behaviours to the BAL assembly.
I am not wishing to expose the model entities to the BAL, so I have created a mapping in the CER to define what common entity is mapped to the model entity. Using this mapping, I try to create an instance of the MER inside of the CER, but I must provide a generic type, but I am struggling.
Mapping Code
Mapping = new Dictionary<Type, Type>
{
{ typeof(IPostPurcahseOrderProperties), typeof(PostedPurchaseOrder) },
{ typeof(IPostPurchaseOrderReceiptsProperties), typeof(PostedPurchaseOrdersReceipt) },
{ typeof(IPostSaleOrdersProperties), typeof(PostedSaleOrder) },
{ typeof(IPostPlacementsProperties), typeof(PostedPlacement) }
};
Instantiation Code
var M = Mapping[typeof(E)];
var bob = new ModelRepository<M>(new ProteinEntities());
Issue Returned by Compiler
M is a variable but used like a type
Help please!
Blockquote
MER Code (As requested)
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using Protein.Interfaces;
namespace Protein
{
public class ModelRepository<M> : IModelRepositoryReadOnly<M>
where M : class
{
#region "fields"
private ObjectContext objContext;
private ObjectSet<M> objectSet;
private IDictionary<Type, Type> mapping;
#endregion
#region "constructors"
public ModelRepository(IObjectContextAdapter context)
{
objContext = context.ObjectContext;
objectSet = objContext.CreateObjectSet<M>();
}
#endregion
#region "behaviours"
public IQueryable<M> Find(Expression<Func<M, bool>> predicate)
{
return objectSet.Where(predicate);
}
public IEnumerable<M> GetAll()
{
return objectSet.ToList();
}
#endregion
}
}
Test Code (as per first web link in comments)
var typeOfClass = typeof(DoSomething<>);
var typeOfArguement = typeof(Bob);
var constructedClass = typeOfClass.MakeGenericType(typeOfArguement);
var created = Activator.CreateInstance(constructedClass);