C#中的类关联 [英] Class associations in C#

查看:137
本文介绍了C#中的类关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我是OOP的新手,正在尝试使用C#建立类关联.我有两个班级,分别是部门和员工.
我想让所有员工都集中在一个部门中,而在另一部门中.我已经在Google上搜索过,但很不幸.我不确定自己的做法是对还是错.

任何帮助,将不胜感激.请查看代码.在此先感谢

- - - - - - - - - - - - - -部门 - - - - - - - - - - - ----

Hi Everyone.
I am new to OOP and trying to make class association in C#. I have two classes called Department and Employee.
I want to get all the employees in one Department and the other way around. I have searched on Google but was unlucky.I am not sure that the way I am doing is right or wrong.

Any help would be appreciated. Please see the code. Thanks in advance

---------------------------Department--------------------------

public class Department
    {
        private int _DepartID;
        private string _DepartName;
        private string _DepartDescp;
       
       public int DepartmentID
        {
            get { return _DepartID; }
            set { _DepartID = value; }
        }             

        public string DepartmentName
        {
            get { return _DepartName; }
            set { _DepartName = value; }
        }      

        public string DepartDescription
        {
            get { return _DepartDescp; }
            set { _DepartDescp = value; }
        }
     
        public override string ToString()
        {
            return this.DepartmentID + " " + this._DepartName  +" " +   this.DepartDescription; 
        }

        List<employee> EmployeeList=new List<employee>();

        public  List<employee> Empoyees(Employee objEmp)
        {
            EmployeeList.Add(objEmp);
            return EmployeeList;
        }                   
                  
    }

------------------------------------员工-------- ----------------

------------------------------------Employee------------------------

public class Employee
   {
       private int _employeeID;
       private string _empFirstName;
       private string _empLastName;
       private string _empTitle;


       public int EmployeeID
       {
           get { return _employeeID; }
           set { _employeeID = value; }
       }

       public string EmpFirstName
       {
           get { return _empFirstName; }
           set { _empFirstName = value; }
       }

       public string EmpLastName
       {
           get { return _empLastName; }
           set { _empLastName = value; }
       }

       public string EmpTitle
       {
           get { return _empTitle; }
           set { _empTitle = value; }
       }

       public override string ToString()
       {
           return this.EmpTitle + " " + this.EmpFirstName + " " + this._empLastName + " " + this._employeeID.ToString ();
       }

   }

-------------------按钮后面的代码-------------------

-------------------Code behind button-------------------

Department obj_Depart = new Department();
Employee obj_Emp = new Employee();

obj_Emp.EmployeeID =1011;
obj_Emp.EmpTitle = "Mr";
obj_Emp.EmpFirstName = "Test1";
obj_Emp.EmpLastName = "Test2";

obj_Depart.DepartmentID = 1201;
obj_Depart.DepartmentName = "IT Department";
obj_Depart.DepartDescription = "Business center";

obj_Depart.Empoyees(obj_Emp);


listBox1.Items.Add(obj_Depart);

推荐答案

您有两种方法可以使用对象将员工和部门关联起来.一种是在Employee 对象中具有Department 属性,另一种是在Department 对象中具有Employee 对象的集合.您还将至少拥有所有Department 或所有Employee 对象的集合.

拥有的方式是,如果您具有所有Deparment 对象的集合,其中包含一个Employee 对象的集合,则可以使用LINQ获得所有Employee 对象.

You have two ways to associate Employees and Departments using objects. One is to have a Department property in the Employee object and the other is to have a collection of Employee objects in the Department object. You will also, at the minimum have either a collection of all Department or all Employee objects.

The way you have it, if you have a collection of all Deparment objects, which contain a collection Employee objects, you can get all Employee objects using LINQ.

var allEmployees = Departments.SelectMany(d => d.EmployeeList);



如果你想得到一个雇员的侮辱



if you want to get the deparment of an employee

var employeeDepartment = Departments.FirstOrDefault(d => d.Employees.FirstOrDefault(e => e == employee) != null));



但是,我认为您会发现,在Employee 类中具有Deparment 属性比在Department 类中具有集合要容易得多.

注意:使用id进行关联更像是一种关系数据概念,并且您正在使用C#进入对象世界.

您的问题还不清楚,所以我只是在猜测您想要的是什么.



However, I think you will find it easier to have a Deparment property in the Employee class than to have a collection in the Department class.

