C# - 在 MVC 数据模型中引用对象
C# - Referencing objects in MVC data model
我正在使用 C# 中的 MVC 模式构建一个软件。该软件有一个复杂的数据模型,其中包含许多 classes,并在运行时创建这些模型 classes 的大量对象(由用户控制)。但是,一个 class (ParkingGarage
) 的对象引用了另一个 class (Car
) 的对象 - 所以 ParkingGarage
对象存储了对所有对象的引用Car
个对象(在数组中),当前停在特定车库中。
Class Car
- Property (String) Model
- Property (String) Color
- Property (String) LicensePlate
Class ParkingGarage
- Property (String) Name
- Property (String) LocationAddress
- Property (Array) ParkedCars
当现在将 Car
个对象存储在 ParkingGarage
个对象的数组中时,我可以只拥有一个充满 Car
个对象的数组,还是应该只存储某种类型的对象?标识符(可能是 Car
class 的 属性 uniqueIdentifier)?
我目前的假设是:如果我将 Car
对象添加到 ParkingGarage
对象的 Array ParkedCars 中,该数组只包含对 car 对象的引用,对吧?我对此有任何疑问吗?
将内存管理问题留给 runtime 并使用通用列表类型 List<>
而不是 array 来保存你的数据。 List<T>
表示一个 可增长的对象列表 并提供诸如 Add
和 Remove
之类的方法,这些方法实际上在幕后操纵 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);
}
}
要从您的数据库中检索汽车,您可以使用存储库模式:
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
}
}
再次重申,不要担心内存管理,你最好专注于为你的项目创建一个好的模型。
我正在使用 C# 中的 MVC 模式构建一个软件。该软件有一个复杂的数据模型,其中包含许多 classes,并在运行时创建这些模型 classes 的大量对象(由用户控制)。但是,一个 class (ParkingGarage
) 的对象引用了另一个 class (Car
) 的对象 - 所以 ParkingGarage
对象存储了对所有对象的引用Car
个对象(在数组中),当前停在特定车库中。
Class Car
- Property (String) Model
- Property (String) Color
- Property (String) LicensePlate
Class ParkingGarage
- Property (String) Name
- Property (String) LocationAddress
- Property (Array) ParkedCars
当现在将 Car
个对象存储在 ParkingGarage
个对象的数组中时,我可以只拥有一个充满 Car
个对象的数组,还是应该只存储某种类型的对象?标识符(可能是 Car
class 的 属性 uniqueIdentifier)?
我目前的假设是:如果我将 Car
对象添加到 ParkingGarage
对象的 Array ParkedCars 中,该数组只包含对 car 对象的引用,对吧?我对此有任何疑问吗?
将内存管理问题留给 runtime 并使用通用列表类型 List<>
而不是 array 来保存你的数据。 List<T>
表示一个 可增长的对象列表 并提供诸如 Add
和 Remove
之类的方法,这些方法实际上在幕后操纵 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);
}
}
要从您的数据库中检索汽车,您可以使用存储库模式:
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
}
}
再次重申,不要担心内存管理,你最好专注于为你的项目创建一个好的模型。