내 C# pojects에 대해 NHibernate를 사용하고 있으므로 여러 모델 클래스가 있습니다.C#에서 가상 메서드를 호출하는 대신
using System;
namespace TestProject.Model
{
public class Room
{
public virtual int Id { get; set; }
public virtual string UniqueID { get; set; }
public virtual int RoomID { get; set; }
public virtual float Area { get; set; }
}
}
NHibernate에 지금까지 잘 작동으로 이러한 개체를 매핑 :
는 다음과 같은 예를 가정 할 수 있습니다. 이제 새 Room 객체를 생성하고 데이터베이스에 저장하려고합니다. 각 멤버를 seperatly로 설정하지 않으려면 모델 클래스에 새 생성자를 추가합니다. 이 잘못된 이유
"ConstructorShouldNotCallVirtualMethodsRule"
This rule warns the developer if any virtual methods are called in the constructor of a non-sealed type. The problem is that if a derived class overrides the method then that method will be called before the derived constructor has had a chance to run. This makes the code quite fragile.
This page도 설명하고 또한 그것을 이해 :의 FxCop 내 코드를 분석
이public RoomProperty()
{
}
public RoomProperty(int pRoomId, int pArea)
{
UniqueID = Guid.NewGuid().ToString();
RoomID = pRoomId;
Area = pArea;
}
다음 내게 말하길 : 가상 구성원 아래 나는 물품. 그러나 나는 문제를 해결하는 방법을 자극하지 않는다. 내가 NHibernate에 초기화 실패 becaue 내 aplication을 시작하지 못할 표준 생성자 호출 후
나는 모든 생성자를 지우고 다음과 같은 방법을 추가
...public void SetRoomPropertyData(int pRoomId, int pArea)
{
UniqueID = Guid.NewGuid().ToString();
RoomID = pRoomId;
Area = pArea;
}
는 .... 데이터를 설정합니다. 그것은 말합니다 :
그러나이 방법을 가상으로 설정하는 것은 생성자에서 가상 멤버를 설정했을 때와 같은 실수입니다. 이러한 실수 (위반)는 어떻게 피할 수 있습니까?
시공시 속성이 아닌 필드에 값을 설정하지 않는 이유는 무엇입니까? –
@ voroninp NHibernate가있는 필드에 쉽게 액세스 할 수 없습니다 – Andrey
내 모델에는 실제로 약 10 명의 멤버가 있으므로 qiet iften이라는 새 룸 개체를 만듭니다. 나는 각 속성을 seperatly로 설정하고 싶지 않다. – Metalhead89