标签: structuremap

注入AutoMapper

我一直在努力将AutoMapper注入控制器.我喜欢Code Camp Server的实现.它创建了一个围绕AutoMapper的IMappingEngine的包装器.依赖注入使用StructureMap完成.但我需要使用Castle Windsor作为我的项目.那么,我们如何使用Windsor实现以下依赖注入和设置?我不是在寻找Castle Windsor中的逐行等效实现.如果你想这样做,请随意.相反,什么是Windsor相当于StructureMap的注册表和配置文件?我需要Profile来定义CreateMap <>,如下所示.

谢谢.

会议控制器:

public MeetingController(IMeetingMapper meetingMapper, ...)
Run Code Online (Sandbox Code Playgroud)

会议映射器:

public class MeetingMapper : IMeetingMapper
{

    private readonly IMappingEngine _mappingEngine;

    public MeetingMapper(IMappingEngine mappingEngine)
    {
      _mappingEngine = mappingEngine;
    }

    public MeetingInput Map(Meeting model)
    {
        return _mappingEngine.Map<Meeting, MeetingInput>(model);    
    }

    ......
}
Run Code Online (Sandbox Code Playgroud)

自动映射器注册表:

public class AutoMapperRegistry : Registry
{

    public AutoMapperRegistry()
    {
        ForRequestedType<IMappingEngine>().TheDefault.Is.ConstructedBy(() => Mapper.Engine);
    }
}
Run Code Online (Sandbox Code Playgroud)

会议映射器配置文件:

public class MeetingMapperProfile : Profile
{

    public static Func<Type, object> CreateDependencyCallback = (type) => Activator.CreateInstance(type);

