public virtual IBaseEntityService <TEntity> GetServiceForEntity <TEntity>() where TEntity : class, IBaseEntity, new() { // try to get repo by type from cache dictionary _serviceCache.TryGetValue(typeof(IBaseRepositoryAsync <TEntity>), out var serviceObject); if (serviceObject != null) { // we have it, cat it to correct type and return return((IBaseEntityService <TEntity>)serviceObject); } // get the creation method from factory var repoCreationMethod = _serviceFactory.GetServiceFactoryForEntity <TEntity>(); if (repoCreationMethod == null) { throw new NullReferenceException("No factory found for entity based service! Entity: " + typeof(TEntity).Name); } serviceObject = repoCreationMethod(_uow); // add repo to cache _serviceCache[typeof(IBaseRepositoryAsync <TEntity>)] = serviceObject; // and return repo return((IBaseEntityService <TEntity>)serviceObject); }
public IBaseEntityService <TEntity> GetEntityService <TEntity>() where TEntity : class, IBaseEntity, new() { ServiceCache.TryGetValue(typeof(IBaseEntityService <TEntity>), out var serviceObject); if (serviceObject != null) { return((IBaseEntityService <TEntity>)serviceObject); } //Repo not found in cache, post it var repoCreationMethod = ServiceFactory.GetServiceFactoryForEntity <TEntity>(); if (repoCreationMethod == null) { throw new NullReferenceException("No factory found for entity: " + typeof(TEntity).Name); } serviceObject = repoCreationMethod(Uow); ServiceCache[typeof(IBaseEntityService <TEntity>)] = serviceObject; return((IBaseEntityService <TEntity>)serviceObject); }