将StructureMap组件注册转换为Autofac版本

时间:2017-02-18 18:55:46

标签: .net asp.net-mvc nhibernate autofac structuremap

我是Autofac的新手,并且有我想要翻译的以下StructureMap组件注册,但我担心我的实现可能不会以推荐的方式完成。 Autofac非常强调您不应该在生命周期范围之外解析(即container.Resolve),但当然container.BeginLifetimeScope()在构建ContainerBuilder之前不可用。 For<ISessionSource>().Singleton().Use<NHibernateSessionSource>(); For<ISession>().Use(ctx => { var unitOfWork = (INHibernateUnitOfWork)ctx.GetInstance<IUnitOfWork>(); return unitOfWork.Session; }); For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use<NHibernateUnitOfWork>();

 builder.RegisterType<NHibernateSessionSource>().As<ISessionSource>().SingleInstance();

 // Is the following recommended? Define that whenever request an `ISession` it should return the `Session` property of the concrete implementation of `INHibernateUnitOfWork`:
 builder.Register(ctx =>
 {
     var unitOfWork = (INHibernateUnitOfWork)ctx.Resolve<IUnitOfWork>();
     return unitOfWork.Session;
 }).As<ISession>().InstancePerRequest(); 

 builder.RegisterType<NHibernateUnitOfWork>().As<IUnitOfWork>().InstancePerRequest();

Autofac:

<div class="col-lg-8 col-md-8 posts-bottom">
 <?php query_posts('category_name=home&showposts=15'.$num_posts.'&paged='.get_query_var('paged').'&p='.$ids);       
    if (have_posts()) : while (have_posts()) : the_post(); $count++;?>
    <div class="post-unit">
        <div class="foto">
            <a href="<?php the_permalink();?>"> <img id="imagem-post" class="thumbnail-post" src="<?php the_post_thumbnail_url(); ?>"/> </a>
        </div>
        <div class="info">
            <p class="date-post"> • <?php echo get_the_date( 'd/m/Y \à\s H:i' ); ?> </p>
            <a class="link-title-post" href="<?php the_permalink(); ?>"> <h2> <?php echo get_the_title(); ?> </h2> </a>
            <?php $subtitulo = get_post_custom_values('subtitulo'); ?>
            <a style="text-decoration: none;" href="<?php the_permalink(); ?>"> <p class="subtitle"> <?php echo $subtitulo[0] ?> </p> </a>
            <?php $titulo_whats = rawurlencode('Vi essa notícia e lembrei de você: ');
            $link_whats = rawurlencode(get_the_permalink()); ?>
            <div id="share-buttons-small">
                <a class="face-icon" href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>" target="_blank"></a>
                <a class="google-icon" href="https://plus.google.com/share?url=<?php the_permalink(); ?>" target="_blank"></a>
                <a class="twitter-icon" href="https://twitter.com/share?url=<?php the_permalink(); ?>&amp;hashtags=goiasverdade" target="_blank"></a>
                <a class="whats-icon" href="whatsapp://send?text=<?php echo $titulo_whats;?> <?php echo $link_whats;?>" target="_blank"></a>
            </div>
        </div>
    <?php endwhile; endif; ?>
    </div> 
    <div class="navigation"><p><?php posts_nav_link(); ?></p></div>
</div>

1 个答案:

答案 0 :(得分:0)

以下是我们从评论中得到的内容。

OP担心lambda中的ctx代表根容器,因此认为这些注册不符合最佳实践,因为Autofac - 像任何其他IoC容器一样 - 建议不要解决来自根容器。

事实证明,ctx表示lambda执行时的当前解析上下文。

在注册中使用InstancePerRequest指向事实ctx,在这种情况下,将是HTTP请求生命周期范围上下文。

总而言之,目前的Autofac注册看起来很好,与StructureMap类似。