    public T CreateDependency<T>()
    { …
Run Code Online (Sandbox Code Playgroud)

structuremap castle-windsor automapper

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

StructureMap IRegistrationConvention注册非默认命名约定?

我目前有一堆像这样的存储库

IMyRepository
IAnotherRepository

它们都继承自IRepository(如果这有帮助的话)

如何让structuremap使用IRegistryConvention扫描程序来注册我命名的具体类型

SqlMyRepository
SqlAnotherRepository

structuremap configuration

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

StructureMap:我如何单元测试注册表类?

我有一个这样的注册表类:

public class StructureMapRegistry : Registry
{
    public StructureMapRegistry()
    {
        For<IDateTimeProvider>().Singleton().Use<DateTimeProviderReturningDateTimeNow>();
    }
Run Code Online (Sandbox Code Playgroud)

我想根据我的意图测试配置,所以我开始编写测试:

public class WhenConfiguringIOCContainer : Scenario
{
    private TfsTimeMachine.Domain.StructureMapRegistry registry;
    private Container container;

    protected override void Given()
    {
        registry = new TfsTimeMachine.Domain.StructureMapRegistry();
        container = new Container();
    }

    protected override void When()
    {
        container.Configure(i => i.AddRegistry(registry));
    }

    [Then]
    public void DateTimeProviderIsRegisteredAsSingleton()
    {
        // I want to say "verify that the container contains the expected type and that the expected type
        // is registered as a singleton
    }
}
Run Code Online (Sandbox Code Playgroud)

如何验证注册表是否符合我的期望?注意:我介绍了容器,因为我没有在Registry类上看到任何可用的验证方法.理想情况下,我想直接在注册表类上进行测试.

.net c# structuremap

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

StructureMap针对所有可能的具体实现注册泛型类型

我有以下内容:

public interface ICommand { }
public class AddUser : ICommand
{
    public string Name { get; set; }
    public string Password { get; set; }
}

public interface ICommandHandler<T> : IHandler<T> where T : ICommand
{
    void Execute(T command);
}

public class AddUserHandler : ICommandHandler<AddUser>
{
    public void Execute(AddUser command)
    {
        Console.WriteLine("{0}: User added: {1}", GetType().Name, command.Name);
    }
}

public class AuditTrailHandler : ICommandHandler<ICommand>
{
    public void Execute(ICommand command)
    {
        Console.WriteLine("{0}: Have seen a command of type {1}", GetType().Name, …
Run Code Online (Sandbox Code Playgroud)

c# structuremap

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

StructureMap初学者| 物业注入

这里已经提到了这个问题的一部分:structuremap属性注入,但从未给出答案.

使用StructureMap,可以进行Property Injection

class SomeController : Controller
{
 public IService Service
 {
  get;
  set;
 }
}
Run Code Online (Sandbox Code Playgroud)

注射得当吗?我是一个

structuremap asp.net-mvc-3

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

如何在不依赖NHibernate的情况下为每个请求实现NHibernate会话?

我之前提出过这个问题,但我仍然在努力寻找一个可以让我理解的例子(请不要只是告诉我看看S#arp架构项目,至少没有一些方向).

到目前为止,我已经在我的网络项目中实现了近乎持久的无知.我的存储库类(在我的数据项目中)在构造函数中使用了一个ISession:

public class ProductRepository : IProductRepository
{
    private ISession _session;
    public ProductRepository(ISession session) {
        _session = session;
    }
Run Code Online (Sandbox Code Playgroud)

在我的global.asax中,我公开当前会话,并在beginrequest和endrequest上创建和处理会话(这是我对NHibernate的依赖):

    public static ISessionFactory SessionFactory = CreateSessionFactory();

    private static ISessionFactory CreateSessionFactory() {
        return new Configuration() 
            .Configure()
            .BuildSessionFactory();
    }

    protected MvcApplication()  {
        BeginRequest += delegate {
            CurrentSessionContext.Bind(SessionFactory.OpenSession());
        };
        EndRequest += delegate {
            CurrentSessionContext.Unbind(SessionFactory).Dispose();
        };
    }
Run Code Online (Sandbox Code Playgroud)

最后我的StructureMap注册表:

    public AppRegistry() {
        For<ISession>().TheDefault
            .Is.ConstructedBy(x => MvcApplication.SessionFactory.GetCurrentSession());

        For<IProductRepository>().Use<ProductRepository>();
    }
Run Code Online (Sandbox Code Playgroud)

看来我需要我自己的ISession和ISessionFactory的通用实现,我可以在我的web项目中使用它并注入我的存储库?

所以只是为了澄清 - 我在我的存储库层使用NHibernate并希望使用session-per-(http)请求.因此,我正在向我的存储库构造函数中注入一个ISession(使用structuremap).目前,为了在每个请求中创建和处理会话,我必须从我的web项目中引用NHibernate.这是我想要删除的依赖项.

谢谢,本

structuremap nhibernate asp.net-mvc dependency-injection

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

ASP.NET中的跨进程模拟

我正在使用ASP.NET MVC 3构建REST API.我正在使用SpecFlow和NUnit作为测试运行器来实现BDD风格.

由于它是一个REST API,因此测试Url:s显然非常重要,因此我希望能够在规范中进行真正的HTTP调用.

我现在正在寻找有关如何实现Cross Process Mocking的技巧.简而言之,我想用我在Specs中生成的实体来模拟数据层.

在Rails应用程序中,我会使用Webrat.有没有相当于.NET的东西呢?

我已经尝试过Deleporter,但它似乎无法"发送"高级构造(在规范中创建一个简单的字符串并在Deleporter中使用它,但不适用于自定义类,属性都变为null)

有没有人有关于如何做到这一点的经验或提示?

编辑:我在Deleporter尝试做的事情是这样的(我知道我可以在Deleporter代码中生成模型,但这是一个简化的例子,所以这对我不起作用):

var models = Builder<Foo>.CreateListOfSize(300);
Deleporter.Run(() =>
{
  var mockService = new Mock<IFooService>();
  // Models will be a list of 300 Foos but the foos properties will all be null
  mockService.Setup(s => s.GetStuff()).Returns(models);
  ObjectFactory.Inject(mockService.Object);
});
Run Code Online (Sandbox Code Playgroud)

c# structuremap bdd specflow asp.net-mvc-3

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

我在哪里可以找到最新的StructureMap文档?

http://docs.structuremap.net/似乎有很多使用已弃用成员的旧示例.有没有一个地方可以找到最新的StructureMap doco?

structuremap

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

StructureMap在HttpContext上抛出ArgumentNullException

我有一个非常奇怪的问题 StructureMap.MVC5

我在Visual Studio中创建了一个全新的MVC5项目(选择了ASP.net MVC项目的默认选项.)

然后我通过nuget包管理器(Install-Package StructureMap.MVC)安装了structuremap.mvc5 .

然后我将以下代码添加到"HomeController.cs"文件的顶部:

namespace TestMVC.Controllers
{
    public interface ITest
    {
        string TestMessage();
    }
    public class Test : ITest
    {
        public string TestMessage()
        {
            return "this worked again 23";
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后我添加了一个构造函数和私有成员,如下所示:

public class HomeController : Controller
{
    private readonly ITest _test;

    public HomeController(ITest test)
    {
        _test = test;
    }
Run Code Online (Sandbox Code Playgroud)

最后,我更新了About操作结果,如下所示:

public ActionResult About()
{
    ViewBag.Message = _test.TestMessage();

    return View();
}
Run Code Online (Sandbox Code Playgroud)

该项目编译并启动.我得到了正常的默认索引页面,但是在浏览器中返回页面后的2到5秒之间,我在return此方法的行中的"StructureMapDependencyScope.cs"中抛出异常:

private HttpContextBase HttpContext {
    get {
        var ctx = …
Run Code Online (Sandbox Code Playgroud)

c# structuremap asp.net-mvc structuremap3

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

如何使用StrcutureMap依赖注入在Web API测试中模拟AutoMapper IMapper对象?

所以我从头开始构建WebAPI,包括我在网上找到的一些最佳实践,例如依赖注入和使用自动映射器的域< - > DTO映射等.

我的API控制器现在看起来与此类似

public MyController(IMapper mapper)
{
}
Run Code Online (Sandbox Code Playgroud)

和AutoMapper注册表:

public AutoMapperRegistry()
{
    var profiles = from t in typeof(AutoMapperRegistry).Assembly.GetTypes()
                   where typeof(Profile).IsAssignableFrom(t)
                   select (Profile)Activator.CreateInstance(t);

    var config = new MapperConfiguration(cfg =>
    {
        foreach (var profile in profiles)
        {
            cfg.AddProfile(profile);
        }
    });

    For<MapperConfiguration>().Use(config);
    For<IMapper>().Use(ctx => ctx.GetInstance<MapperConfiguration>().CreateMapper(ctx.GetInstance));
}
Run Code Online (Sandbox Code Playgroud)

我也在构建一些测试用例,实现MOQ,这是我感到有些不确定的地方.每当调用我的控制器时,我需要传递一个IMapper,如下所示:

var mockMapper = new Mock<IMapper>();
var controller = new MyController(mockMapper.Object);
Run Code Online (Sandbox Code Playgroud)

但是,如何配置IMapper以获得正确的映射?在配置Mapper之前重新创建我已经创建的相同逻辑会感觉多余.所以我想知道这样做的推荐方法是什么?

structuremap unit-testing moq automapper asp.net-web-api

7
推荐指数
2
解决办法
7753
查看次数