Esempio n. 1
0
        static void Main(string[] args)
        {
            //PostCode pc = new PostCode("KA1 1RE");
            //Console.WriteLine(pc.FullCode);

            EmployeeMapper em = new EmployeeMapper();

            // Get the same employee twice
            Employee emp1 = em.GetById(1);
            Employee emp2 = em.GetById(1);
            // check that the same object is returned by each call
            bool sameobject1 = emp1.Equals(emp2);

            // Get all hourly paid employees
            List<Employee> hpes = em.GetAllHourlyPaid();
            bool sameobject2 = emp1.Equals(hpes[0].Supervisor);
            bool sameobject3 = hpes[0].Equals(hpes[1].Supervisor);

            // Create and store a new hourly paid employee
            HourlyPaidEmployee newhpe = new HourlyPaidEmployee();
            Address newaddress = new Address("Entity Park", 100, new PostCode("KA1 1BX"));
            newhpe.Address = newaddress;
            newhpe.Name = "Michael";
            newhpe.Username = "******";
            newhpe.PhoneNumber = "2222";
            newhpe.Supervisor = emp1;

            int newID = em.StoreHourlyPaid(newhpe);
            Employee newEmp = em.GetById(newID);

            // wait for key press before ending
            Console.ReadLine();
        }
        /// <summary>
        /// gets all hourly paid employees
        /// with associated address, postcode and supervisor objects
        /// </summary>
        /// <returns>list of employees</returns>
        public List<Employee> GetAllHourlyPaid()
        {
            List<Employee> result = new List<Employee>();

            StringBuilder sqlb = new StringBuilder(
                    "SELECT employeeid,name,username,supervisor, phonenumber, propertyname, propertynumber, postcode ");
            sqlb.Append("FROM EMPLOYEES e, ADDRESSES a ");
            sqlb.Append("WHERE e.addressID = a.addressID ");
            sqlb.Append(@"AND discriminator = 'h'");
            string sql = sqlb.ToString();

            SqlCeCommand comm = new SqlCeCommand(sql, conn);
            if (conn.State == ConnectionState.Closed) conn.Open();
            SqlCeDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);

            while(reader.Read())
            {
                Employee emp = null;

                int employeeID = reader.GetInt32(0);
                string name = reader.GetString(1);
                string username = reader.GetString(2);
                int supervisor = -1;
                if(!reader.IsDBNull(3))
                    supervisor = reader.GetInt32(3);
                string phonenumber = reader.GetString(4);
                string propertyname = reader.GetString(5);
                int propertynumber = reader.GetInt32(6);
                string postcode = reader.GetString(7);

                if (identityMap.ContainsKey(employeeID))
                {
                    emp = identityMap[employeeID];
                }
                else
                {
                    PostCode pc = new PostCode(postcode);
                    Address ad = new Address(propertyname, propertynumber, pc);
                    emp = new HourlyPaidEmployee(employeeID, name, username, ad, phonenumber);
                    emp.Supervisor = GetById(supervisor);
                    identityMap.Add(employeeID, emp);
                }
                result.Add(emp);
            }

            //conn.Close();

            return result;
        }
