标签: structuremap

使用(流利)NHibernate与StructureMap(或任何IoCC)

在我学习NHibernate的过程中,我已经达到了下一个障碍; 我该如何将它与StructureMap集成?

虽然代码示例非常受欢迎,但我对一般过程更感兴趣.

我打算做的是......

  • 使用Fluent NHibernate创建我的类映射以在NHibs配置中使用
  • 实现ISession和ISessionFactory
  • 将我的ISessionFactory实例作为单例引导到StructureMap中
  • 使用StructureMap注册ISession,使用per-HttpRequest缓存

但是,不要我需要调用我的会话实例各种整洁行动方法的HttpRequest的结束(因为这就是它的生命结束)?

如果我在Dispose()中进行整理,那么structuremap会为我解决这个问题吗?

如果没有,我该怎么办?

谢谢

安德鲁

c# structuremap nhibernate fluent-nhibernate

5
推荐指数
1
解决办法
3507
查看次数

对于某些情况,使用StructureMap将不同的对象注入构造函数

我有IRepository <T>,并实现了SqlRepository <T>.SqlRepository在构造函数中有DataContext参数.

SM配置如下所示:

x.ForRequestedType(typeof(IRepository<>))
.TheDefaultIsConcreteType(typeof(SqlRepository<>));

x.ForRequestedType<DataContext>().CacheBy(InstanceScope.Hybrid)
.TheDefault.Is.ConstructedBy(()=>{
                        var dc = new FirstDataContext();
                        dc.Log = new DebuggerWriter();
                        return dc;
                    });
Run Code Online (Sandbox Code Playgroud)

但是为了构建IRepository <SpecificObject>,我想注入不同的DataContext.我怎么说SM当我要求IReposiotry <SpecificObject>我想要不同的DataContext,而不是FirstDataContext而是SecondDataContext(DC转到不同的数据库).

例如,当我要求IRepository <T>时,我想要注入FirstDataContext,但是当我要求明确表示IRepository <Product>时,我想要注入SecondDataContext.

此外,SecondDC应该是由SM缓存的混合!

.net c# structuremap inversion-of-control

5
推荐指数
1
解决办法
2861
查看次数

如何通过StructureMap为具有WCF的自定义角色提供程序提供依赖注入?

我们将在WCF中使用自定义角色提供程序.重写的方法GetRolesForUser将需要使用已存在的RoleRepository.

现在,使用一个普通的类,我们使用StructureMap构造它,并且通过构造函数注入RoleRepository依赖项.

但是,它是WCF,它通过web.config中的roleManager属性来构造自定义角色提供程序类并且"完成".

我真的不想将RoleRepository depndency硬连接到自定义角色probvider类,但它看起来像我必须要的.

有任何想法吗?

structuremap wcf dependency-injection roleprovider

5
推荐指数
1
解决办法
1055
查看次数

structuremap ObjectFactory.GetAllInstances <IHandle <TEvent >>()

我正忙着在最近的一个项目中实现事件.

我已经验证了structuremap正在扫描正确的汇编和添加EventHandler

Scan(cfg =>
            {
               cfg.TheCallingAssembly();
                cfg.IncludeNamespace("ABC.EventHandler");
                cfg.ConnectImplementationsToTypesClosing(typeof(IHandle<>));

           });

 public class StructureMapEventDispatcher : IEventDispatcher
    {

        public void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent
        {

            foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>())
            {

                handler.Handle(eventToDispatch);

            }

        }

    }
Run Code Online (Sandbox Code Playgroud)

之前我曾经从域中触发事件.有点像Dispatcher.RaiseEvent(new [domainEvent class](x,y,z));

而事件将被激起.我不得不改变设计,我现在收集集合中的collectiong事件

_domainEvents = new Collection<IDomainEvent>();
Run Code Online (Sandbox Code Playgroud)

我将域保存到存储库后再将其提升

 public static void Raise(ICollection<IDomainEvent> domainEvents)
        {
            foreach (var domainEvent in domainEvents)
            {
                DomainEventDispatcher.Raise(domainEvent);
            }

        }
Run Code Online (Sandbox Code Playgroud)

但现在

ObjectFactory.GetAllInstances<IHandle<TEvent>>() 返回0个处理程序计数

如果我注意的话

ObjectFactory.GetAllInstances<IHandle<DomainEventClass>>() 它正确返回处理程序集合(目前我有2个,它显示2个计数)

...我假设这与事件被提出类型IDomainEvent 而不是实际类型有关,这使得结构图很难解决它.

我该如何解决这个问题?

问候,

三月

-

编辑1:

我已经确定struturemap容器包含从程序集扫描的所有事件处理程序.

编辑2

我不知道如何让这个问题引起更多关注.我正在为获得所需结果的解决方案添加赏金.如果问题不明确,请询问.

基本上我想ObjectFactory.GetAllInstances<IHandle<TEvent>>()返回处理程序TEvent …

c# structuremap domain-events

5
推荐指数
1
解决办法
1720
查看次数

使用StructureMap在自定义成员资格提供程序中注入

我使用StructureMap做了一点工作,我设法在我的控制器中注入(通过构造函数注入)一个接口的具体类型存储库.

现在,我需要将存储库类型注入我的自定义成员资格提供程序.但是怎么样?我的自定义成员资格提供程序是通过Membership.Provider.ValidateUser(例如)创建的.

对于控制器我创建了一个这样的类:

public class IocControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(
        System.Web.Routing.RequestContext requestContext, 
        Type controllerType)
    {
        return (Controller)
            ObjectFactory.GetInstance(controllerType);
    }
}
Run Code Online (Sandbox Code Playgroud)

