0

My current code looks like the following:

 var cacheWork = new UnitOfWork();
 Log.Info("Caching Cable Entity");
 cacheWork.GetRepository<CableEntity>().All();
 Log.Info("Caching CablePart Entity");
 cacheWork.GetRepository<CablePartEntity>().All();
 Log.Info("Caching CableType Entity");
 cacheWork.GetRepository<CableTypeEntity>().All();
 Log.Info("Caching Country Entity");
 cacheWork.GetRepository<CountryEntity>().All();
 Log.Info("Caching Filesystem Entity");
 cacheWork.GetRepository<FilesystemEntity>().All();
 Log.Info("Caching Host Entity");
 cacheWork.GetRepository<HostEntity>().All();
 Log.Info("Caching Location Entity");
 cacheWork.GetRepository<LocationEntity>().All();
 Log.Info("Caching NodeCable Entity");
 cacheWork.GetRepository<NodeCableEntity>().All();
 Log.Info("Caching Node Entity");
 cacheWork.GetRepository<NodeEntity>().All();
 Log.Info("Caching Person Entity");
 cacheWork.GetRepository<PersonEntity>().All();
 Log.Info("Caching Tier Entity");
 cacheWork.GetRepository<TierEntity>().All();
 Log.Info("Caching User Entity");
 cacheWork.GetRepository<UserEntity>().All();
 cacheWork.Dispose();

What i want is to get rid of the explicit declaring of type for each of these classes. I am able to retrieve all of the types and return it as a list.

What i now want is to do something like this:

var ns = typeof(T).Namespace;
var types = typeof(T).Assembly.GetExportedTypes()
.Where(t => t.Namespace == ns);

var cacheWork = new UnitOfWork();
foreach (Type t in types)
    cacheWork.GetRepository<t>().All();
cacheWork.Dispose();

So i already know that that this syntax does not work, But i'm figuring it has to be a way of doing this via for example reflection. Anyone That can help me with this?

EDIT:

From the already answered SO question i came up with the following solution:

            var ns = typeof(BaseEntity).Namespace;
        var types = typeof(BaseEntity).Assembly.GetExportedTypes()
        .Where(t => t.Namespace == ns);

        var cacheWork = new UnitOfWork();
        types.ForEach(x =>
        {
            Log.Info("Caching " + x.Name + "...");
            MethodInfo repoMethod = typeof (UnitOfWork).GetMethod("GetRepository");
            MethodInfo generic = repoMethod.MakeGenericMethod(x);
            var repository = generic.Invoke(cacheWork, null);

            MethodInfo getAllMethod = repository.GetType().GetMethod("All");
            getAllMethod.Invoke(repository, null);

        });
        cacheWork.Dispose();
  • you can use a generic class with a method which gives you a repository of the given type , something like this. public class unitofwork { public RepoType GetRepository(){ return your repo; } } – Prashant Apr 06 '15 at 18:13
  • My GetRepository method is already defined as: public IReadWriteRepository GetRepository() where TEntity : class.. The problem is that i cannot pass in the **t** inside the foreach loop. – Per Arne Andersen Apr 06 '15 at 18:16
  • 1
    Check this post ... may be this might help you a bit. http://stackoverflow.com/questions/2547617/generic-foreach-loop-in-c-sharp – Prashant Apr 06 '15 at 18:20

0 Answers0