StructureMap配置DI与asp.net MVC / LINQ2SQL组合? [英] StructureMap configuration for DI with asp.net MVC / Linq2Sql combo?

查看:163
本文介绍了StructureMap配置DI与asp.net MVC / LINQ2SQL组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的不完整StructureMap配置:

Here is my incomplete StructureMap configuration:

PS:大的道歉要求你写我的应用程序为我,但我发现了StructureMap API是一个有点混乱,近到旧的API的一切,我发现,当我进行Google搜索是指

PS: Big apologies for asking you to write my app for me but I'm finding the StructureMap api to be a bit confusing as nearly everything I find when I do google searches refers to the older api.

    public static void Configure(IContainer container)
    {
        container.Configure(c =>
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
            SQLDataContext dataContext = new SQLDataContext(connectionString);

            c.For<SQLDataContext>().HttpContextScoped();

            c.For<IAdminRepository>().Use<SQLAdminRepository_v2>().Ctor<SQLDataContext>().Is(dataContext);
            c.For<IMemberRepository>().Use<SQLMemberRepository_v2>().Ctor<SQLDataContext>().Is(dataContext);
            c.For<IUtilityRepository>().Use<SQLUtilityRepository_v2>().Ctor<SQLDataContext>().Is(dataContext);

            c.For<IAdminService>().Use<AdminService_v2>();
            c.For<IMemberService>().Use<MemberService_v2>();
            c.For<IUtilityService>().Use<UtilityService_v2>();

            c.For<ResourcePool>().Singleton();

        });
    }

每个服务都有其上通过其构造函数传递相应的信息库的依赖。例如

Each Service has a dependency on the corresponding Repository which is passed in via its constructor. e.g. :

public class ContactService_v2 : IContactService
{
    IContactRepository contactRepository;

    public ContactService_v2(IContactRepository contactRepository)
    {
        this.contactRepository = contactRepository;
    }

    public IQueryable<Contact> Get_Contacts()
    {
        return contactRepository.GetContacts();
    }

    public void Save_Contact(Contact contact)
    {
        contactRepository.Save_Contact(contact);
    }
}

所以服务层是刚刚与实际存储库躺在它背后门面。 [和之前你问或建议 - 不,我不会改变这种着急,因为最初我只是想获得DI努力使code可测试开始之前,我做任何事情戏剧性]

So the Service layer is just facade with the real Repository lying behind it. [and before you ask or suggest - No I won't be changing that in a hurry because initially I just want to get the DI working to make the code testable before I start doing anything dramatic.]

Q1:应用程序使用LINQ2SQL的数据访问。重要的是,所有的服务发送到每个控制器有,在其储存库依赖于相同的数据上下文实例的依赖关系,因为它们可能需要加入到彼此在整个库制成复杂的查询。因此,我想用一个HttpContext的生命周期的数据上下文。这是(上图)我该怎么办?

Q1: The application uses Linq2Sql data access. It is essential that all the Services sent to each controller have, in their Repository dependency a dependency on the same data context instance, because they may need to join to each other in complex queries made across Repositories. Therefore I want to use a HttpContext lifecycle for the data context. Is this (above) how I do that?

Q2:本ResourcePool用于跟踪一个单身的模式,但我有它重构到普通班采取单个服务作为其构造函数的参数。是对的吗?在这种情况下,它的行为就好像它从asp.net应用程序缓存来了?

Q2: The ResourcePool used to follow a singleton pattern but I have refactored it to a ordinary class taking a single service as its constructor parameter. Is that right? In this case, will it behave as if it came from the asp.net application cache?

Q3:什么是自动注册等效存储库和服务配置? [PS:是的,我知道,_v2后缀是不同寻常,但也存在各自的帐户Xservice和XRepository遗留当量,我希望在我的闲暇已经加入了DI后重构路程,工作无缝。

Q3: What are the auto-registration equivalents for the Repository and Service configurations? [PS: Yes I know that the "_v2" suffix is unconventional but there already exist legacy equivalents of each XService and XRepository which I wish to refactor away at my leisure after the DI has been added and is working seamlessly].

Asp.Net MVC允许一个人有很强的模型,像这样绑定:

Asp.Net MVC allows one to have strong model binding like so :

[HttpPost]
public ActionResult List(AdminDisplay admin)
{
    admin.GetAdminPage();
    return View(admin);
}

在这种情况下,每一个类(如AdminDisplay)需要一个默认参数的构造函数,这样的MVC框架可以创建它。而且,这种模式势必类中创建的任何类也需要这样的参数的构造函数。

In such a case every class (such as AdminDisplay) requires a default parameterless constructor so that the MVC framework can create it. Furthermore any class created inside such a model bound class also needs such a parameterless constructor.

Q4:如果我要替换这些参数构造函数,是不是足以刚才添加的条目他们在StructurMap配置code? - 或者我不需要做的前提是所有这些模型绑定类及其家属有哪些contructors只使用参数什么StructureMap能够解析

Q4: If I want to replace these parameterless constructors, is it sufficient to just add the entries for them in the StructurMap configuration code? - or do I not need to do that provided that all these model binding classes and their dependents have contructors which only use parameters what StructureMap is able to resolve.

有关的不良格式道歉以上,但我怎么关闭所见即所得的编辑?

Apologies for the bad formatting above but how do I turn off wysiwyg editing?

PS:这是允许的提供答案的问题Q1,Q2,Q3,Q4的没有回答所有的人

PS: It is permissible to provide answer any of the questions Q1, Q2, Q3, Q4 without answering all of them!

推荐答案

Q1:
更改code从数据上下文:

Q1: Change the code for the data context from:

SQLDataContext dataContext = new SQLDataContext(connectionString);
c.For<SQLDataContext>().HttpContextScoped();

c.For<SQLDataContext>().HybridHttpOrThreadLocalScoped()
.Use(() => new SQLDataContext(connectionString));

更改code从每个存储库:

Change the code for each repository from:

c.For<IAdminRepository>().Use<SQLAdminRepository_v2>()
.Ctor<SQLDataContext>().Is(dataContext);

c.For<IAdminRepository>().Use<SQLAdminRepository_v2>()
.Ctor<SQLDataContext>();

这篇关于StructureMap配置DI与asp.net MVC / LINQ2SQL组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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