Click here to Skip to main content
Click here to Skip to main content

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

, 11 Nov 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

I created a video, and tried to pulish here, but it seems video is no supported. So please click following link to view the video of creating Demo2.

         Video Link

 

2 Using the Code

I will explain the framework with the attached demo project. This is 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.

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 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 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 is 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.

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

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. 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 following, this is a excel file, you can edit the entity info in excel format and update to database.

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.

4 Framework in Detail  

4.1 Framework directory structure in detail 

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

  • AutoGen:

    The files generate by the TOOL and no need to be edited by developer, will be place in this directory. So the framework basic files, the domain entity define files, interface files will be place under this directory. Under this directory, there are four  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 developer, can be placed in this directory. This is the place where developer can customize their logic. And in this directory, also contains following four sub-directories.

    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 no need to be edit. And all files in Customize, developer can edit it freely. 

4.2  Framework logic in detail 

  • Domain Object Layer

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

  • Infrastructure Layer

    This is the layer provide data operation functions to database. Or call it DAO. But in here, I trying to design a uniform access interface base on the entity. So all the interface are summaried in the IDataContext interface file. In IDataContext, all the methods are generic method and can adapt to different entities. So developer only need 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 don't need to take care the every entity's mapping code. Of course, it's also possible for you to customize yourself data access interface if necessary.

  • Domain Service Layer

    This is also simple, as 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 enities.

  • 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.

  • 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 part together. So here define the context for different layers, and integrated the log/exception process with Spring.net AOP.

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

And, I am also plan 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)

Share

About the Author

Changqin Liu
Systems Engineer NGIC
Japan Japan
No Biography provided

Comments and Discussions

 
QuestionVery good PinmemberVolynsky Alex8-Nov-13 20:38 
AnswerRe: Very good PinprofessionalChangqin Liu9-Nov-13 0:44 
GeneralRe: Very good PinmemberVolynsky Alex9-Nov-13 0:51 
GeneralMy vote of 5 Pinmemberfredatcodeproject8-Nov-13 6:24 
GeneralRe: My vote of 5 PinprofessionalChangqin Liu9-Nov-13 0:45 
QuestionHas the infrastucture of DataBase Acess use DAO Object ? PinmemberToater24-Oct-13 20:06 
AnswerRe: Has the infrastucture of DataBase Acess use DAO Object ? PinprofessionalChangqin Liu24-Oct-13 21:29 
QuestionSupport Sql Server and ORACLE Pinmemberkiquenet.com14-Oct-13 22:30 
AnswerRe: Support Sql Server and ORACLE PinprofessionalChangqin Liu14-Oct-13 22:44 
GeneralRe: Support Sql Server and ORACLE Pinmemberkiquenet.com18-Nov-13 19:57 
GeneralRe: Support Sql Server and ORACLE PinprofessionalChangqin Liu18-Nov-13 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 12 Nov 2013
Article Copyright 2013 by Changqin Liu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid