public IEnumerable <T> GetAll(Func <T, bool> where) { var sql = _sqlRepository.GetAll(where).ToList(); IEnumerable <T> entities = new List <T>(); if (GetMongo <T> .IsMongo()) { var mongo = _mongoRepository.GetAll().AsQueryable(); IEnumerable <T> mongoToSql = new List <T>(); foreach (var item in mongo) { var entity = new MongoToSql().GetEntityFromMongo <T, TD>(item); IEnumerable <T> listEntity = (entity != null) ? new List <T>() { entity } : new List <T>(); mongoToSql = mongoToSql.AsQueryable().Union(listEntity); } entities = entities.Union(mongoToSql); } var mongoFuc = (entities.Any())?GetFunc(where, entities):entities; sql.AddRange(mongoFuc); _logging.Add(new Logging(DateTime.UtcNow, "Get all entities by function", typeof(T).Name, "")); return(sql); }
public QueryResult <T> GetAll(Query <T> query, bool isWithDeleted = false) { var sql = _sqlRepository.GetAll(query, isWithDeleted); IEnumerable <T> entities = new List <T>(); if (GetMongo <T> .IsMongo()) { var mongo = (isWithDeleted) ? _mongoRepository.GetAll().AsQueryable() : _mongoRepository.GetAll().Where(x => !x.IsDeleted).AsQueryable(); IEnumerable <T> mongoToSql = new List <T>(); foreach (var item in mongo) { var entity = new MongoToSql().GetEntityFromMongo <T, TD>(item); IEnumerable <T> listEntity = (entity != null) ? new List <T>() { entity } : new List <T>(); mongoToSql = mongoToSql.Union(listEntity); } entities = entities.Union(mongoToSql); } var mongoQuery = GetQuery(query, entities); sql.List = sql.List.Union(mongoQuery.List); sql.Count += mongoQuery.Count; _logging.Add(new Logging(DateTime.UtcNow, "Get all entities by query: " + query, typeof(T).Name, "")); return(sql); }
public IRepository <T> Repository <T>() where T : class, IEntityBase, new() { if (repositories == null) { repositories = new Dictionary <string, object>(); } var type = typeof(T).Name; if (!repositories.ContainsKey(type)) { var mongoType = (GetMongo <T> .IsMongo()) ? GetMongo <T> .GetMongoType() : new MongoBase(); var repositoryType = typeof(CommonRepository <,>); var repositoryInstance = new Lazy <IRepository <T> >( () => (IRepository <T>) Activator.CreateInstance(repositoryType.MakeGenericType(typeof(T), mongoType.GetType()), _context)).Value; repositories.Add(type, repositoryInstance); } return((IRepository <T>)repositories[type]); }
public IEnumerable <T> FindBy(Expression <Func <T, bool> > predicate) { var entities = _sqlRepository.FindBy(predicate).ToList(); if (GetMongo <T> .IsMongo() && !(new T() is Order) && !(new T() is OrderDetail)) { var mongo = _mongoRepository.FindBy(predicate); entities.AddRange(mongo); } return(entities); }
public IEnumerable <T> GetAll() { var entities = _sqlRepository.GetAll().ToList(); if (GetMongo <T> .IsMongo() && !(new T() is Order) && !(new T() is OrderDetail)) { var mongo = _mongoRepository.GetAll(); entities.AddRange(Mapper.Map <IEnumerable <T> >(mongo)); } _logging.Add(new Logging(DateTime.UtcNow, "Get all entities", typeof(T).Name, "")); return(entities); }
public void Add(T entity) { if (GetMongo <T> .IsMongo()) { if (entity is Game && _mongoRepository.GetSingle((entity as Game).Key) != null) { _mongoRepository.MakeOutdated((entity as Game).Key); } else if (_mongoRepository.GetSingle(entity.Id.AsObjectId().ToString()) != null) { _mongoRepository.MakeOutdated(entity.Id.AsObjectId().ToString()); } } _sqlRepository.Add(entity); _logging.Add(new Logging(DateTime.UtcNow, "Add entity from Sql", typeof(T).Name, MongoRepository <TD> .GetVersion(entity))); }
public int GetCount(Func <T, bool> where, bool isWithDeleted) { var sqlCount = _sqlRepository.GetCount(where, isWithDeleted); if (GetMongo <T> .IsMongo()) { var mongo = (isWithDeleted) ? _mongoRepository.GetAll().AsQueryable() : _mongoRepository.GetAll().Where(x => !x.IsDeleted).AsQueryable(); var mongoCount = 0; foreach (var item in mongo) { var mongoEntity = new MongoToSql().GetEntityFromMongo <T, TD>(item); mongoCount += (mongoEntity != null && where (mongoEntity)) ? 1 : 0; } sqlCount += mongoCount; } return(sqlCount); }
public T GetSingle(string id) { if (id.Length == Guid.Empty.ToString().Length) { var entity = _sqlRepository.GetSingle(id); if (entity != null) { _logging.Add(new Logging(DateTime.UtcNow, "Get single entity from sql", typeof(T).Name, MongoRepository <TD> .GetVersion(entity))); return(entity); } } if (GetMongo <T> .IsMongo()) { var mongo = _mongoRepository.GetSingle(id); return(Mapper.Map <T>(mongo)); } return(null); }
public void Edit(T entity) { var model = entity; if (GetMongo <T> .IsMongo() && _sqlRepository.FindBy(x => x.Id == entity.Id).FirstOrDefault() == null) { _logging.Add(new Logging(DateTime.UtcNow, "Editing entities in mongo", typeof(T).Name, "old version:" + MongoRepository <TD> .GetVersion(GetSingle(model.Id.AsObjectId().ToString())) + " ; new version: " + MongoRepository <TD> .GetVersion(entity))); _mongoRepository.MakeOutdated(entity.Id.AsObjectId().ToString()); _sqlRepository.Add(model); } else { _logging.Add(new Logging(DateTime.UtcNow, "Editing entity in sql", typeof(T).Name, "old version:" + MongoRepository <TD> .GetVersion(GetSingle(model.Id.ToString())) + " ; new version: " + MongoRepository <TD> .GetVersion(entity))); _sqlRepository.Edit(model); } }