打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
StructureMap

The first thing you should know is that StructureMap (and other IoC tools like it) are designed tomake compositional and modular software designs easier to build by offloading the grubby mechanics ofresolving dependencies, reading configuration data, and assembling object graphs to the IoC tool insteadof cluttering up your application code.

Before you get started with StructureMap it's recommended that you first get comfortable with the Software Design Conceptsof Dependency Injectionand Inversion Of Control.It's important that you structure and build your application with these concepts in mind to fully utilize StructureMap's abilities.

Assuming that you're already familiar with those concepts, or you'd really rather skip the pedantry and jump right into concrete code, the first thing to do is, go Get StructureMap and jump into usage.

Main Usage

By and large, you really only do two kinds of things with StructureMap:

  1. Configure the container by registering the what and how StructureMap should build or find requested services based on a type and/or name.

  2. Resolve object instances of a service or dependency built out with all of its dependencies.

So let's say that you have a simple object model like so:

    public interface IBar    {    }    public class Bar : IBar    {    }    public interface IFoo    {    }    public class Foo : IFoo    {        public IBar Bar { get; private set; }        public Foo(IBar bar)        {            Bar = bar;        }    }

You could explicitly build a StructureMap Container object to build these types like this:

// Configure and build a brand new// StructureMap Container object            var container = new Container(_ =>            {                _.For<IFoo>().Use<Foo>();                _.For<IBar>().Use<Bar>();            });// Now, resolve a new object instance of IFoo            container.GetInstance<IFoo>()                // should be type Foo                .ShouldBeOfType<Foo>()                // and the IBar dependency too                .Bar.ShouldBeOfType<Foo>();

or utilize StructureMap's type scanning conventions to configure the relationships and do the same thing like this:

            var container = new Container(_ =>            {                _.Scan(x =>                {                    x.TheCallingAssembly();                    x.WithDefaultConventions();                });            });            container.GetInstance<IFoo>()                .ShouldBeOfType<Foo>()                .Bar.ShouldBeOfType<Foo>();

Integrating StructureMap within your application

At some point you will want to integrate StructureMap into your application. Whether you are using Windows Presentation Foundation (WPF), FubuMVC, ASP.NET WebForms, ASP.NET MVC or any other framework or technology, you will have to do some sort of plumbing and bootstrapping. Depending on the used technology or framework there can be important integration points that you will have to use to fully enable the power of StructureMap.

While StructureMap doesn't provide integration support out of the box for all of the frameworks and technologies out there, we do find it important to help you get started with integrating StructureMap into your application. That said, StructureMap does provide integration support for FubuMVC (a web framework, which is part of the same family as StructureMap).

What to do when things go wrong?

StructureMap, and any other IoC tool for that matter, is configuration intensive, which means that their will be problems in that configuration. We're all moving to more convention based type registration, so more stuff is happening off stage and out of your sight, making debugging the configuration even trickier. Not to worry (too much), StructureMap has some diagnostic abilities to help you solve configuration problems:

Need Help?

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
IoC+AOP的简单实现
ASP.NET Core中的依赖注入(3): 服务的注册与提供
通过定义UnityContainer扩展变”Explicit Interception”为”Automatic Interception”
Unity 3学习(一):简介与示例
The magical moment when container load balancing meets service discovery | Rancher Labs
逆变与协变详解(转载)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服