DotNet Application Framework (Spring.net + ibatis.net)

Datetime:2017-02-03 09:52:26         Topic: MyBatis  Spring          Share        Original >>
Here to See The Original Article!!!

1 Introduction

Java project has many good frameworks integrated with spring + ibatis. But in the .NET project, we can find few sample frameworks integrated with spring.net+ibatis.net. This article will provide a sample framework integrated with spring.net and ibatis.net. and focus on creating a uniform data access layer. Although I title this article with "integrate with spring and ibatis", the more important thing is to provide a robust, flexible framework.
In here, I also provide a help tool to generate this framework's basic coding files with simple configuration. With the tool help, then the developer can focus on the business logic development base on this framework.

2 Using the Code

I will explain the framework with the attached demo project. This is very simple sample. The MSAccess database file contains one table " userinfo ", and the table contains three columns ( FirstName , SecondName , FamilyName ), and this demo project provides the table data read / insert / delete functions.

2.1 Let's see how simple it is to operate the Users.mdb table.

First, we need to create an entity class, following is the interface.

public partial interface Iuserinfo:IBaseEntity
	{
		string FirstName{get;set;}
		string SecondName{get;set;}
		string FamilyName{get;set;}

	}

Second, we need a dataMapper class.

public partial class userinfoDataMapper : usersDataMapper<iuserinfo />
	{
	}
public partial class usersDataMapper<tentity />:BaseDataMapper<tentity /> where TEntity : class
	{
        public override ISqlMapper Mapper
        {
            get
            {
                if (mapper == null)
                    mapper = new InfrastructureFactory().GetSqlMapper("users");
                return mapper;
            }
            set
            {
                mapper = value;
            }
        }
    }

The key of data access layer is the BaseDataMapper class, this is an abstract and provides all the table insert / delete / read / update operations virtual methods, you don't need to implement any of these methods, what you need to do is only to inherit your dataMapper class from this BaseDataMapper class, then you can operate your table.

Third, in this framework, define a domain service layer to do some complex operation to table, so here you need to define another domainservice class.

public partial class userinfoService:BaseDomainService<Iuserinfo>,IuserinfoService
	{
		
	}

At last, as the title shows, this framework is integrated with ibatis.net and spring.net. So for accessing the table data, you need to create the ibatis config fragment.

These four steps are almost all the work you need to do when you add new table to your project.

Now let's see how to do the table data operate in application.

Get the service from the context.

IuserinfoService Service
        {
            get { return (IuserinfoService)DomainServicesContext.Context.GetObject("userinfoService"); }
        }

Read all the records in the userinfo table:

void ReadAllUsers()
        {
            var list=Service.GetList<iuserinfo />("",null);
            if (list.Count == 0) Console.WriteLine("There is no any user record.");
            foreach (var item in list)
            {
                Console.WriteLine("My Name is {0} {1} {2}", 
                item.FirstName, item.SecondName, item.FamilyName);
            }
        }

Add a new record to the userinfo table:

void AddNewUser()
        {
            Iuserinfo userInfo = new userinfo();
            userInfo .FirstName= Console.ReadLine();
            userInfo.FamilyName = Console.ReadLine();
            Service.Insert(userInfo);
        }

Delete a record from the userinfo table:

void DeleteOneUser()
        {
            Iuserinfo userInfo = new userinfo();
            userInfo.FirstName = Console.ReadLine();
            userInfo.FamilyName = Console.ReadLine();
            Service.Delete(userInfo);
        }

Above is what I want to do -- provide a uniform access model to application data layer.

2.2 Framework Structure in Image

The following screenshot shows the basic structure of this framework. Framework provides the application multiple layers excluding the VIEW layer. As the sample, you can use any view display as you like.

Sample Image - maximum width is 600 pixels

Sample Running Requirement

  1. Microsoft Access ODBC Driver
  2. .NET 4.0

3 Now, Let's See How the Sample is Generated with the Tool

I created a source code generate tool for this framework. With this tool, you only need to do some simple configuration, then the tool will help you to generate the C# code files of the framework. And currently, it will support the following databases:

  1. Microsoft Access
  2. IBM DB2
  3. MySQL

3.1 Help Tool Download

3.2 Manual for Generating src Code with Tool

1. Config your project basic information, and click the "Add New Project" button to create your project data:

2. Config the database information:

Click the "Edit Database Info" button to open a new window to config your database information:

Click the "Add" button to register your database information.

3. Generate src code

Now go back to MainWindow and click the "Auto Generate Src Files" button to generate basic framework source code files for your project.

The tool will create a zip file including all the files the framework requires. You only need to unzip it to local and can start your project's coding.

History

  • Version 1.0 edited on 2013/09/20

P.S.

This is only a very basic version. For any update with the framework, please refer to UPDATE.
If you have any questions or problems using this sample code or framework, please feel free to contact me, or raise the topic here. Any concerns are welcome.








New