Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

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

4.77/5 (20 votes)
17 Oct 2019CPOL7 min read 81K   3.2K  
C# framework integrated with spring.net and ibatis.net

1. Introduction

Java projects have many good frameworks integrated with spring + ibatis. But in .NET projects, 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 titled this article "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's help, the developer can focus on the business logic development based on this framework.

2. Using the Code

I will explain the framework with the attached demo project. This is a very simple sample. The MS Access database file contains a 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, and following is the interface.

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

Second, we need a DataMapper class.

C#
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 the 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 the table, so here, you need to define another domainservice class.

C#
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.

Image 1

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

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

Get the service from the context.

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

Read all the records in the userinfo table:

C#
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:

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

Delete a record from the userinfo table:

C#
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. the framework provides the application multiple layers excluding the View layer. As a sample, you can use any view display you like.

Sample Image - maximum width is 600 pixels

Sample Running Requirements

  • Microsoft Access ODBC Driver
  • .NET 4.0

3. How the Sample Is Generated With the Tool

I created a source code generation 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 for 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 Source Code With Tool

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

Image 3

2. Config the Database Information

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

Image 4

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

3. Edit the Entity

If you want to edit the Entity detail, you can also click the "Edit Project Info" button on the MainWindow, and open a screen as shown below, this is an Excel file, you can edit the entity info in Excel format and update to database.

Image 5

4. Generate Source 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.

Image 6

4. Framework in Detail

4.1. Framework Directory Structure in Detail

Image 7

There are three level 1 directories: AutoGen/Customize/Resources

  • AutoGen

    The files are generated by the TOOL and there is no need to be edited by developer, will be placed in this directory. So the framework basic files, the domain entity define files, interface files will be placed under this directory. Under this directory, there are five sub-directories:

    1. Domain.Objects: Domain Entity classes
    2. Infrastructure: (Database) Infra operation classes
    3. Domain.Services: Domain service classes
    4. App.Services: App services classes
    5. Common: Framework common and utility classes
  • Customize

    Any class implemented or defined by developers, can be placed in this directory. This is the place where developers can customize their logic. And in this directory, the following four sub-directories are contained:

    1. Infrastructure: (Database) Infra operation classes
    2. Domain.Services: Domain service classes
    3. App.Services: App services classes
    4. Common: Framework common and utility classes
  • Resources

    This directory also contains AutoGen and Customize sub-directory. And similar meaning, all files in AutoGen can be generated by tool and there is no need to edit. And the developer can freely edit all files in Customize.

4.2. Framework Logic in Detail

  • Domain Object Layer

    Domain Object layer is the simplest layer, it contains all the entities classes. In fact, I am trying to build this framework to be adapted to the Domain Driven Design. So according to the conception in DDD, has the following interface design as the class diagram shows. But here is just a simple model, about the detail, you can refer to the Web-Site and extend the interface. And if you have any other ideas, you can also contact me and improve this framework. Anyway, this is just a simple layer, something of the entity classes.

    Image 8

  • Infrastructure Layer

    This is the layer that provides data operation functions to database. Or call it DAO. But in here, I am trying to design a uniform access interface base on the entity. So all the interfaces are summarized in the IDataContext interface file. In IDataContext, all the methods are generic methods and can adapt to different entities. So developer only needs to define the Ibatis SQL statement in XML config file, the framework will do all the mapping for you, then you can access the data in uniform interface, and you don't need to take care of every entity's mapping code. Of course, it's also possible for you to customize yourself data access interface if necessary.

    Image 9

  • Domain Service Layer

    This is also simple, as the following class diagram shows. IBaseDomainService also contains some uniform interface to access the data through the Infrastructure layer interface, but you can extend the interface by yourself freely. The target of this layer is to do simple operate integration to entities.

    Image 10

  • App Layer

    This layer is designed basing on DomainService layer and face to UI directly. You can implement any business logic in here freely. So there is no UI description in this framework. You can choose any UI layer design as you like. And this App layer will help you to separate the business logic from your UI.

    Image 11

  • Framework Context Common

    The four parts defined before, are almost isolated, you can use anyone in your project as you like. So this Framework Context Common layer tries to integrate the four parts together. So here, define the context for different layers, and integrate the log/exception process with Spring.net AOP.

    Image 12

Up to now, I have described this framework in different views in general. As I always said, this article is trying to provide a basic structure. There are many things that need to be improved, including the framework, the tool. In future, I will try to do these improvements. But I wish you can join this improvement or discussion if you are also interested in this framework.

And, I am also planning to write another article to explain how to use the tool with different database type in detail.

History

  • Version 1.0 edited on 2013/09/20

P.S.

This is only a very basic version. For any updates with the framework, please refer to this link. 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)