我读了一篇关于 使用 Autofac 完全解耦应用程序。但尽管我尽了最大的努力(而且对这一切都非常陌生),我就是无法让 Autofac 凝结起来。
我从 MS Patterns & 转向 Unity。实践企业库,情况好多了。为了让事情对自己来说不必要地困难,我将所有的东西分离到项目中:
- UnityDi(控制台应用程序)
- UnityDi.Contracts(接口)
- UntiyDi.Domain(类)
- UnityDi.Repositories(数据访问)
- UnityDi.Services(通过访问存储库)一个服务层)
我使用 XML 配置来支付 Unity:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="UnityDi.Contracts" />
<assembly name="UnityDi.Domain" />
<assembly name="UnityDi.Services" />
<assembly name="UnityDi.Repositories" />
<namespace name="UnityDi.Contracts" />
<namespace name="UnityDi.Domain" />
<namespace name="UnityDi.Services" />
<namespace name="UnityDi.Repositories" />
<container>
<register type="IUser" mapTo="User"></register>
<register type="IUserService" mapTo="UserService"></register>
<register type="IUserRepository" mapTo="UserRepository"></register>
</container>
</unity>
</configuration>
并将其放入正在运行的应用程序中,不用担心:
private static readonly IUnityContainer Container = new UnityContainer();
...
Container.LoadConfiguration();
但是为了做到这一点,我需要从我的控制台引用所有上述项目 应用程序。
有没有办法让应用程序只引用 UnityDi.Contracts (界面)?然后应用程序就可以很好地、真正地解耦(诚然是用大锤)。
我希望这足以解释,我对此完全陌生,我这样极端是为了促进更好的学习。
I read a really cool blog about using Autofac to completely decouple an application. But try as I might (and being horribly new to all this), I just couldn't get Autofac to gel.
I turned to Unity from the MS Patterns & Practices Enterprise Library and that went a whole lot better. To make things unnecessarily hard for myself, I separated out all my stuff into projects as:
- UnityDi (Console app)
- UnityDi.Contracts (Interfaces)
- UntiyDi.Domain (Classes)
- UnityDi.Repositories (Data Access)
- UnityDi.Services (Access to repository through a service layer)
I used XML configuration to pony up Unity:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="UnityDi.Contracts" />
<assembly name="UnityDi.Domain" />
<assembly name="UnityDi.Services" />
<assembly name="UnityDi.Repositories" />
<namespace name="UnityDi.Contracts" />
<namespace name="UnityDi.Domain" />
<namespace name="UnityDi.Services" />
<namespace name="UnityDi.Repositories" />
<container>
<register type="IUser" mapTo="User"></register>
<register type="IUserService" mapTo="UserService"></register>
<register type="IUserRepository" mapTo="UserRepository"></register>
</container>
</unity>
</configuration>
And got that into a running app, no worries:
private static readonly IUnityContainer Container = new UnityContainer();
...
Container.LoadConfiguration();
BUT in order to do so, I need a reference to all the above projects from my console app.
Is there a way to make the app only ever have a reference to UnityDi.Contracts (the interfaces)? Then the app is well and truly decoupled (admittedly with a sledgehammer).
I hope that is enough of an explanation, I'm totally new to this and I'm being extreme like this to facilitate better learning.
发布评论
评论(1)
我怀疑您需要项目引用的原因是,如果没有它们,当您按 F5 时,VS 不会将程序集复制到您的应用程序 bin 文件夹中。怎么会呢,它无法知道你需要它们!
项目参考是解决问题的最快方法。您可以做的另一件事是添加一个构建后步骤来复制适当的 DLL 以最终位于正确的目录中,以便您可以运行该应用程序。
I suspect the reason it looks like you need project references is that without them, VS won't copy the assemblies into your apps bin folder when you hit F5. How would it, it has no way of knowing you need them!
The project references are the quickest solution to the problem. The other thing you could do is add a post-build step to copy the appropriate DLLs to end up in the right directory so you can run the app.