Note: Using id''s for associations is more of a relational data concept, and you are in the object world with C#.

Your question is a bit unclear, so I am only guessing at what you want.


好吧,我要采用的一种方法是在Employee类中添加"DepartmentID"属性(例如钥匙).将员工添加到列表< employee>首先使用"DepartmentID"为null或0,然后使用您的GUI,将部门分配给员工,即填充"DepartmentID"属性,仅此而已.您不需要具有List<>在数据类中,它们应仅包含单个实体的数据.操作应按业务类别分开.

一个部门可以有许多员工,但是一个雇员只能在一个部门中,这是一对多关系,而不是一对多关系(AFAIT).

希望这会有所帮助.

我真的不知道为什么我的主意会被否决的原因,除非您或任何被否决的人有更好的解决方案或主意.无论如何,这里有一些东西可以帮助您入门.以下代码显示了执行此操作的两种方法(到目前为止,这在我的世界中是最少的)-分层和未分层:

Well, the one way i would approach is add "DepartmentID" property in the Employee class (like a foreign key). Add Employee to a List<employee> initially with "DepartmentID" null or 0 and then using your GUI, assign a Department to the employees i.e. populate the "DepartmentID" property and that''s it. You don''t need to have List<> in the data classes, they should only contain data for the individual entity. Operations should be separated out to business classes.

One Department can have many EMployees but one Employee can be in only one Department, its one to many relation and not a many to many (AFAIT).

Hope this helps.

I really don''t see a reason why my idea would be downvoted unless you or whoever downvoted it had a better solution or idea. Anyway, here is a little something to get you started. The following code shows 2 ways of doing of doing it (atleast in my world so far) - layered and unlayered:

public class Department
    {
        public int DepartmentID { get; set; }

        public string DepartmentName { get; set; }

        public string DepartDescription { get; set; }
    }







public class Employee
    {
        public int EmployeeID { get; set; }

        public string EmpFirstName { get; set; }

        public string EmpLastName { get; set; }

        public string EmpTitle { get; set; }

        public int DepartmentId { get; set; }
    }





static void Main(string[] args)
        {
            //unlayered style
            List<Department> departments = new List<Department>();
            List<Employee> employees = new List<Employee>();

            Department d = new Department();
            d.DepartmentID = 1;
            d.DepartmentName = "xyz";
            d.DepartDescription = "abc";
            departments.Add(d);
            Department d1 = new Department();
            d1.DepartmentID = 2;
            d1.DepartmentName = "xyz2";
            d1.DepartDescription = "abc2";
            departments.Add(d1);

            Employee e;
            for (int i = 0; i < departments.Count; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    e = new Employee();
                    e.DepartmentId = departments[i].DepartmentID;
                    e.EmpFirstName = "add";
                    e.EmpLastName = "test";
                    e.EmployeeID = 123;
                    e.EmpTitle = "adf";
                    employees.Add(e);
                }
            }
            //querying for an employee name in a particular department
            string employeename = employees.Where(x => x.DepartmentId == departments.Where(y => y.DepartmentName == "xyz").Select(y => y).FirstOrDefault().DepartmentID).Select(x => x.EmpFirstName).FirstOrDefault();
            Console.WriteLine(employeename);
            
            //layered style - using Business Logic class to define an employee list prop which you can add to
            DepartmentBL dbl = new DepartmentBL();
            for (int i = 0; i < departments.Count; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    e = new Employee();
                    e.DepartmentId = departments[i].DepartmentID;
                    e.EmpFirstName = "add";
                    e.EmpLastName = "test";
                    e.EmployeeID = j+1;
                    e.EmpTitle ="adf" + (j+1).ToString();
                    dbl.EmployeeList.Add(e);
                }
            }

            Console.WriteLine("Employees added :" + dbl.EmployeeList.Count.ToString());


        }



Department和Employee数据类未定义任何操作,这些操作应该是单独的类(查找n层体系结构)的任务,在这种情况下为DepartmentBL(BL =业务层或逻辑).由于部门由"雇员组成,因此它公开了可以添加雇员"的雇员"列表属性.希望这在一定程度上有所帮助.



Department and Employee data classes don''t define any operation, that should be the task of a separate class (lookup n-tier architecture), in this case DepartmentBL (BL = Business Layer or Logic). It exposes an Employees list property that you can add Employees to, since a department "consists of" employees. Hope this helps to some extent.


这篇关于C#中的类关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