首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环引用+ IoC

循环引用+ IoC
EN

Stack Overflow用户
提问于 2014-01-14 06:01:01
回答 1查看 166关注 0票数 0

我有这样的情况:

网站项目-使用Unity3 IoC调用业务类。Web项目看不到业务项目。它只是引用了Contracts Project。

代码语言:javascript
复制
namespace Biblioteca.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();

            var Autor = Inject.Resolve<IAutorBO>();
        }
    }
}

业务项目-在这里,我使用Unity3 IoC指向数据项目中的AutorDO类(见下文)。业务项目看不到数据项目。

代码语言:javascript
复制
namespace Biblioteca.Data
{
    public sealed partial class AutorBO : IAutorBO
    {                
        #region Atributos

        private IAutorDO AutorDO = Inject.Resolve<IAutorDO>();

        #endregion

        #region Métodos Interface

        public IQueryable<DTOAutor> GetAll()
        {
            return AutorDO.GetAll();
        }

        public DTOAutor GetById(int id)
        {
            return AutorDO.GetById(id);
        }

        void IAutorBO.Insert(DTOAutor dto)
        {
            AutorDO.Insert(dto);
        }

        void IAutorBO.Delete(DTOAutor dto)
        {
            AutorDO.Delete(dto);
        }

        void IAutorBO.Update(DTOAutor dto)
        {
            AutorDO.Update(dto);
        }

        //IQueryable<DTOAutor> IAutorBO.SearchFor(Expression<Func<Autor, bool>> predicate)
        //{
        //    return AutorDO.SearchFor(predicate);
        //}

        IQueryable<DTOAutor> IAutorBO.GetAll()
        {
            return AutorDO.GetAll();
        }

        DTOAutor IAutorBO.GetById(int id)
        {
            return AutorDO.GetById(id);
        }

        #endregion

        #region Outros Métodos

        #endregion
    }
}

数据访问项目-这是我的数据项目。

代码语言:javascript
复制
namespace Biblioteca.Data
{
    public sealed partial class AutorDO : IAutorDO
    {
        #region Atributos

        Repository<Autor> repository = new Repository<Autor>();

        #endregion

        #region Implementações Interface

        /// <summary>
        /// Implementação do método de interface Insert
        /// </summary>
        /// <param name="dto"></param>
        void IAutorDO.Insert(Transport.DTOAutor dto)
        {
            Autor entity = AssemblerAutor.ToEntity(dto);
            repository.Insert(entity);
        }

        /// <summary>
        /// Implementação do método de interface Delete
        /// </summary>
        /// <param name="dto"></param>
        void IAutorDO.Delete(Transport.DTOAutor dto)
        {
            Autor entity = AssemblerAutor.ToEntity(dto);
            repository.Delete(entity);
        }

        /// <summary>
        /// Implementação do método de interface Update
        /// </summary>
        /// <param name="dto"></param>
        void IAutorDO.Update(Transport.DTOAutor dto)
        {
            Autor entity = AssemblerAutor.ToEntity(dto);
            repository.Update(entity);
        }

        /// <summary>
        /// Implementação do método de interface SearchFor
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        //IQueryable<Transport.DTOAutor> IAutorDO.SearchFor(Expression<Func<Autor, bool>> predicate)
        //{
        //    IQueryable<Autor> list = repository.SearchFor(predicate);
        //    IQueryable<Transport.DTOAutor> dtoList = (IQueryable<Transport.DTOAutor>)AssemblerAutor.ToDTOs(list);
        //    return dtoList;
        //}

        /// <summary>
        /// Implementação do método de interface GetAll
        /// </summary>
        /// <returns></returns>
        IQueryable<Transport.DTOAutor> IAutorDO.GetAll()
        {
            IQueryable<Autor> list = repository.GetAll();
            IQueryable<Transport.DTOAutor> dtoList = (IQueryable<Transport.DTOAutor>)AssemblerAutor.ToDTOs(list);
            return dtoList;
        }

