MVC三层基础框架+T4+Spring.Net

原有框架基础上进行扩展运用T4模版添加表,新增Spring.Net。

1、T4模版代码,如下:

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
 output extension=".cs"#>
 
<#

CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);

string inputFile = @"....\\OA.Model\\Model1.edmx";

EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();

EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);

#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OA.IDal;

namespace OA.IDBSession
{
    public partial interface IDBSession
    {
    <# foreach(EntityType entity in ItemCollection.GetItems<EntityType>()){#>
        I<#=entity.Name#>Dal <#=entity.Name#>Dal { get; set; }
    <#}#>
    }
}
View Code

2、Spring.Net重点说明,主要功能解决层之间的耦合。

  1)、引入spring.net

 

  2)、配置web.config

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
    </configSections>
  <spring>
    <!--spring.Net配置-->
    <context>
      <resource uri="file://~/Config/Service.xml"/>
    </context>
  </spring>
  <connectionStrings>
View Code

  新建文件夹Config/Service.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object type="OA.BLL.SYS_DEVICE_TABLEService, OA.BLL" 
          singleton="false" name="SYS_DEVICE_TABLEService" >
  </object>
</objects>

  完成以上步骤,即可在new对象的时候,进行解耦了。

  例如针对 public ISYS_DEVICE_TABLEService DeviceInfoService =new  SYS_DEVICE_TABLEService(); 进行解耦

        public DeviceInfoController() {
            IApplicationContext ctx = ContextRegistry.GetContext();
            DeviceInfoService = (ISYS_DEVICE_TABLEService)ctx.GetObject("SYS_DEVICE_TABLEService");
        }
        private MLedSystemEntities db = new MLedSystemEntities();
        public ISYS_DEVICE_TABLEService DeviceInfoService { get; set; }
View Code

 

转载于:https://www.cnblogs.com/jxsd/p/5007187.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值