Quick & Easy OR Mapping with Euss
Creating A Visual Studio .Net Solution
You can download the source code for this sample here.
Open Visual Studio and create a blank solution named EussDemo by clicking on the following menu items:
- File
- New Project
- Other Project Types
- Visual Studio Solutions
- Blank Solution
Generating The Domain Model Classes
The easiest way to store objects using Euss is to use the code generation tools shipped with it.
Code generation is based on XML file containing a description of the classes to generate.
There are two different ways to generate the classes with Euss:
- Using Visual Studio .Net
- Using a command line utility named domain.exe
Code Generation within Visual Studio .Net
Add a new Class Library named EussDemo.Domain to the solution by clicking on the following menu items:
- File
- Add
- New Project
- Visual C#
- Class Library
Remove the unnecessary Class1.cs file
Add a reference to Euss by clicking on the following menu items:
- Project
- Add Reference
- Evaluant Universal Storage Services for .Net 2.0
Add a new Euss model file named EussDemo.Domain.xml by clicking on the following menu items:
- Project
- Add New Item
- Euss Domain Model File
Type the following content and save the file.
<?xml version="1.0" encoding="utf-8" ?>
<?evaluant-application progid="EUSS.GenerationModel"?>
<Model xmlns="http://euss.evaluant.com/schemas/GenerationModel.xsd">
<Package name="EussDemo.Domain">
<Class name="Customer">
<Property name="Name" type="System.String" />
</Class>
<Class name="Order">
<Property name="Amount" type="System.Int32" />
</Class>
<Relationship type="composition">
<Parent name="Customer" role="Customer" multiplicity="1" />
<Child name="Order" role="Orders" multiplicity="*" />
</Relationship>
</Package>
</Model>
The code generation process is transparently launched as soon as you save this file.
Generating The Mapping File
A mapping file is an XML file containing the description of the links between the objects you want to store and a database schema where you will put those objects.
Mapping File Generation With The Command Line Utility
Open an Euss Command Prompt:
- Start
- Programs
- Euss
- Euss Command Prompt
Type this command:
mapping.exe /model:"c:\temp\EussDemo\EussDemo.Domain\EussDemo.Domain.xml" /out:"c:\temp\EussDemo\EussDemo.Domain\EussDemo.Domain.eum.xml"
A mapping file will be created in the same folder as the domain model file.
NB: If an error message is displayed, please check the location of the file.
The content of the file should be like this:
<?xml version="1.0" encoding="utf-8"?>
<Mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Entity type="EussDemo:Domain:Customer" table="Customer">
<Id field="CustomerId">
<Generator name="guid" />
</Id>
<Attribute name="Name" field="Name" db-type="String" size="50" />
<Reference name="Orders" entityChild="EussDemo:Domain:Order">
<Rule parentField="CustomerId" childTable="Order"
childField="FK_CustomerId" />
</Reference>
</Entity>
<Entity type="EussDemo:Domain:Order" table="Order">
<Id field="OrderId">
<Generator name="guid" />
</Id>
<Attribute name="Amount" field="Amount" db-type="Int32" />
</Entity>
</Mapping>
Creating A Console Application
Add a new Console Application named EussDemo.Test to the solution by clicking on the following menu items:
- File
- Add
- New Project
- Visual C#
- Console Application
Add a reference to Euss by clicking on the following menu items:
- Project
- Add Reference
- Evaluant Universal Storage Services for .Net 2.0
Add a reference to EussDemo.Domain by clicking on the following menu items:
- Project
- Add Reference
- Project
- EussDemo.Domain
Add a new configuration file named engines.config by clicking on the following menu items:
- Project
- Add New Item
- Euss Engine Configuration File
The sample content is based on an XML Provider. To be able to persist the data in a SQL Server database we have to change its content with this one:
<?xml version="1.0" encoding="utf-8"?>
<PersistenceEngines
xmlns="http://euss.evaluant.com/schemas/EngineConfiguration.xsd"
DefaultEngine="Sql">
<PersistenceEngine Name="Sql"
Factory=" Evaluant.Uss.SqlMapper.SqlMapperProvider">
<ConnectionString>Server=.;DataBase=uss;UID=sa;PWD=</ConnectionString>
<Dialect>Evaluant.Uss.SqlMapper.MsSqlDialect</Dialect>
<Driver>Evaluant.Uss.SqlMapper.MsSqlDriver</Driver>
<MappingFileName>~/EussDemo.Domain.eum.xml</MappingFileName>
<Metadata Type="model">~/EussDemo.Domain.xml</Metadata>
</PersistenceEngine>
</PersistenceEngines>
NB: You have to change the connection string and create the corresponding database in SQL Server.
The configuration tells the application to load the metadata and the mapping
from the file we created in the EussDemo.Domain project. We then have to copy those
file in the same folder as the compiled application. To do it, select those files in
Visual Studio .Net, and in the Properties window select Copy If Newer in Copy to Output Directory.
Repeat this operation with engines.config.
Copy this content in Program.cs and run the application:
using System;
using System.Collections.Generic;
using System.Text;
using EussDemo.Domain;
using Evaluant.Uss.ObjectContext;
namespace EussDemo.Test
{
class Program
{
static void Main(string[] args)
{
ObjectService os = new ObjectService("engines.config");
ObjectContext oc = os.CreateObjectContext();
oc.InitializeRepository(); // Creates all the tables in the database
Customer c = new Customer();
c.Name = "Eric";
Order o1 = new Order();
o1.Amount = 10;
Order o2 = new Order();
o2.Amount = 20;
c.Orders.Add(o1);
c.Orders.Add(o2);
oc.BeginTransaction();
oc.Serialize(c);
oc.CommitTransaction();
IList<Order> myOrders =
oc.Load<Order>(typeof(Customer), "[Name = 'Eric'].Orders");
foreach (Order o in myOrders)
Console.WriteLine(o.Amount);
}
}
}
In the database you can see that all the objects were persisted in specific tables: