解决循环引用(C#) [英] Resolving Circular References (C#)

查看:108
本文介绍了解决循环引用(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在循环参考/依赖性方面遇到一些问题.我的思考过程一定有问题,我只是不明白.

这是我的项目:

  Flip.Main(ASP.NET MVC)Flip.Domain(C#DLL)Flip.Services(C#DLL)Flip.Utility(C#DLL) 

当前参考/相关性:

  Flip.Main->Flip.Domain,Flip.Services,Flip.UtilityFlip.Services->Flip.Domain,Flip.UtilityFlip.Domain->翻转实用程序 

我想以这样的方式来构造项目:我的服务项目具有所有服务,我的领域项目是模型,存储库和用于查询模型的流利"扩展,而main和Utility项目几乎可以自我解释./p>

遇到的问题:

1)我的Flip.Services项目中有一个EmailService,它需要发送本地化的电子邮件.所有本地化都在Flip.Main的App_GlobalResources中完成.现在不知道如何将强类型的电子邮件和其他本地化资源作为Flip.Main传递到我的服务层.Main已经依赖于服务层,因此我可以使其依赖于Main项目.

2)我有商务舱,例如CustomerSearchFilter代表强类型搜索查询.我想要那些不在Flip.Domain项目之外的业务类,因为它们不是域模型的一部分.但是,在我的CustomerSearchFilter类中,我具有域类实例(例如CustomerGroup),因此它需要了解域类.同时,我在Flip.Domain项目中的Fluent接口需要知道什么是CustomerSearchFilter,以便可以将其应用于IQueryable接口.再次循环引用.

3)我有一个自定义的[AuthorizeSessionState]属性,该属性用于装饰ASP.NET MVC Flip.Main项目中的特定控制器动作.这是一个ActionFilterAttribute,需要实例化驻留在我的Flip.Services项目中的SessionService.我不能将其放入我的Utility类中(因为Flip.Services已经引用了Flip.Utility).我也不认为他们应该在Flip.Main中-我是否必须为此做另一个项目!?

(还有20个)

我觉得自己在某个地方犯了一个错误,尤其是当我读到其他人通常不会遇到循环引用问题时.帮助吗?

解决方案

对所有非平凡的类使用接口.将接口放置在与实现不同的程序集中.

I'm having a couple of problems with circular reference/dependency that I've been sitting on all day. Something must be wrong with my thought process, I just don't get it.

Here are my projects:

Flip.Main     (ASP.NET MVC)
Flip.Domain   (C# DLL)
Flip.Services (C# DLL)
Flip.Utility  (C# DLL)

Current References/Dependencies:

Flip.Main ->     Flip.Domain, Flip.Services, Flip.Utility
Flip.Services -> Flip.Domain, Flip.Utility
Flip.Domain ->   Flip.Utility

I wanted to structure my project in a way that my services project has all services, my domain project the model, repository and 'fluent' extensions to query the model, and the main and utility project are pretty much self explanatory.

Problems encountered:

1) I have an EmailService in my Flip.Services project, which needs to send out localized emails. All localization is done in Flip.Main's App_GlobalResources. No idea how to get the strongly typed emails and other localized resources now to my service layer as Flip.Main already depends on the service layer and therefore I can have it depend back to the Main project.

2) I have business classes, e.g. CustomerSearchFilter which represents a strongly typed search query. I want those business classes outside of the Flip.Domain project because they are not part of the domain model. However, in my CustomerSearchFilter class I have domain class instances (e.g. CustomerGroup) so it needs to know about domain classes. At the same time my Fluent interface in my Flip.Domain project needs to know what CustomerSearchFilter is so I can apply it to my IQueryable interface. Circular reference again.

3) I have a custom [AuthorizeSessionState] attribute which I use to decorate specific controller actions in my ASP.NET MVC Flip.Main project. This is an ActionFilterAttribute which needs to instantiate my SessionService who resides in my Flip.Services project. I can't put this into my Utility class though (because Flip.Services already references Flip.Utility). I don't think they should be in Flip.Main either - do I have to make another project for this!?

(20 more)

I feel like I'm making a mistake somewhere down the line, especially when I read that others usually don't encounter circular reference problems. Help?

解决方案

Use interfaces for all non-trivial classes. Place interfaces in a different assembly from implementation.

这篇关于解决循环引用(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