0

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);
Andrew
  • 59
  • 8
  • Can you please include the source code for `ModelRepository`? – mjwills Sep 05 '17 at 13:10
  • 5
    Possible duplicate of [Pass An Instantiated System.Type as a Type Parameter for a Generic Class](https://stackoverflow.com/questions/266115/pass-an-instantiated-system-type-as-a-type-parameter-for-a-generic-class) – mjwills Sep 05 '17 at 13:13
  • @mjwills There could be another solution. He could have `Mapping = new MyMapType { Map.Create(), Map.Create(), /* ... */ };` or similar. It is clear that once he uses `typeof(X)`, he "loses" the type `X` itself and only has the `System.Type` instance which _represents_ `X`. – Jeppe Stig Nielsen Sep 05 '17 at 13:17
  • Try using reflection. https://stackoverflow.com/questions/67370/dynamically-create-a-generic-type-for-template – Swetha Sep 05 '17 at 13:28
  • Thanks for the response everyone. – Andrew Sep 05 '17 at 13:35
  • I have step through the first post suggested, and I works in so far as I can create the class with the generic type...but it returns an object type and does not give me strong type. Any further suggestions? Thanks again :) – Andrew Sep 05 '17 at 13:36
  • 1
    @Andrew By "strong type", you mean a compile-time type? This is clearly not going to work because you don't know the type until runtime. – Tim Rogers Sep 05 '17 at 13:42
  • Awe, okay Tim. Thanks for that fact. :) – Andrew Sep 05 '17 at 13:43
  • Okay, I think that is this posted done. Thanks everyone :) – Andrew Sep 05 '17 at 13:45

0 Answers0