Esempio n. 3
0
        static void SetupObjects()
        {
            // EMPLOYEES
            Employee fernando = new SalariedEmployee
            {
                EmployeeID = 1,
                Name = "Fernando",
                Username = "******",
                PhoneNumber = "9999",
                PayGrade = 7
            };
            Employee felipe = new HourlyPaidEmployee
            {
                EmployeeID = 2,
                Name = "Felipe",
                Username = "******",
                PhoneNumber = "8888",
                Supervisor = fernando
            };
            Employee nico = new HourlyPaidEmployee
            {
                EmployeeID = 3,
                Name = "Nico",
                Username = "******",
                PhoneNumber = "7777",
                Supervisor = felipe
            };

            // ADDRESSES
            Address ormHouse = new Address
            {
                PropertyName = "ORM House",
                PropertyNumber = 1,
                PostCode = "G4 0BA",
                Employees = new ArrayList4<Employee> { nico }           // db4o activateable collections
            };
            Address linqTower = new Address
            {
                PropertyName = "LINQ Tower",
                PropertyNumber = 9,
                PostCode = "KA1 1XX",
                Employees = new ArrayList4<Employee> { fernando, felipe }
            };

            // SET ADDRESSES FOR EMPLOYEES
            fernando.Address = linqTower;
            felipe.Address = linqTower;
            nico.Address = ormHouse;

            // PROJECTS
            Project webShop = new Project
            {
                ProjectID = 1,
                ProjectName = "Web Shop",
                Employees = new ArrayList4<Employee> { felipe, nico }
            };
            Project financeSystem = new Project
            {
                ProjectID = 2,
                ProjectName = "Finance System",
                Employees = new ArrayList4<Employee> { fernando }
            };
            Project secret = new Project
            {
                ProjectID = 3,
                ProjectName = "Secret",
                Employees = new ArrayList4<Employee> { fernando, felipe }
            };

            // SET PROJECTS FOR EMPLOYEES
            fernando.Projects = new ArrayList4<Project> { financeSystem, secret };
            felipe.Projects = new ArrayList4<Project> { webShop, secret };
            nico.Projects = new ArrayList4<Project> { webShop };

            // OBJECT GRAPH
            IList<Employee> employees = new ArrayList4<Employee> { fernando, felipe, nico };

            // STORE OBJECT GRAPH
            string dbFileName = GetDbFileName();

            using (IObjectContainer db = Db4oEmbedded.OpenFile(dbFileName))
            {
                foreach (Employee emp in employees)
                {
                    db.Store(emp);
                }
            }
        }
        /// <summary>
        /// gets specified employee with associated address and postcode objects
        /// </summary>
        /// <param name="id">the employee id</param>
        /// <returns>an employee object</returns>
        public Employee GetById(int id)
        {
            Employee result = null;

            // check whether target object is already loaded
            if (identityMap.ContainsKey(id))
            {
                result = identityMap[id];
            }
            else
            {
                StringBuilder sqlb = new StringBuilder(
                    "SELECT employeeid,name,username,phonenumber, discriminator, paygrade, propertyname, propertynumber, postcode ");
                sqlb.Append("FROM EMPLOYEES e, ADDRESSES a ");
                sqlb.Append("WHERE e.addressID = a.addressID ");
                sqlb.Append("AND employeeID = ");
                sqlb.Append(id.ToString());
                string sql = sqlb.ToString();

                SqlCeCommand comm = new SqlCeCommand(sql, conn);
                if(conn.State==ConnectionState.Closed) conn.Open();
                SqlCeDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);

                if (reader.Read())
                {
                    int employeeID = reader.GetInt32(0);
                    string name = reader.GetString(1);
                    string username = reader.GetString(2);
                    string phonenumber = reader.GetString(3);
                    string discriminator = reader.GetString(4);
                    int paygrade = 0;
                    if(!reader.IsDBNull(5))
                        paygrade = reader.GetInt32(5);
                    string propertyname = reader.GetString(6);
                    int propertynumber = reader.GetInt32(7);
                    string postcode = reader.GetString(8);

                    PostCode pc = new PostCode(postcode);
                    Address ad = new Address(propertyname, propertynumber, pc);

                    if (discriminator.Equals("S"))
                    {
                        // need to include pay grade in database schema and adjust query
                        result = new SalariedEmployee(employeeID, name, username, ad, phonenumber, paygrade);
                    }
                    else if (discriminator.Equals("H"))
                    {
                        result = new HourlyPaidEmployee(employeeID, name, username, ad, phonenumber);
                    }
                    else
                    {
                        throw new Exception("Invalid employee type");
                    }
                }

                //conn.Close();
                identityMap.Add(id, result);
            }
            return result;
        }
 //CONSTRUCTORS
 /// <summary>
 /// constructor for HourlyPaidEmployee 
 /// </summary>
 /// <param name="employeeId">the employee's id number</param>
 /// <param name="name">the employee's name</param>
 /// <param name="username">the employee's username</param>
 /// <param name="phoneNumber">the employee's phone number</param>
 public HourlyPaidEmployee(int employeeId, string name, 
     string username, Address address,string phoneNumber)
     : base(employeeId, name, username, address, phoneNumber)
 {
 }
Esempio n. 6
0
        static void Main(string[] args)
        {
            // set database initializer
            Database.SetInitializer<CompanyContext>(new CompanyContextInitializer());

            // initialise EF Profiler
            HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize();

            CompanyContext db = new CompanyContext();

            // Create Address objects
            Address ad1 = new Address
            {
                AddressId = 3,
                PropertyName = "LINQ Tower",
                PropertyNumber = 99,
                PostCode = "G1 1XX"
            };

            Address ad2 = new Address
            {
                AddressId = 4,
                PropertyName = "EF House",
                PropertyNumber = 101,
                PostCode = "G4 0XX"
            };

            // Create Employee objects and associate with Addresses
            Employee emp1 = new Employee
            {
                Name = "Fernando",
                Username = "******",
                PhoneNumber = "1234",
                Address = ad1
            };

            SalariedEmployee emp2 = new SalariedEmployee
            {
                Name = "Felipe",
                Username = "******",
                PhoneNumber = "5678",
                Address = ad2,
                DepartmentName = "Sales",                   // set FK property
                PayGrade = 3
            };

            // Create Department object
            Department dep1 = new Department
            {
                DepartmentName = "Sales"
            };

            // Add Employee objects to Department
            dep1.Employees.Add(emp1);
            //dep1.Employees.Add(emp2);

            db.Departments.Add(dep1);
            //db.Employees.Add(emp1);
            db.Employees.Add(emp2);
            db.SaveChanges();

            // dispose context and open a new context
            db.Dispose();
            db = new CompanyContext();

            // reload entity from database
            //db.Entry(dep1).Reload();

            db.Configuration.LazyLoadingEnabled = false;
            db.Configuration.ProxyCreationEnabled = false;

            List<Employee> employees = db.Employees
                .Include(e => e.Address)
                .Include(e => e.Department)
                .ToList<Employee>();

            Address address = employees[0].Address;

            var query = from d in db.Departments
                        where d.DepartmentName == "Marketing"
                        select d;

            //string command = query.ToString();
            //Console.WriteLine(command + "\n");

            Department department = query.FirstOrDefault();

            // set break point here and inspect objects with debugger
            Console.WriteLine("Press any key to finish...");
            Console.ReadLine();
        }