Legacy application repository standard - ASP.NET MVC 5

Asked

Viewed 49 times

1

I am participating in a project using ASP.NET MVC 5, I would like opinions to form a concept in which I can achieve good results with a good team productivity.

What I have of interfaces:

  • Irepository
  • Icacherepository
  • Icacheservice
  • Iunitofwork

The implementations of the interfaces:

  • Repository : IRepository
  • Cacherepository: Repository, ICacheRepository
  • Inmemorycache : ICacheService

Repository.cs

    public class Repository<T> : IRepository<T> where T : class
    {
        public MyDatabaseContext context;

        public Repository(MyDatabaseContext context)
        {
            this.context = context;
        }

        public IQueryable<T> All()
        {
            return this.context.Set<T>().AsQueryable<T>();
        }

        public T Find(int id)
        {
            return this.context.Set<T>().Find(id);
        }
    }

CacheRepository.cs

    public class CacheRepository<T> : Repository<T>, ICacheRepository<T> where T : class
    {
        protected readonly ICacheService cacheService;

        public CacheRepository(MyDatabaseContext context, ICacheService cacheService) : base(context)
        {
            this.cacheService = cacheService;
        }

        public IEnumerable<T> All()
        {
            return this.cacheService.GetOrSet<IEnumerable<T>>($"{typeof(T)}", () => base.All());
        }

        public T Find(int id)
        {
            return this.cacheService.GetOrSet<T>($"{typeof(T)}.{id}", () => base.Find(id));
        }
    }

As for the part of Unitofwork:

I have the implementation of Unitofwork centralizing the parts, I used an example of the method of GetCacheRepository<T>() to demonstrate how I will return the repositories already with Context and Cacheservice (in the case of repositories that may be cached).

    public class UnitOfWork<TContext> : IUnitOfWork where TContext : MyDatabaseContext, new()
    {
        private readonly MyDatabaseContext DataContext;
        protected readonly ICacheService cacheService;
        private Dictionary<Type, object> repositories;
        private Dictionary<Type, object> cacheRepositories;

        public UnitOfWork()
        {
            this.DataContext = new TContext();
            this.cacheService = new InMemoryCache();
            this.repositories = new Dictionary<Type, object>();
            this.cacheRepositories = new Dictionary<Type, object>();
        }

        /* Método duplicado para exemplificação */
        public ICacheRepository<T> GetCacheRepository<T>() where T: class
        {
            if (!this.cacheRepositories.Keys.Contains(typeof(T)))
            {
                var repository = new CacheRepository<T>(this.DataContext, this.cacheService);

                this.cacheRepositories.Add(typeof(T), repository);
            }

            return this.cacheRepositories[typeof(T)] as ICacheRepository<T>;
        }

        /* Método duplicado para exemplificação */
        public IRepository<T> GetRepository<T>() where T : class
        {
            if (!this.repositories.Keys.Contains(typeof(T)))
            {
                var repository = new Repository<T>(this.DataContext);

                this.repositories.Add(typeof(T), repository);
            }

            return this.repositories[typeof(T)] as IRepository<T>;
        }
    }

How the service uses:

    public class SampleService
    {
        protected readonly IUnitOfWork uow;
        protected readonly IRepository<MyModel> MyRepository;
        protected readonly ICacheRepository<MyModelEnum> MyCacheRepository;

        public SampleService(IUnitOfWork uow)
        {
            this.uow = uow;

            this.MyRepository = this.uow.GetRepository<MyModel>();
            this.MyCacheRepository = this.uow.GetRepository<MyModelEnum>();
        }

        public MyModel FindById(int id)
        {
           return this.MyRepository.Find(1);
        }

        public IEnumerable<MyModelEnum> GetEnum()
        {
           return this.MyCacheRepository.All();
        }

    }

How Ninject Injects Dependency:

   kernel.Bind<IUnitOfWork>().To<UnitOfWork<MyDatabaseContext>>();

this scenario was designed for discussion with users, there is no right or wrong answer, I want various opinions, thanks!

  • What is your doubt and what the issue has to do with Asp.net-mvc-5?

  • 1

    My question/question and how to use Unitofwork and repository standard and service in ASP.NET MVC 5 applications, I would like to approach and ideas of people who use the similar stack.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.