Spring.net实例

首先

新建控制台应用,然后添加一系列的类

步骤

UserInfo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Spring.net
{
    public class UserInfo
    {
        public string Name { get; set; }
        public string Age { get; set; }
    }
}

IUserInfoDal

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Spring.net
{
    public interface IUserInfoDal
    {
        UserInfo UserInfo { get; set; }
        string Name { get; set; }
        void Show();
    }
}

AdoNetUserInfoDal

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Spring.net
{
    class AdoNetUserInfoDal : IUserInfoDal
    {
        public AdoNetUserInfoDal(string name, UserInfo userInfo)
        {
            Name = name;
            UserInfo = userInfo;
        }
        public string Name { get; set; }

        public UserInfo UserInfo { get; set; }

        public void Show()
        {
            Console.WriteLine("我是AdoNetDal,属性注入:Name" + Name);
            Console.WriteLine("UserInfo,Name" + UserInfo.Name + "Age=" + UserInfo.Age);

        }
    }
}

EFUserInfoDal

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Spring.net
{
    class EFUserInfoDal:IUserInfoDal
    {
        public EFUserInfoDal()
        {

        }
        public UserInfo UserInfo { get; set; }
        public string Name { get; set; }

        public void Show()
        {
            Console.WriteLine("我是EFDal,属性注入:Name" + Name);
            Console.WriteLine("UserInfo,Name" + UserInfo.Name + "Age=" + UserInfo.Age);

        }
    }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <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>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>

  <!--Spring.Net节点配置-->
  <spring>

    <context>
      <!--容器配置-->
      <resource uri="config://spring/objects"/>
      <!--xml文件方式,更改属性,复制到输出目录:始终复制-->
      <!--
      <resource uri="file://objects.xml"/>
      -->
      <!--嵌入程序集方式,assembly://程序集名/项目名/objects.xml,更改属性,始终复制,生成操作,嵌入的资源-->
      <!--
      <resource uri="assembly://Spring.Net/Spring.Net/objects.xml"/>-->
    </context>

    <objects xmlns="http://www.springframework.net">
      <!--这里放容器里面所有节点-->
      <description>An example that demostrates simple IoC features.</description>

      <!--name 必须要唯一, type=类的全名称所在的程序集-->
      <object name="UserInfoDal" type="Spring.Net.EFUserInfoDal,Spring.Net">
        <property name="Name" value="张三"/>
        <!--ref指向下面的属性注入-->
        <property name="UserInfo" ref="UserInfo"/>
      </object>

      <!--构造函数注入-->
      <object name="UserInfoDal2" type="Spring.Net.AdoNetUserInfoDal,Spring.Net">
        <constructor-arg index="0" value="张三"/>
        <constructor-arg index="1" ref="UserInfo"/>
      </object>

      <!--属性注入-->
      <object name="UserInfo" type="Spring.Net.UserInfo, Spring.Net">
        <property name="Name" value="李四"/>
        <property name="Age"  value="15"/>
      </object>


    </objects>
  </spring>

</configuration>

Program 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spring.Core;
using Spring.Context.Support;
using Spring.Context;
using Common.Logging;

//Spring.Aop.dll 面向切面编程
//Spring.Core.dll spring框架基础
//Common.Logging.dll 这个必须也要引用

namespace Spring.net
{
    class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            IUserInfoDal efDal = ctx.GetObject("UserInfoDal") as IUserInfoDal;
            efDal.Show();
            IUserInfoDal adoDal = ctx.GetObject("UserInfoDal2") as IUserInfoDal;
            adoDal.Show();
            Console.ReadKey();
        }
    }
}

实现

Tips

1.注意引用

using Spring.Context.Support;

using Spring.Context;

using Common.Logining;

2.在NuGet包管理器中添加Spring.core

最后

学习尚未深入,只能单纯实现,原理还在慢慢研究中......

 

文章参考此处

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
课程通过实际项目融入常用开发技术架构,讲授风格独特,提供详细上课日志及答疑,赠送配套的项目架构源码注释详细清晰且表达通俗,均能直接在实际项目中应用,正真的物超所值,价格实惠任务作业:综合运用《C#/.Net企业级系统架构设计实战精讲教程》课程所学知识技能设计一个学生成绩管理系统的架构。要求:1.系统基于MVC的三层架构,各层单独建不同的解决方案文件夹。2.采用Model First开发方式,设计架构时只需要设计学生表(TbStudent)和课程表(TbCourse)。学生表必须有的字段是ID、stuName、age;课程表必须有的字段是ID、courseName、content。3.数据访问层采用Entity Framework或NHibernate来实现,必须封装对上述表的增删改查方法。4.必须依赖接口编程,也就是必须要有数据访问层的接口层、业务逻辑层的接口层等接口层。层层之间必须减少依赖,可以通过简单工厂或抽象工厂。5.至少采用简单工厂、抽象工厂、Spring.Net等技术中的2种来减少层与层之间的依赖等。6.封装出DbSession类,让它拥有所有Dal层实例和SaveChanges方法。7.设计出数据访问层及业务逻辑层主要类的T4模板,以便实体增加时自动生成相应的类。8.表现层要设计相关的控制器和视图来验证设计的系统架构代码的正确性,必须含有验证增删改查的方法。9.开发平台一定要是Visual Studio平台,采用C#开发语言,数据库为SQL Server。10.提交整个系统架构的源文件及生成的数据库文件。(注意: 作业需写在CSDN博客中,请把作业链接贴在评论区,老师会定期逐个批改~~)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Geek-Banana

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值