并在Global.asaxApplication_Start():

//...
ObjectFactory.Initialize(x =>
{
    x.AddRegistry(new ArticleRegistry());
}
                                    );


ControllerBuilder.Current.SetControllerFactory(
    new IocControllerFactory());
//...
Run Code Online (Sandbox Code Playgroud)

但是如何使用StructureMap在我的自定义成员资格提供程序中注入具体类型?

c# structuremap asp.net-mvc membership-provider asp.net-mvc-2

5
推荐指数
1
解决办法
674
查看次数

结构图 - 覆盖注册

是否可以在注册表中注册接口,然后"重新注册"它以覆盖第一次注册?

IE:

For<ISomeInterface>().Use<SomeClass>();
For<ISomeInterface>().Use<SomeClassExtension>();
Run Code Online (Sandbox Code Playgroud)

我在运行时想要的是我的对象工厂SomeClassExtension在我要求时返回ISomeInterface.

提前致谢!

c# structuremap registry dependencies code-injection

5
推荐指数
1
解决办法
2020
查看次数

使用structuremap将SetterProperty注入到Asp.Net MVC ActionFilter

为什么我无法将SetterProperty通道注入StructureMapMVC ActionFilter?

public class LockProjectFilter : ActionFilterAttribute
    {
        [SetterProperty]
        public ISecurityService SecurityService { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var loggedinStaffId = SecurityService.GetLoggedInStaffId();
            if (loggedinStaffId == 1)
                throw new ArgumentNullException();
            base.OnActionExecuting(filterContext);
        }
    }


    public static IContainer Initialize()
    {
        ObjectFactory.Initialize(x =>
                         {
                             x.Scan(scan =>
                                            {
                                                scan.TheCallingAssembly();
                                                scan.WithDefaultConventions();
                                                scan.AssemblyContainingType<ISecurityService>();
                                            });
                             x.SetAllProperties(p => p.OfType<ISecurityService>());
                             //x.ForConcreteType<LockProjectFilter>().Configure
                                // .Setter(c => c.SecurityService).IsTheDefault();
                         });
        return ObjectFactory.Container;
    }
Run Code Online (Sandbox Code Playgroud)

structuremap dependency-injection actionfilterattribute asp.net-mvc-4

5
推荐指数
1
解决办法
1626
查看次数

如何在结构图3中进行setter注入

我已经更新到structuremap 3,现在我不能使用FillAllPropertiesOfType进行setter注入.

它是否已被弃用,我应该使用什么呢?

c# structuremap dependency-injection ioc-container structuremap3

5
推荐指数
1
解决办法
2168
查看次数

结构图和Aspnet.Identity UserManager静态类问题

我在我的帐户控制器中获取正确的UserManager实例时遇到了问题.目前,我无法重置密码以作为我的提供商工作,其他设置被忽略.

在Startup - void ConfigureAuth(IAppBuilder app)中我有以下内容:

app.CreatePerOwinContext<ViewingBookerDatabaseContext>(ViewingBookerDatabaseContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
Run Code Online (Sandbox Code Playgroud)

然后在AccountManager类中,上面引用的方法Create包含以下内容:

public class ApplicationUserManager : UserManager<ApplicationUser>
{
    // Configure the application user manager
    public ApplicationUserManager(ApplicationUserStore store)
        : base(store)
    {
    }

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var manager = new ApplicationUserManager(new ApplicationUserStore(new ViewingBookerDatabaseContext()));

        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };
        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = false,
            RequireDigit = …
Run Code Online (Sandbox Code Playgroud)

c# structuremap static inversion-of-control asp.net-mvc-5

5
推荐指数
1
解决办法
2895
查看次数

摇摇欲坠的用户界面在webapi中未显示任何内容

我跟着这个到XML文档的一部分,以便使用Swashbuckle创建扬鞭文档。它应该允许我通过以下方式查看端点:

http:// localhost:51854 / swagger / ui / index

不幸的是,我看不到任何端点:

在此处输入图片说明

任何想法为什么以及如何解决这个问题?请注意,我是从一个空的webapi项目创建了我的webapi的-也许就是这个问题。一定有一些东西不见了,但我不确定...

现在,我确定了以下代码是根本原因。在Global.asax.cs中:

var container = new XyzWebApiStructureMapContainerConfigurator().Configure(GlobalConfiguration.Configuration);
GlobalConfiguration.Configuration.Services
.Replace(typeof(IHttpControllerActivator),
new StructureMapHttpControllerActivator(container));
Run Code Online (Sandbox Code Playgroud)

一些课程:

public class XyzWebApiStructureMapContainerConfigurator
{
    public IContainer Configure(HttpConfiguration config)
    {
        var container = new Container(new BlaWebApiRegistry());
        config.DependencyResolver = new StructureMapDependencyResolver(container);
        return container;
    }
}

public class StructureMapDependencyResolver : StructureMapDependencyScope, IDependencyResolver, IHttpControllerActivator
{
    private readonly IContainer _container;

    public StructureMapDependencyResolver(IContainer container)
        : base(container)
    {
        _container = container;
        container.Inject<IHttpControllerActivator>(this);
    }

    public IDependencyScope BeginScope()
    {
        return new StructureMapDependencyScope(_container.GetNestedContainer());
    }

    public IHttpController …
Run Code Online (Sandbox Code Playgroud)

structuremap swagger swagger-ui asp.net-web-api2 swashbuckle

5
推荐指数
1
解决办法
6122
查看次数