标签: structuremap

结构映射到Ninject规则

如何在Ninject中编写此StructureMap行

        ForRequestedType<HttpContextBase>()
            .TheDefault.Is.ConstructedBy(x => new HttpContextWrapper(HttpContext.Current));
Run Code Online (Sandbox Code Playgroud)

structuremap ninject

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

MVC3和StructureMap,基于控制器注入具体类

假设我的几个控制器构造函数采用接口 - IPetInterface IPetInterface有3个具体实现.

您将如何配置StructureMap以基于需要它的控制器提供其中一个具体实现.

原油的例子....

class DogStuff: IPetInterface{}

class CatStuff: IPetInterface{}

class GiraffeStuff: IPetInterface{}

class DogController : Controller
{
    DogController(IPetInterface petStuff)

    // some other stuff that is very unique to dogs
}

class CatController : Controller
{
    CatController(IPetInterface petStuff)

    // some other stuff that is very unquie to cats
}
Run Code Online (Sandbox Code Playgroud)

c# structuremap asp.net-mvc-3

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

多个连接字符串,一个具有依赖注入的存储库

现在我有一个基本的 IRepository,它接受 IConnect (包含一个字符串值)。我在获取 DI(结构图)来确定要使用哪个连接字符串时遇到问题。理论上,如果我在实体上使用属性,我可以编写一个注册表/扫描仪来确定这一点,但我想知道是否有更简单的方法来做到这一点?

现在我有这样的东西

ObjectFactory.Initialize(factory =>
{
    factory.For<IConnect>().Singleton().Use<ConnectToMarket>()
        .Ctor<string>("connectionString")
        .Is(_marketConnectionString);
    //and some other stuff
});
Run Code Online (Sandbox Code Playgroud)

有想法吗?

structuremap dependency-injection

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

如何从 Web API 2 中的静态类使用 StructureMap?

我有一个 WebApi 解决方案,我正在使用StructureMap.WebApi2 Nuget 包进行依赖注入。

我想使用Fody Tracer编织跟踪方法。我正在实现我自己的自定义日志适配器,这要求我从静态类/方法返回我的记录器实例。

我使用结构映射从静态类/方法获取记录器实例的正确方法是什么?

c# structuremap static fody asp.net-web-api2

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

asp.net核心内置依赖注入长代码

在我们之前的应用程序中,我们使用了StructureMap,我们可以编写很少的代码.

在每个服务之前我们添加了依赖项,如:

[SetterProperty]
public IXService XService { get; set; }
Run Code Online (Sandbox Code Playgroud)

并在构造函数中

ObjectFactory.BuildUp(this);
Run Code Online (Sandbox Code Playgroud)

然后在测试中我们可以简单地实例化它

var service = new XService();
Run Code Online (Sandbox Code Playgroud)

现在,我们启动另一个应用程序并使用asp.net核心内置DI容器.看起来我们应该为每个测试编写很多代码和非常长的构造函数:

    private readonly ILogger<AccountsController> _logger;
    private readonly IMapper _mapper;
    private readonly IAccountBlService _accountBlService;
    private readonly IValidationHelper _validationHelper;
    private readonly IValidator<AccountDTO> _accountDTOValidator;
    private readonly Example _example;
    private readonly IConfiguration _configuration;

    public AccountsController(BillingContext context, ILogger<AccountsController> logger, IMapper mapper, IAccountBlService accountBlService, 
        IValidationHelper validationHelper, IValidator<AccountDTO> accountDTOValidator, IOptions<Example> example, IConfiguration configuration)
    {
        _logger = logger;
        _mapper = mapper;
        _accountBlService = accountBlService;
        _validationHelper = validationHelper;
        _accountDTOValidator = accountDTOValidator; …
Run Code Online (Sandbox Code Playgroud)

structuremap dependency-injection ioc-container asp.net-core

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

我的MVC存储库模式和StructureMap的问题

我有一个在ado.net实体框架之上创建的存储库模式.当我试图实现StructureMap来解耦我的对象时,我一直得到StackOverflowException(无限循环?).这是模式的样子:

IEntityRepository,其中TEntity:class定义基本的CRUD成员

MyEntityRepository:IEntityRepository实现CRUD成员

IEntityService,其中TEntity:class定义返回每个成员的公共类型的CRUD成员.

MyEntityService:IEntityService使用存储库检索数据并返回一个公共类型作为结果(IList,bool等)

问题似乎与我的服务层有关.更具体地说是构造函数.

    public PostService(IValidationDictionary validationDictionary)
        : this(validationDictionary, new PostRepository())
    { }

    public PostService(IValidationDictionary validationDictionary, IEntityRepository<Post> repository)
    {
        _validationDictionary = validationDictionary;
        _repository = repository;
    }
Run Code Online (Sandbox Code Playgroud)

从控制器,我传递一个实现IValidationDictionary的对象.我明确调用第二个构造函数来初始化存储库.

这是控制器构造器的外观(第一个创建验证对象的实例):

    public PostController()
    {
        _service = new PostService(new ModelStateWrapper(this.ModelState));
    }

    public PostController(IEntityService<Post> service)
    {
        _service = service;
    }
Run Code Online (Sandbox Code Playgroud)

如果我没有传递我的IValidationDictionary对象引用,一切都有效,在这种情况下,第一个控制器构造函数将被删除,服务对象只有一个构造函数接受存储库接口作为参数.

我感谢任何帮助:)谢谢.

