使用安全的向下转换为一流的设计方案 [英] Options for class design using safe downcasting

查看:179
本文介绍了使用安全的向下转换为一流的设计方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与向下转换挣扎后(见 [我的原帖] ),并制作<一个href=\"http://stackoverflow.com/questions/7276507/serializable-classes-and-dynamic-proxies-in-ef-how\">[deep副本] ,我发现<一个href=\"http://stackoverflow.com/questions/5381690/options-for-class-design-using-safe-downcasting\">[this同名文章] ,其中一个建议是用C ++做就如何处理这一问题。有了太多的兴奋,我实现它在C#中如下:

After struggling with downcasting (see [my original post]) and with making [deep copies], I found [this eponymous article] where a suggestion is made in C++ as to how to deal with the issue. With much excitement I implemented it in C# as follows:

public partial class User
{
virtual public Employer GetEmployer() { return null; }
...
}

public partial class Employer
{
public override Employer GetEmployer() { return this; }
...
}

然后我用这样的:

which I then used like this:

User u = GetUser();
Employer e = u.GetEmployer();

不过(我想,这并不奇怪),倍率不会被调用,并返回一个空。

However (I suppose, not surprisingly), the override is never called and a null is returned.

我试图解决的问题是我收集将是一个非常常见的用例:我有点我的数据需要存储,但它是不完整的。后来我得到更多的数据,我用它来提炼(垂头丧气)我对世界的理解。

The problem I'm trying to solve is what I gather would be a very common use-case: I get a bit of data I need to store but it's incomplete. Later I get more data and I use it to refine (downcast) my understanding of the world.

在这种特殊情况下,我用我的网站寄来的电子邮件地址,所以我知道他们是用户,但总的来说我什么都不知道否则他们。后来(当他们填写表格),我知道他们其实是一个雇主,所以我需要向下转换我的用户

In this particular case, I get an e-mail address from someone using my website so I know they are a User, but in general I don't know anything else about them. Later (when they fill out a form), I know that they are actually an Employer, so I need to downcast my User.

什么是正确的做法吗?

推荐答案

我回答你的前任的问题之一。您正在试图解决的东西不能得到解决。这里的解决方案是不使用继承。为什么?因为继承是的关系。所以,如果您有:

I answered one of your former questions. You are trying to solve something which cannot be solved. The solution here is not to use inheritance. Why because inheritance is is a relationship. So if you have:

public class User { }

public class Employee : User { }

您有此关系 Employeee为用户,但您不必反向关系用户是员工。但是,这是exectly你正在尝试从用户铸造做员工用户的实例不能转换到员工(除了我在previous答案所说的情况 - 但是你不要有这种情况)。

You have this relations Employeee is User but you don't have reverse relationship User is Employee. But that is exectly what you are trying to do with casting from User to Employee. Instance of User cannot be cast to Employee (except the situation I mentioned in my previous answer - but you don't have this situation).

使用这种方法,你会解决它在面向对象的方法,而不继承铸造或什么别的永远需要。

Use this approach and you will solve it in object oriented way without needs for inheritance casting or what ever else.

public class User 
{
    public virtual EmployeeData { get; set; }
}

public class EmployeeData { }

这种方法改变了从你的设计是一个有一个的关系。 EmployeeData工作在这种情况下,在1独立的实体映射 - 0..1关系(这将导致数据库中的两个表)。您也可以使用1 - 1的关系类型或复合,如果你是幸福的事实,即用户 EmployeeData工作将存储在同一个表中。

The approach changes your design from is a to has a relationship. EmployeeData in this case is separate entity mapped in 1 - 0..1 relation (that will result in two tables in the database). You can also use 1 - 1 relation or ComplexType if you are happy with the fact that both User and EmployeeData will be stored in the same table.

这篇关于使用安全的向下转换为一流的设计方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