如何将继承与返回的其他继承的属性映射?

发布于 2024-09-05 04:37:40 字数 578 浏览 7 评论 0原文

我有抽象类 Vehicle 和两个派生自的类:Car 和 ForkLift。

public abstract class Vehicle
{
    public EngineBase Engine { get; set; } 
}

public class Car : Vehicle
{
    public GasEngine Engine { get; set; }
}

public class ForkLift : Vehicle
{
    public ElectricEngine Engine { get; set; }
}

和引擎类:

public abstract class EngineBase
{
}

public class GasEngine : EngineBase
{
}

public class ElectricEngine : EngineBase
{
}

引擎通过“每个类层次结构的表”进行映射。对于车辆,我想使用相同的模式。

如何映射 Engine 类并使用该 Engine 属性派生?

如何使用延迟加载来做到这一点?

I have abstract class Vehicle and two classes that derive from: Car and ForkLift.

public abstract class Vehicle
{
    public EngineBase Engine { get; set; } 
}

public class Car : Vehicle
{
    public GasEngine Engine { get; set; }
}

public class ForkLift : Vehicle
{
    public ElectricEngine Engine { get; set; }
}

and Engine clasess:

public abstract class EngineBase
{
}

public class GasEngine : EngineBase
{
}

public class ElectricEngine : EngineBase
{
}

Engines are mapped with "table per class hierarchy". With Vehicles I want to use the same pattern.

How to map Engine class and derived with that Engine property?

How to do it with lazy-loading?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感悟人生的甜 2024-09-12 04:37:40

该代码无法编译,这使得您不可能映射它。

That code does not compile, which makes it improbable that you can map it.

两个我 2024-09-12 04:37:40

在 Vehicle 中使用受保护的字段并使用访问策略映射它:

public abstract class Vehicle
{
    protected Engine _engine;
}

在 Fluent NHibernate 中,这将被映射:

References(x => x.Engine, "EngineId").Access.CamelCaseField(Prefix.Underscore);

然后扩展 Vehicle 的类可以根据需要对其进行转换:

public class Car : Vehicle
{
    public GasEngine
    {
        get { return (GasEngine)_engine; }
        set { _engine = Value; }
    }
}

Use a protected field in Vehicle and map it with an access strategy:

public abstract class Vehicle
{
    protected Engine _engine;
}

In Fluent NHibernate this would be mapped:

References(x => x.Engine, "EngineId").Access.CamelCaseField(Prefix.Underscore);

Then the classes that extend Vehicle can cast it as needed:

public class Car : Vehicle
{
    public GasEngine
    {
        get { return (GasEngine)_engine; }
        set { _engine = Value; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文