        /// <summary>
        /// Implementação do método de interface GetById
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Transport.DTOAutor IAutorDO.GetById(int id)
        {
            Autor entity = new Autor();
            Transport.DTOAutor dto = new Transport.DTOAutor();

            using (var ctx = new BibliotecaContext())
            {
                entity = repository.GetById(id);
                dto = AssemblerAutor.ToDTO(entity);
            }

            return dto;
        }

        #endregion
    }
}

业务和数据项目都引用了合同项目,该项目具有用于实现IoC的所有Unity3 IoC接口。以下是用于实现IoC的接口:

代码语言:javascript
复制
namespace Biblioteca.Contracts
{
    public interface IAutorBO
    {
        #region Métodos CRUD

        void Insert(DTOAutor dto);
        void Delete(DTOAutor dto);
        void Update(DTOAutor dto);
        //IQueryable<DTOAutor> SearchFor(Expression<Func<Autor, bool>> predicate);
        IQueryable<DTOAutor> GetAll();
        DTOAutor GetById(int id);

        #endregion
    }
}

namespace Biblioteca.Contracts
{
    public interface IAutorDO
    {
        #region Métodos CRUD

        void Insert(DTOAutor dto);
        void Delete(DTOAutor dto);
        void Update(DTOAutor dto);
        //IQueryable<DTOAutor> SearchFor(Expression<Func<Autor, bool>> predicate);
        IQueryable<DTOAutor> GetAll();
        DTOAutor GetById(int id);

        #endregion
    }
}

作为补充,我使用了一个通用存储库,如下所示:

代码语言:javascript
复制
namespace Biblioteca.Data
{
    /// <summary>
    /// Interface para classe genérica para métodos CRUD
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IRepository<T>
    {
        void Insert(T entity);
        void Delete(T entity);
        void Update(T entity);
        IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
        IQueryable<T> GetAll();
        T GetById(int id);
    }
}

namespace Biblioteca.Data
{
    /// <summary>
    /// Classe genérica para métodos CRUD da camada Data
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Repository<T> : IRepository<T> where T : class 
    {
        #region Attributes

        protected DbSet<T> dbset;
        protected DbContext ctx;

        #endregion

        #region Constructor

        public Repository()
        { }

        public Repository(DbContext ctx)
        {
            this.ctx = ctx;
            dbset = ctx.Set<T>();
        }

        #endregion

        #region IRepository<T> Members

        public void Insert(T entity)
        {
            if (dbset.Contains(entity))
            {
                Update(entity);
            }
            else
            {
                dbset.Add(entity);
            }
        }

        public void Delete(T entity)
        {
            dbset.Remove(entity);
        }

        public void Update(T entity)
        {
            using (ctx)
            {
                ctx.Set<T>().Attach(entity);
                ctx.SaveChanges();
            }
        }

        public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
        {
            return dbset.Where(predicate);
        }

        public IQueryable<T> GetAll()
        {
            return dbset;
        }

        public T GetById(int id)
        {
            return dbset.Find(id);
        }

        #endregion
    }
}

请注意,我在所有类中都有一个注释方法。此方法无法实现,因为它将导致循环依赖。

我有一个引用合同项目的数据项目。但我不能使用"SearchFor“方法,因为它需要数据项目中的实体Autor。

请注意,Business和Data都需要看到实体类,因为我有相同的方法签名。

我需要一些方法来允许以它的方式使用IoC,其中Web not引用业务和业务而不是引用数据,并且能够创建其他方法,其中我可以将实体作为参数传递。

有什么建议吗?我已经尝试创建第三个项目,并指向它,但我无法使其工作。可以使用反射吗?如果可能,如何实现?

如果有任何建议我将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2014-02-28 08:06:10

如果你有一个循环依赖,这是一个线索,你应该修改你的架构。

如果您需要“让数据项目引用合同项目”,那么合同不能引用数据来获取实体。你说“业务和数据需要看到实体类”,那么为什么不把你的实体Autor放在一个实体项目中呢?

Data ->引用Contract ->引用实体Contract ->引用实体

或者,也可以在合同项目中定义实体,而不是定义数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21102249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档