当我想到这些东西时,我正在为我的应用程序创建对象。我知道使用DBML而不是手动创建类(参见下面的类)可以提高我的应用程序开发的速度,但我真的很困惑使用DBML的其他缺点和优点,而不是像我那样使用手工创建类。感谢所有愿意帮助的人。 :)
[Serializable]
public class Building
{
public Building()
{
LastEditDate = DateTime.Now.Date;
LastEditUser = GlobalData.CurrentUser.FirstName + " " + GlobalData.CurrentUser.LastName;
}
public int BuildingID { get; set; }
public string BuildingName { get; set; }
public bool IsActive { get; set; }
public DateTime LastEditDate { get; set; }
public string LastEditUser { get; set; }
public static bool CheckIfBuildingNameExists(string buildingName, int buildingID = 0)
{
return BuildingsDA.CheckIfBuildingNameExists(buildingName, buildingID);
}
public static Building CreateTwin(Building building)
{
return CloningUtility.DeepCloner.CreateDeepClone(building);
}
public static List<Building> GetBuildingList()
{
return BuildingsDA.GetBuildingList();
}
public static List<Building> GetBuildingList(bool flag)
{
return BuildingsDA.GetBuildingList(flag).ToList();
}
public static Building SelectBuildingRecord(int buildingId)
{
return BuildingsDA.SelectBuilding(buildingId);
}
public static void InsertBuildingRecord(Building building)
{
BuildingsDA.InsertBuilding(building);
}
public static void UpdateBuildingRecord(Building building)
{
BuildingsDA.UpdateBuilding(building);
}
public static void DeleteBuildingRecord(int building)
{
BuildingsDA.DeleteBuilding(building);
}
}
我的DAL是这样的:
internal static class BuildingsDA
{
internal static Building SelectBuilding(int buildingId)
{
SqlCommand commBuildingSelector = ConnectionManager.MainConnection.CreateCommand();
commBuildingSelector.CommandType = CommandType.StoredProcedure;
commBuildingSelector.CommandText = "Rooms.asp_RMS_Building_Select";
commBuildingSelector.Parameters.AddWithValue("BuildingID", buildingId);
SqlDataReader dreadBuilding = commBuildingSelector.ExecuteReader();
if (dreadBuilding.HasRows)
{
dreadBuilding.Read();
Building building = new Building();
building.BuildingID = int.Parse(dreadBuilding.GetValue(0).ToString());
building.BuildingName = dreadBuilding.GetValue(1).ToString();
building.IsActive = dreadBuilding.GetValue(2).ToString() == "Active";
building.LastEditDate = dreadBuilding.GetValue(3).ToString() != string.Empty ? DateTime.Parse(dreadBuilding.GetValue(3).ToString()) : DateTime.MinValue;
building.LastEditUser = dreadBuilding.GetValue(4).ToString();
dreadBuilding.Close();
return building;
}
dreadBuilding.Close();
return null;
}
....................
}
我还想知道两种OOP实现方法之间的速度是否更快,这要归功于:)
答案 0 :(得分:4)
<强> DBML 强>
优点:
缺点:
如果您手动创建类,则可能需要花费更多时间来编写代码,但您可以更灵活地使用它。您的客户端代码不依赖于您的DAL,DAL上的任何更改都不会导致客户端代码出现问题。
答案 1 :(得分:2)
手动创建模型类可以为属性添加其他属性(无法使用DBML完成),应用自己的数据验证(据我记得,可以使用部分方法使用DBML完成)。
对于许多表和关联,DBML可能变得难以阅读。
手动创建模型类的缺点是您必须完成所有DBML内容(属性和大量代码)。
如果要手动创建模型类,可以查看Entity Framework Code First或Fluent NHibernate。两者都可以轻松创建模型。