Kanas.net Quick Start

Source: Internet
Author: User

The weather in Guangzhou is no longer as hot as it was in the autumn of September. It is the best choice of the year to see the beautiful scenery of kanas in this season. If you do not have the time and budget to go To the beautiful kanas, please enter with meKanas.netAlthough it is not as romantic as going to kanas, it is also a second choice.

Kanas.net frameworkIt is a very easy-to-use persistent layer framework. If you have a very good object-oriented basis and use this framework to achieve transparent access to data, you should be satisfied. If you question my conclusion, start this small example with me. I believe you will share the same conclusion with me.

Step 1: Download the latest version of kanas.net frameworkAnd install it in the system.

Kanas.net frameworkIs a very small framework, the content installed into the system includes the coreSource codeAnd all documents will not exceed 6 m . We recommend that you disable it during installation.Microsoft Visual studio.net 2003Because you needIDEInstall a small plug-in to help you automaticallyErmThe document is automatically converted to the object type SourceCode.

A small description is required here. The installation package you downloaded from the blog garden only contains the core source code and some test code,MsdnStyle help documentation,Vs. netPlug-in. ForKanas. netThe framework is sufficient for users. A more complete installation package includes a visualization tool(From a variety of database models,VisioDatabase Design Model andPowerdesignerImport data directly to the Database Design ModelKanas. netIn the object link model), A groupPowerPoint/WordTutorial and a series of extension packages, due to copyrightLicenseThe issue is not yet open and is only released within the company.

Step 2:If your system is installedVs. net, You can directly open the shortcut after installationKanas. net. SDKSolution, then you can see the five projects included in the solution:

  • Kanas. utils: PracticalProgramProject. I have written an article about this project.Article.
  • Kanas. Common: Infrastructure Project. This project is slightly different from the version released by our company. The latter has added three important infrastructure: Based onXMLOfDomArchitecture, based onXMLConfiguration architecture and dataset export architecture.
  • Kanas. Framework: Persistent layer project. Contains all persistence services.
  • Kanas. Contacts: Contact project. This project is selected fromMicrosoft OfficeOfAccessSample DatabaseContact. Although the database is very simple and contains very few relationships, it can be used as an entry-level question to avoid many complicated problems. In this project, we can see allKanas.net frameworkProject, how to construct a configuration environment, how to generate relevant business code through the entity relationship model, and easily solve complex business problems in the business module.
  • Kanas. Test: Test item. This project is a unit test project, if your system is installedTestdriven.netYou can evenNunitOfGuiDirectRunI believe you can see all the green lights.

Step 3:Do not rush to study the first three projects and focus on them.Kanas. ContactsProject. This is the most important project in the entire solution. This project demonstratesKanas. netSeveral very useful configuration or extension functions:

    • Generate entity code through object link model
    • Initialize the business environment through Configuration
    • Custom database providers
    • Custom Constraints
    • Custom constraint sub-database implementation

First, let's take a look.ErModel. InKanas. ContactsThere isContact. ErmFile, the creation operation is "NONE", but the custom tool"Kanascodegen". Please refer toHtml/XMLEditor "to open this document, you can immediately see the entire entity-link model, in addition to tables and fields in the database model, but also some additional content, such as the domain conceptual model.

ErThe root element of the model is"Ermodel", Which specifies some main attributes:

    • Description: Model description.
    • Namespace: The namespace for generating code.
    • Codelang: Language used to generate code.CSHARPYesC #,VBYesVB. NET.
    • Workingpath: The working path, that is, the path to generate the code file. This property does not control the code generation plug-in.Vs. netThe plug-in always generates codeErmThe directory where the document is located.
    • Layout: Generate code layout. The optional items areSingle (Generate a single code file),Chunk (Each type generates a separate code file)AndBuild (Directly compiled as an assembly without generating code files). This property does not control the code generation plug-in.Vs. netPlug-ins always generate code into a single file.

ContactThe project does not include enumeration. ThereforeEnumsDoes not contain any elements. In my other projectPMCBecause there are many options in defect management, there are actually more than 10 items.

EntitiesThis section contains all objects. Each object elementEntityidA series of attributes at the beginning of the attribute,Name (Type name),Description (Type description),Aggregated (Whether to build a set type),Mapping (Name of the table mapped to the database). EachEntitySection contains severalPropertyElement, eachPropertyElements includeName (Attribute name),Type (Attribute type),Description (Attribute description),Mapping (Field name mapped to the database).

Click the show all files button in the toolbar of Solution Explorer.Contact. ErmThere isContact. CSDocumentation. Open this document to view the code of all entities.

Now let's try to modify it.ErmDocumentation to see if the generated source code can be automatically synchronized.

1.OpenContact. CSUnit, locateContact class, You can seeCILSProperty, indicating "getting calls together ":

/**/ /// <Summary>
///Obtain telephone Aggregation
/// </Summary>
[Mappedcollection ( Typeof (Callcollection ), Typeof (CALL ), 1 )]
Public Callcollection CILS
{
Get
{
Return(Callcollection )(This. Ensuredcell. getcollection ("CILS")));
}
}

This property is a typical one-to-many aggregation relationship. It collects all phone records of the contact. The reason for generating this property is thatContact. ErmDocumentCallEntityContactTheAggregated attribute. Try to delete this property and save itErmDocumentation. ReturnContact. CSPage, you will findContact. cballsThe property is missing. Let's modify it again.ErmDocument, inCallEntityContactAdd an attribute after the attribute element:

Aggregated = "CILS"

And saveErmDocumentation. ReturnContact. CSPage, you will findContact. cballsThe property is returned.

ErmDocument is a key document in the entire business system. This document can be used to implement a series of functions on the deployment, such as rebuilding the database.

Next, let's take a look at how to initialize the business environment. AccordingKanas.netTutorial, useKanas.netProvidedAdaptersGlobal object orAppcontextTo initialize the business environment:

    • Create a database engine by registering a data adapter or data provider;
    • You can bind a business type to a specified data adapter to obtain the access method for each business type.
    • Install the service catcher to interfere with data loading and persistence.
    • You can install the app context widget to register the working part bound to the app context.

HoweverKanas. ContactsThe above method is not used in the project, but is implemented by configuration.

InKanas. ContactsThe most important unit in the project isServices. CS. In this unitServicesClass is the "Guiding class" of the entire business module. This type can be used to initialize and persist services in all business environments.This type of constructor is private, so this class must be obtained through the static factory method. This type has a static method and a static attribute. The former is initialized by specifying the path of the configuration file, and the latter is obtained through the static attribute.(Singleton)Instance.

AnalysisServicesClass constructor, you can find that,ServicesPassKanas. utils. configloaderTo initialize the business environment through the configuration file. Open the configuration fileKanas. Contacts. configIt can be seen that the user can freely control the entire initialization process.

Note: In the configuration fileAdapterSection, with one line of ConfigurationLogger. Normally, similar

Logger = "logfile:./logs/contacts. Log"

This configuration is used to write database logs to a specified file. However, in this project, the User-Defined logger is used for the purposeNunitOfGuiThe second is to demonstrate how to customize the logger or connect to other log frameworks.

although kanas. net Framework provides oledbprovider for access access database, however, kanas. the contacts project uses a custom mdbprovider database provider. There are two purposes: first, the oledbprovider constructor ADO. net change the connection string parameter to MDB file name parameter; the second is to demonstrate how to customize Based on ADO.. Net .

Kanas. ContactsThe project also defines a constraint subitem. This constraint Sub-item is used to describe the month and day of the date attribute specified by the object, which is the same as the month and day of the given date. The sub-idea of this constraint is based on a very reasonable need: Find all contacts who have their birthdays on a certain date. FollowKanas.netTo solve this problem at the database level, you can only discard this constraint to retrieve all the data, in the instantiation stage, compare the corresponding attributes with the given date to determine whether the instance needs to be loaded. Because any constraint must be implemented.GetdataselectedMethod. Obviously, this method will cause a great loss of performance, becauseAccessThe access engine providesMonth (birthdate) andDay (birthdate). As a resultPropertiedconstraintDerived fromDayconstraintAnd implementsDayconstraintOfCriteriabuilderInInitAdd thisCriteriabuilderIn the end, this constraint is implemented through database queries rather than runtime matching.

Step 4:Run the unit test.

InKanas. TestProject, there isTestbaseServes as the base class for all test classes. The advantage of setting up this base class is the shared and unified initialization process. From thisTestbaseClass derived from four test classes:

Basic: Basic test. The test contains three test cases:

    • Insert contact
    • Delete contact
    • Modify contact attributes.

Loadingtest: Load test. The test contains seven test cases:

    • Basic Loading
    • Load married contacts
    • Load by string Constraints
    • Load by nested Constraints
    • Subload based on the Anti-reference date type Constraint
    • Lazy Loading
    • Load by birthday

Aggreatedtest: Aggregation Test. This test contains a test case:

    • Testing of counting the number of contacts by Contact Type

Datatablebuildtest: Test Data Table Generation.

    • Basic View Test
    • View test with quick query
    • View test with nested quick query

 OK,Come on,Good lucky!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.