编程语言
首页 > 编程语言> > 【ASP.NET】Spring.Net 通过工厂方法创建对象 构造函数注入 属性注入

【ASP.NET】Spring.Net 通过工厂方法创建对象 构造函数注入 属性注入

作者:互联网

通过工厂方法创建对象分为

通过实例工厂方法创建对象和通过工厂的一个静态方法创建对象

 

下面是工厂的一个静态方法

namespace Wang.OA.DALFactory
{
    public class DbSessionFactory
    {
        //一个请求共用一个DbSession
        public static IDbSession GetCurrentDbSession()
        {
            IDbSession db = CallContext.GetData("DbSession") as IDbSession;
            if (db == null)
            {
                db = new DbSession();
                CallContext.SetData("DbSession", db);
            }

            return db;
        }
    }
}

dals.xml配置如下

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" default-autowire="constructor">
  <!--通过实例工厂方法创建对象-->
  <!--<object name="DbSessionFactory" type="Wang.OA.DALFactory.DbSessionFactory, Wang.OA.DALFactory" singleton="true" />
  <object name="DbSession" type="Wang.OA.DALFactory.DbSession, Wang.OA.DALFactory" singleton="false" factory-method="GetCurrentDbSession"
      factory-object="DbSessionFactory"/>-->
  <!--通过工厂的一个静态方法创建对象-->
  <object name="DbSession" type="Wang.OA.DALFactory.DbSessionFactory, Wang.OA.DALFactory" singleton="false" factory-method="GetCurrentDbSession"/>
</objects>

在services.xml中配置

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" default-autowire="constructor">
  <object name="UserInfoService" type="Wang.OA.BLL.UserInfoService, Wang.OA.BLL" singleton="false">
    <!--<property name="DbSession" ref="DbSession"></property>-->
    <constructor-arg index="0" ref="DbSession"/>
  </object>
  <object name="OrderInfoService" type="Wang.OA.BLL.OrderInfoService, Wang.OA.BLL" singleton="false">
    <!--<property name="DbSession" ref="DbSession"></property>-->
    <constructor-arg index="0" ref="DbSession"/>
  </object>
</objects>

在controllers.xml中配置

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" default-autowire="constructor">
  <object type="Wang.OA.UI.Porta.Controllers.UserInfoController, Wang.OA.UI.Porta" singleton="false" >
    <property name="UserInfoService" ref="UserInfoService" />
    <property name="OrderInfoService" ref="OrderInfoService" />
  </object>
</objects>

 

在UserInfoService类中定义一个构造函数

public class UserInfoService : BaseService<UserInfo>, IUserInfoService
{

    public UserInfoService(IDbSession dbSession) : base(dbSession)
    {

    }
     
    public override void SetCurrentDal()
    {
        CurrentDal = this.DbSession.UserInfoDal;
    }
}

在UserInfoController中

namespace Wang.OA.UI.Porta.Controllers
{
    public class UserInfoController : Controller
    {
        IUserInfoService UserInfoService { get; set; }

        // GET: UserInfo
        public ActionResult Index()
        {
            ViewData.Model = UserInfoService.GetEntities(u => true);
            return View();
        }
    }
}

 

通过属性注入

通过属性注入,更加方便

去除构造函数,只在基类定义两个属性

public IBaseDal<T> CurrentDal { get; set; }

public IDbSession DbSession { get; set; }

子类中

public class UserInfoService : BaseService<UserInfo>, IUserInfoService
{

}

dal.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" default-autowire="constructor">
  <!--通过工厂的一个静态方法创建对象-->
  <object name="DbSession" type="Wang.OA.DALFactory.DbSessionFactory, Wang.OA.DALFactory" singleton="false"
          factory-method="GetCurrentDbSession"/>

  <object name="UserInfoDal" type="Wang.OA.EFDAL.UserInfoDal, Wang.OA.EFDAL" singleton="false"/>
  <object name="OrderInfoDal" type="Wang.OA.EFDAL.OrderInfoDal, Wang.OA.EFDAL" singleton="false"/>
  
</objects>

services.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" default-autowire="constructor">
  <object name="UserInfoService" type="Wang.OA.BLL.UserInfoService, Wang.OA.BLL" singleton="false">
    <property name="DbSession" ref="DbSession"></property>
    <property name="CurrentDal" ref="UserInfoDal"></property>
    <!--<constructor-arg index="0" ref="DbSession"/>-->
  </object>
  <object name="OrderInfoService" type="Wang.OA.BLL.OrderInfoService, Wang.OA.BLL" singleton="false">
    <property name="DbSession" ref="DbSession"></property>
    <property name="CurrentDal" ref="OrderInfoDal"></property>
    <!--<constructor-arg index="0" ref="DbSession"/>-->
  </object>
</objects>

controllers.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" default-autowire="constructor">
  <object type="Wang.OA.UI.Porta.Controllers.UserInfoController, Wang.OA.UI.Porta" singleton="false" >
    <property name="UserInfoService" ref="UserInfoService" />
    <!--<property name="OrderInfoService" ref="OrderInfoService" />-->
  </object>
</objects>

 

标签:xml,ASP,db,IDbSession,UserInfoService,DbSession,构造函数,public,注入
来源: https://blog.csdn.net/weixin_38211198/article/details/99707231