Example

Connection

Also, you can put in app.config or web.config file
OpenErpService openErpService = new OpenErpService("http://192.168.11.129:8069", "XXX", "admin", "admin");


Parameters => OpenErp Url Server, Database name, User, Password

Model

In a later versión i want to use 4.5 entity framework data annotations, but i need to use 4.0 framework so i wrote a simple attribute
[OpenErpMap("hr.employee")]
    public class Employee
    {
        public DateTime Birthday
        {
            get;
            set;
        }

        [OpenErpMap("id")]
        public int id
        {
            get;
            set;
        }

        [OpenErpMap("identification_id")]
        public string IdentificationNumber
        {
            get;
            set;
        }

        [OpenErpMap("journal_id")]
        public int JournalId
        {
            get;
            set;
        }

        [OpenErpMap("name")]
        public string Name
        {
            get;
            set;
        }

        [OpenErpMap("user_id")]
        public int UserId
        {
            get;
            set;
        }

        public Employee()
        {
        }
    }

Read operations

   //Search for Employes where identificationNumber field starts with '1418'
   string id = '1418'
   IEnumerable<employee> Employees = openErpService.GetEntities<Employee>(e => e.IdentificationNumber.StartsWith(id));

   // A more complex search. Last Attendance from employee
   DateTime when = DateTime.Now;
   List<string> attendanceActionValues = new List<string>();
            attendanceActionValues.Add(Attendance.Sign_in);
            attendanceActionValues.Add(Attendance.Sign_out);
            Attendance lastAttendance = openErpService
                        .GetEntity<Attendance>(a => a.EmployeeId == employee.id 
                        && a.Date < when && attendanceActionValues.Contains(a.Action));
  //Current employee Sheet
Sheet currentSheet = openErpService.GetEntity<Sheet>(a => a.DateFrom <= when && when >= a.DateTo && a.EmployeeId == employee.id);

Create operations

currentSheet = new Sheet();
                DateTime date = when;
                currentSheet.DateTo = date;
                currentSheet.EmployeeId = employee.id;
                currentSheet.UserId = employee.UserId;
                openErpService.AddEntity<Sheet>(currentSheet);

Include method

Load objects properties. Only for read, update method only update entity, not related properties.
[OpenErpMap("hr.employee")]
class Employee
{
    property int UserId{get;set;}
    property User user{get;set;}
}

[OpenErpMap("res.user")]
class User
{
    property int Id{get;set;}
    property String Login{get;set;}
}

IEnumerable<Employee> employees = openErpService.GetEntities<Employee>(e => e.Name.Equals("Administrator")).Include(e => e.User);
//Load employees and populate User property with User object from OpenErp

Last edited Apr 14, 2014 at 8:18 PM by Jlob, version 1