structuremap model-view-controller repository-pattern

0
推荐指数
1
解决办法
1869
查看次数

StructureMap双向/循环依赖的问题

我目前正在我们的业务层中集成StructureMap,但由于双向依赖性而存在问题.

该层包含多个管理器,每个管理器可以相互调用方法:没有通信限制或规则.这还包括可能的循环依赖性,如下例所示.我知道设计本身是有问题的,但目前我们只是希望StructureMap能够工作,并将在未来专注于进一步的重构.

每个经理实现IManager接口:

internal interface IManager
{
  bool IsStarted { get; }

  void Start();
  void Stop();
}
Run Code Online (Sandbox Code Playgroud)

并且还有自己的特定界面:

internal interface IManagerA : IManager
{
  void ALogic();
}

internal interface IManagerB : IManager
{
  void BLogic();
}
Run Code Online (Sandbox Code Playgroud)

这是两个虚拟管理器实现:

internal class ManagerA : IManagerA
{
  public IManagerB ManagerB { get; set; }

  public void ALogic() { }

  public bool IsStarted { get; private set; }
  public void Start() { }
  public void Stop() { }
} …
Run Code Online (Sandbox Code Playgroud)

structuremap circular-dependency bidirectional

0
推荐指数
1
解决办法
3646
查看次数

让流畅的NHibernate构建数据库表

我正在使用ASP.NET MVC2,Fluent NHibernate,StructureMap和PostgreSQL构建应用程序.当谈到Fluent NHibernate时,我是一个全新的人.我从几个不同的来源获得了一个设置但是当我构建并运行我的应用程序时,它不会在我的连接字符串中为数据库创建数据库表.我有几个不同文件的代码,所以我不知道我需要发布哪个代码,如果我应该发布所有它.如果有一个要检查的密钥请告诉我或让我知道发布所有代码.谢谢!

structuremap postgresql asp.net-mvc fluent-nhibernate

0
推荐指数
1
解决办法
3663
查看次数

如何配置StructureMap以获取多个contstructor参数?

我有以下类型需要由StructureMap实例化:

public class AWebService : IAWebService
{
    private readonly string _applicationId;
    private readonly string _username;
    private readonly string _password;

    public AWebService(string applicationId, string username, string password)
    {
        _applicationId = applicationId;
        _username = username;
        _password = password;
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是这个构造函数需要3个参数.我已经看到了如何为StructureMap提供一个参数的示例(例如,在使用StructureMap时传递构造函数参数)但是我不确定我需要做什么来传递3.

它只是一个案例:

For<IAWebService>().Use<AWebService>()
  .Ctor<string>("applicationId").Is(GetIDFromConfig())
  .Ctor<string>("username").Is(GetUsernameFromConfig())
  .Ctor<string>("password").Is(GetPasswordFromConfig());
Run Code Online (Sandbox Code Playgroud)

或者我必须以不同的方式配置它?

c# structuremap

0
推荐指数
1
解决办法
1937
查看次数

.net mvc与StructureMap依赖注入

我正在开发一个.net webAPI项目,我们正在使用依赖注入(我相信StructureMap)为每个会话数据访问对象提供控制器实例.这部分效果很好.

我现在需要做的是使用DataAccessObject提供AuthorizationFilterAttribute的实例.

AuthorizationFilterAttribute通过注释使用.例如:

[ApiKeyAuthorization]
public DataModel ControllerAction(int id) { }
Run Code Online (Sandbox Code Playgroud)

这将确保在控制器运行之前,检查授权.

我需要的是参考我的每个会话共享数据库访问对象创建一个ApiKeyAuthorization对象.

是否有一种简单的方法可以实现这一目标?

structuremap asp.net-mvc asp.net-web-api

0
推荐指数
1
解决办法
1123
查看次数

如何将 StructureMap 用于现实世界的应用程序

我在 ASP.NET MVC 中开发了 Web 应用程序,使用 StructureMap DI 模式从 BAL 检索数据到 UI,并使用分层架构模式从 DAL 检索数据到 BAL。在下面找到我的类图。

看我的类图

所以,我厌倦了将 StructureMap 服务注册为

    public class Bootstrapper
    {
          public static void Initialize()
          {
              StructureMapConfiguration.AddRegistry(new ServiceRegistry());
          }
          public class ServiceRegistry : Registry
          {
              protected override void configure()
              {
                   ForRequestedType<IVehicleService> ().TheDefaultIsConcreteType<VehicleService>();
               }
          }
    }
Run Code Online (Sandbox Code Playgroud)

但是,配置未正确应用,出现错误并说“在接口类中找不到某些方法”。当然,它是正确的。因为在我的 BAL 类中从 DAL 继承了一些方法/函数。所以,请帮助我解决这个问题或提供任何最佳实践来应用我的项目。

structuremap

0
推荐指数
1
解决办法
492
查看次数

从 StructureMap 获得的 HttpContext 上的空用户

好的,我之前的问题/设置有太多的变量,所以我将其分解为简单的骨骼组件。

鉴于以下使用 StructureMap3 的代码...

//IoC setup
For<HttpContextBase>().UseSpecial(x => x.ConstructedBy(y => HttpContext.Current != null ? new HttpContextWrapper(HttpContext.Current) : null ));
For<ICurrentUser>().Use<CurrentUser>();

//Classes used
public class CurrentUser : ICurrentUser
{
    public CurrentUser(HttpContextBase httpContext)
    {
        if (httpContext == null) return;
        if (httpContext.User == null) return;
        var user = httpContext.User;
        if (!user.Identity.IsAuthenticated) return;
        UserId = httpContext.User.GetIdentityId().GetValueOrDefault();
        UserName = httpContext.User.Identity.Name;
    }

    public Guid UserId { get; set; }
    public string UserName { get; set; }
}

public static class ClaimsExtensionMethods
    public static Guid? GetIdentityId(this IPrincipal …
Run Code Online (Sandbox Code Playgroud)

c# structuremap asp.net-mvc dependency-injection structuremap3

0
推荐指数
1
解决办法
3237
查看次数

StructureMap和.Net Core

我正在创建一个准系统.NET Core web api项目(从下面的空白模板开始)https://andrewlock.net/removing-the-mvc-razor-dependencies-from-the-web-api-template-in-asp-网核心/

下面的代码工作正常,直到我添加了StructureMap.现在,我收到了这个错误.

StructureMap.StructureMapConfigurationException:没有注册默认实例,无法自动确定类型'System.IServiceProvider'

没有为System.IServiceProvider指定配置

1.)Container.GetInstance()

在StructureMap.SessionCache.GetDefault(Type pluginType,IPipelineGraph pipelineGraph),位于WebApplication4.Startup.ConfigureServices(IServiceCollection服务)上的StructureMap.Container.GetInstanceT ---从抛出异常的上一个位置开始的堆栈跟踪---在System.Runtime Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()中的Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()中的Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)上的.ExceptionServices.ExceptionDispatchInfo.Throw()

有任何想法吗?

请注意:我们没有使用,builder.AppMvc()因为我们正试图尽可能减少这个api.

这是相关的代码.

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        var builder = services.AddMvcCore();
        builder.AddApiExplorer();

        builder.AddAuthorization();
        builder.AddFormatterMappings();
        builder.AddJsonFormatters();
        builder.AddCors();

        // Register the Swagger generator, defining one or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
        });

        var container = ContainerConfigurator.Configure();

        return container.GetInstance<IServiceProvider>();
    }

    // This method gets called by the runtime. Use this method to …
Run Code Online (Sandbox Code Playgroud)

c# structuremap .net-core asp.net-core

0
推荐指数
1
解决办法
1942
查看次数