Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-在MVC数据模型中引用对象_C#_Oop_Model View Controller_Reference - Fatal编程技术网

C#-在MVC数据模型中引用对象

C#-在MVC数据模型中引用对象,c#,oop,model-view-controller,reference,C#,Oop,Model View Controller,Reference,我正在用C#中的MVC模式构建一个软件。该软件有一个复杂的数据模型,包含许多类,并在运行时创建这些模型类的大量对象(由用户控制)。但是,一个类的对象(ParkingGarage)引用另一个类的对象(Car)-因此ParkingGarage对象存储对当前停放在特定车库中的所有Car对象(以数组形式)的引用 等级汽车 - Property (String) Model - Property (String) Color - Property (String) LicensePlate - Prop

我正在用C#中的MVC模式构建一个软件。该软件有一个复杂的数据模型,包含许多类,并在运行时创建这些模型类的大量对象(由用户控制)。但是,一个类的对象(
ParkingGarage
)引用另一个类的对象(
Car
)-因此
ParkingGarage
对象存储对当前停放在特定车库中的所有
Car
对象(以数组形式)的引用

等级
汽车

- Property (String) Model
- Property (String) Color
- Property (String) LicensePlate
- Property (String) Name
- Property (String) LocationAddress
- Property (Array) ParkedCars
停车库

- Property (String) Model
- Property (String) Color
- Property (String) LicensePlate
- Property (String) Name
- Property (String) LocationAddress
- Property (Array) ParkedCars
当现在将
汽车
对象存储在
停车库
对象的数组中时,我是否可以拥有一个充满
汽车
对象的数组,或者我应该只存储某种标识符(可能是
汽车
类的属性唯一标识符)


我目前的假设是:如果我将
Car
对象添加到
ParkingGarage
对象的ParkedCars数组中,该数组仅包含对Car对象的引用,对吗?我有什么问题吗?

将内存管理问题留给运行时处理,并使用通用列表类型
List
而不是数组来保存数据<代码>列表表示对象的可增长列表,并提供了
添加
删除
等方法,这些方法实际上在封面下操纵和排列T以产生所需的结果

这里有一些代码可以作为指导

public class Car
{
    public int Id { get; set;}
    public string Model { get; set; }
    public string Color { get; set; }
    public string LicensePlate { get; set; }

}

public class ParkingGarage
{
    private List<Car> parkedCars;

    public int Id { get; set; }
    public string Name { get; set; }
    public string LocationAddress { get; set; }

    public List<Car> ParkedCars
    {
        get { return parkedCars; }
    }

    public ParkingGarage()
    {
        parkedCars = new List<Car>();
    }

    public void AddCar(Car car)
    {
        // perform validation
        parkedCars.Add(car);
    }
}
公车
{
公共int Id{get;set;}
公共字符串模型{get;set;}
公共字符串颜色{get;set;}
公共字符串许可证牌{get;set;}
}
公共停车场
{
私家车;
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串位置地址{get;set;}
公共停车场
{
获取{返回停车场;}
}
公共停车场
{
parkedCars=新列表();
}
公共车辆
{
//执行验证
停放车辆。添加(车辆);
}
}
要从数据库检索汽车,可以使用存储库模式:

public class CarRepository
{
    public List<Car> FindAll()
    {
        // Code to retrieve all cars 
        List<Car> cars = new List<Car>();
        // Retrieve All rows from Cars Table and populate the List
        return cars;           
    }

    public List<Car> FindAllWith(string carModel)
    {
        // Code to retrieve all cars with a particular model
    }

    public List<Car> FindAllWith(int parkingGarageId)
    {
        // Code to retrieve all cars from a particular parking garage
    }

    public void Save(Car car)
    {
       // Code to save car to the database
    }

}
public类CarRepository
{
公共列表FindAll()
{
//检索所有车辆的代码
列出车辆=新列表();
//从Cars表中检索所有行并填充列表
返回车辆;
}
公共列表FindAllWith(字符串carModel)
{
//用于检索具有特定型号的所有汽车的代码
}
公共列表FindAllWith(国际停车场标识)
{
//从特定停车场检索所有车辆的代码
}
公共空间拯救(汽车)
{
//将汽车保存到数据库的代码
}
}

同样,不要担心内存管理,您最好集中精力为您的项目创建一个好的模型。

保留对对象集合(汽车)的引用是可以的,除非您面临特定的问题?一般来说,做最简单和最简单的事情是有益的,当然,如果你预见到问题,那么就考虑诸如懒散加载、过滤收集等等的事情。通常你会把汽车存放在某种商店里,并有一个仓库来获取你需要的,通过模型、制作等。我关心的是内存问题。因为可能有很多汽车对象,如果有一辆会被摧毁,但车库对象仍然保留着一个参考。我对C#相当陌生,因此不是内存管理专家。“存储/存储库”是什么意思?你能告诉我更多的细节吗?谢谢,但还有一个问题:
CarRepository
中的
List
不应该有属性或公共变量吗?我认为没有理由在存储库中有
List
属性。为什么这样认为?例如,如何使用
FindAll()
方法查找所有汽车对象?是否应该有一个包含所有
Car
对象的列表?首先创建存储库
CarRepository repo=new CarRepository()然后像这样填充汽车列表:
列出所有汽车=repo.FindAll()啊,好的,现在知道了。您假定我使用的是一个数据库或类似的数据库,它保存所有汽车对象,存储库将它们加载到
列表中
——就像数据库的镜像一样