简体   繁体   中英

Circular Dependencies in Asp.Net MVC Layerd Architecture

I am new in Asp.Net MVC and want to split my project into layerd architecture having 1- MVC Project (presentation layer UI layer) 2- Business Logic Layer BLL (here i want to validate data View Model and convert View Model to Database Model using auto mapper and then want to send back View Model by convert back by using auto mapper to ui (MVC Project) layer 3- Data Access Layer having repositories DB Context etc which i want to referenced only in business layer.

my confusion is in between business logic layer and MVC Project (UI) layer. my view model classes are in Model folder inside MVC project and Business logic layer have reference of Data Access Layer having database table classes. so my view model are not recognized in business logic layer. if i want to add MVC project (where my view model exist) reference to business logic layer it give circular dependencies error. i do lot of search on forums and tutorials but failed to find solution or failed to understand the concept.

my business logic layer and data access layer are library project while UI layer is MVC project

if any body can explain with example by send data view model to business logic layer and receive back view model from business logic layer

Data Access Layer

namespace DAL.Infrastructure.Contract
{

    public interface IBaseRepository<T> : IDisposable where T : class
    {
        IEnumerable<T> GetAll();
        IEnumerable<T> FindIEnumerableByExpression(Expression<Func<T, bool>> predicate);
        T FindFirstOrDefaultByExpression(Expression<Func<T, bool>> predicate);
        T GetById(object Id);
        T Insert(T entity);
        T Delete(T entity);
        void Update(T entity);
        void Save();
    }
}



namespace DAL.Infrastructure
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        public PMSEntities dbContext = null;
        public DbSet<T> dbSet = null;

        public BaseRepository()
        {
            this.dbContext = new PMSEntities();
            dbSet = dbContext.Set<T>();
        }

        public virtual IEnumerable<T> GetAll()
        {
            return dbSet.AsEnumerable<T>();
        }

        public T GetById(object id)
        {
            return this.dbSet.Find(id);
        }

        public IEnumerable<T> FindIEnumerableByExpression(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            IEnumerable<T> query = dbSet.Where(predicate).AsEnumerable();
            return query;
        }

        public T FindFirstOrDefaultByExpression(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            return this.dbSet.FirstOrDefault(predicate);
        }

        public virtual T Insert(T entity)
        {
            return dbSet.Add(entity);
        }

        public virtual T Delete(T entity)
        {
            return dbSet.Remove(entity);
        }

        public virtual void Update(T entity)
        {
            dbContext.Entry(entity).State = System.Data.Entity.EntityState.Modified;
        }

        public void Save()
        {
            dbContext.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    dbContext.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}

i want to use this above class in business logic layer not direct in controller. here is my confusion how to write business logic layer and use auto mapper in business logic layer having error of circular dependencies

ViewModels shouldn't be part of the Business Logic layer, since they are purely for presentation. How you organized it now makes it so that Business NEEDS the UI/View, which needs the Business to get started, which needs the UI/View to get started (repeat...)

UI/View may know about the business layer, but not the other way around. So the 'proper' way to do this is first mapping your database information to a Plain Old C# Object, which your UI layer may ask for through the business layer. After that, the UI layer may do its job by converting it to a class that's optimized to display that information, the ViewModel. That doesn't mean you have to do that inside the controller, you can add other classes inside your UI/View project that take care of the logic.

UI/View can know about Business, Business can know about Data Access, but never the other way around. That way you won't get circular dependencies and no entangled code.

I hope that makes sense to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM