码迷,mamicode.com
首页 > 其他好文 > 详细

ef 多个模块,通过程序集映射entity,指定对应的repository

时间:2014-09-09 18:10:19      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   ar   for   art   div   

在Entity Framework repository下加两个方法:

        public virtual T GetByEntityName(object id, string EntityTypeName)
        {
            dynamic repository = GetEFRepository(EntityTypeName);
            if (repository != null)
                return repository.Entities.Find(id);
            else
                return null;
        }
        
        public virtual dynamic GetRecordRepository(string EntityTypeName)
        {
            dynamic repository = GetEFRepository(EntityTypeName);
            if (repository != null)
            {
                var type = repository.GetType().GetGenericArguments()[0];
                dynamic part = Activator.CreateInstance(type);
                return part;
            }
            else
            {
                return null;
            }
        }
  private object GetEFRepository(string EntityTypeName)
        {

          

            if (_repositoryDictionary == null)
            {
                var typesToRegister = TypeUtil.GetEntityTypes();
                _repositoryDictionary = new Dictionary<string, object>();
                foreach (var type in typesToRegister)
                {
                    object repository = Activator.CreateInstance(typeof(EfRepository<>).MakeGenericType(type), _context);

                    _repositoryDictionary[type.Name] = repository;
                }
            }

            if (_repositoryDictionary.ContainsKey(EntityTypeName))
                return _repositoryDictionary[EntityTypeName];
            else
                return null;
        }

 TypeUtil.cs

 public class TypeUtil
    {
        public static IList<Type> GetEntityTypes()
        {
            var cacheManager = new MemoryCacheManager();
            return cacheManager.Get("ENTITY-TYPE-ASSEMBLY", () =>
            {
                var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
                 .Where(type => (typeof(BaseEntity).IsAssignableFrom(type) && (type.GetConstructor(Type.EmptyTypes) != null)));
                return typesToRegister.ToList();
            });
            
        }
    }

test:

 [Test]
        public void TypeUtilTest()
        {

            var n = _repository.GetRecordRepository("NewsEntity");
            dynamic nm = _repository.GetByEntityName(1, "NewsEntity");
            var title = nm.Title;

            var c = _repository.GetRecordRepository("NewsCommentEntity");
            dynamic cm = _repository.GetByEntityName(1, "NewsCommentEntity");
            var comment = cm.Comment;
            string pass = title + "\n" + comment;
            Assert.Pass(pass);
           
        }

 

ef 多个模块,通过程序集映射entity,指定对应的repository

标签:style   blog   color   os   io   ar   for   art   div   

原文地址:http://www.cnblogs.com/wlwjc/p/3963043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!