DataServices如何返回类组合 [英] How DataServices returns a class composition

查看:80
本文介绍了DataServices如何返回类组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个项目中,我有以下阶级关系。 `Employee`和`Client`与`Company`有组合关系。所以实现如下。





In a project I have following class relationship. `Employee` and `Client` have a composition relationship with `Company`. So implemented this as follows.


class Company
{
    private Employee _Employee {get;set;} // private fields as composition
    private Client _Client {get;set;}

    public Company()
    {
        _Employee = new Employee();
        _Client = new Client();
    }

    public AddEmploees() //Employee objects are controlled by Company
    {
        //
    }

    public DeleteEmploees()
    {
        //
    }

    public AddClients() //Client objects are controlled by Company
    {
        //
    }

    public DeleteClients()
    {
        //
    }
}


class Employee
{
    string Name {get;set;}
    int ID {get;set;}
    string Address {get;set;}
    string Department  {get;set;}
    DateTime DOB {get;set;}

    private Employee() // constructor private
    {
    }
}


class Client
{
    string CID {get;set;}
    string Name {get;set;}
    string Type {get;set;}
    DateTime StartDate {get;set;}
    string Address {get;set;}

    private Client() // constructor private
    {
    }
}







当我想要显示`client` /`employee`的详细信息时UI我的`DataService`应该返回一个`Company`对象而不是返回`Employee / Client`对象,因为关系是composit离子。所以我可以在我的`DataService`中使用类似`GetDetails()`的方法,然后从数据库中获取所需的详细信息,以分配`Employee`和`Client`的属性。但现在问题是,我将无法访问`Company`对象的私有字段(`_Employee`,`_ Client`)来设置属性的值如下





公共公司GetDetails()

{

公司公司=新公司();

string selectStatement =SELECT ...;

//从数据库获取数据

company.client.name = rdr [name]。value; //这是不可能的。







返回公司;

}



虽然我几乎没有想法解决这个问题,但是他们似乎没有适应这种阶级关系(构成)或违反关注点分离原理。感谢你在这方面的帮助吗?




When I want to show `client` / `employee` details on the UI my `DataService` is supposed to return a `Company` object rather than returning `Employee/Client` objects as the relationship is composition. So I could have a method like `GetDetails()` in my `DataService` and then get the required details from the database to assign for properties of `Employee` and `Client`. But now the problem is, I will not be able to access private fields (`_Employee` , `_Client`) of `Company` object to set values for the properties as follows


public Company GetDetails()
{
Company company = new Company();
string selectStatement = "SELECT...";
// Get data from DB
company.client.name = rdr["name"].value; // This is not possible.
.
.
.
return company;
}

Though I have few ideas to sort out this problem but non of them are seems adaptable to this class relationship (composition) or either violating separation of concerns principle. Appreciate your help in this regard?

推荐答案

让你的财产公开不公开

Make your property public not private
public Client _Client {get;set;}





或添加addtional属性有公共访问权限



or add addtional property With public access


这篇关于DataServices如何返回类组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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