简体   繁体   English

Asp.Net MVC分层体系结构中的循环依赖

[英]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. 我是Asp.Net MVC的新手,我想将我的项目拆分为具有1- MVC项目(表示层UI层)2-业务逻辑层BLL的分层体系结构(在这里我想验证数据视图模型并将视图模型转换为数据库模型使用自动映射器,然后希望通过使用自动映射器转换回ui(MVC项目)第3层来返回视图模型。数据访问层具有存储库DB Context等,我只想在业务层中引用。

my confusion is in between business logic layer and MVC Project (UI) layer. 我的困惑是在业务逻辑层和MVC项目(UI)层之间。 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. 我的视图模型类位于MVC项目内的Model文件夹中,而业务逻辑层引用了具有数据库表类的数据访问层。 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. 如果我想添加MVC项目(存在我的视图模型的地方)对业务逻辑层的引用,则会给出循环依赖错误。 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 我的业务逻辑层和数据访问层是库项目,而UI层是MVC项目

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. ViewModels不应成为业务逻辑层的一部分,因为它们仅用于表示。 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,这需要开始业务,需要UI / View来开始(重复...)

UI/View may know about the business layer, but not the other way around. UI / View可能知道业务层,但反过来却不知道。 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. 因此,执行此操作的“正确”方法是首先将数据库信息映射到Plain Old C#对象,您的UI层可能会在业务层中要求该对象。 After that, the UI layer may do its job by converting it to a class that's optimized to display that information, the ViewModel. 之后,UI层可以通过将其转换为经过优化以显示该信息的类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项目中添加其他用于处理逻辑的类。

UI/View can know about Business, Business can know about Data Access, but never the other way around. UI / View可以了解业务,Business可以了解数据访问,但是反之则不行。 That way you won't get circular dependencies and no entangled code. 这样,您将不会得到循环依赖,也不会纠缠代码。

I hope that makes sense to you. 我希望这对您有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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