当前位置: 首页 > 工具软件 > StructureMap > 使用案例 >

DI 依赖注入之StructureMap框架

糜博远
2023-12-01

DI  依赖注入之StructureMap框架

一.简叙:

structureMap只是DI框架中的其中之一。

二.安装及使用:

1.懒人方法:

使用MVC5项目时,可以直接在nuget程序包中安装StructureMap.MVC5;(根据自己的mvc版本安装对应的)

使用webapi项目时,可以直接在nuget程序包中安装StructureMap.WebApi2

public class DefaultRegistry : Registry
    {
        #region Constructors and Destructors

        public DefaultRegistry()
        {
            Scan(
                scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                });
            //For<IExample>().Use<Example>();
            For<AWSW.IDAL.IUserDAL>().Use<AWSW.DAL.UserDAL>();
            For<AWSW.IBLL.IUserBLL>().Use<AWSW.BLL.UserBLL>();
        }

        #endregion
    }

2.勤快人方法:(手动需要写几个类)

通过nuget方式安装structureMap框架,版本根据自己的项目需要选择。

(1)创建容器:

public class IoC
    {
        public static IContainer Initialize()
        {
            return new Container(x =>
            {
                x.AddRegistry<DefaultRegistry>();
            });
        }
    }

(2)注册:

public class DefaultRegistry : Registry
    {
        public DefaultRegistry()
        {
            Scan(x =>
            {
                x.TheCallingAssembly();
                x.WithDefaultConventions();
            });
            For<IUserDAL>().Use<UserDAL>();
            For<IUserBLL>().Use<UserBLL>();
        }
    }

(3)调用:(global.asax文件中调用来初始化容器)

var container = IoC.Initialize();

【到此步,在没有使用mvc或webapi项目的时候已经完了】

(4)mvc使用

SetResoler参数是实现IDependencyResolver的类,所以我们写个实现它的类,实现即可

DependencyResolver.SetResolver(new MVCStructureMapDependencyResolver(container));

实现IDependencyResolver接口的类

public class MVCStructureMapDependencyResolver : IDependencyResolver
    {
        IContainer _container;
        public MVCStructureMapDependencyResolver(IContainer container)
        {
            this._container = container;
        }
        public object GetService(Type serviceType)
        {
            try
            {
                return this._container.GetInstance(serviceType);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            try
            {
                return this._container.GetAllInstances(serviceType).Cast<object>();
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

 

(5)webapi使用:

DependencyResolver的属性需要一个实现IDependencyResolver接口类,并且IDependencyResolver接口内BeginScope的方法返回值又是个接口IDependencyScope,于是还得去实现它。就有了如下的代码:

GlobalConfiguration.Configuration.DependencyResolver = new WebApiStructureMapDependencyResolver(container);
public class WebApiStructureMapDependencyResolver : WebApiStructureMapDependencyScope, IDependencyResolver
    {
        IContainer container;
        public WebApiStructureMapDependencyResolver(IContainer container) : base(container)
        {
            this.container = container;
        }

        IDependencyScope IDependencyResolver.BeginScope()
        {
            return new WebApiStructureMapDependencyScope(container);
        }
    }

    public class WebApiStructureMapDependencyScope : IDependencyScope
    {
        IContainer container;
        public WebApiStructureMapDependencyScope(IContainer container)
        {
            this.container = container;
        }

        public void Dispose()
        {
            this.container.Dispose();
        }

        public object GetService(Type serviceType)
        {
            try
            {
                return container.GetInstance(serviceType);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            try
            {
                return container.GetAllInstances(serviceType).Cast<object>();
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

 

转载于:https://www.cnblogs.com/zlp520/p/9094730.html

 类似资料: