我有一个关于面向对象概念或SQL /继承的问题 [英] I have a question about object oriented concepts or maybe SQL / inheritance

查看:109
本文介绍了我有一个关于面向对象概念或SQL /继承的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个关于面向对象概念的问题。所以我有一个程序,我有一个User类,我有一个Customer类,它继承了所有的用户属性,然后有一些额外的例子:用户有Id,用户名,密码。然后客户继承了所有这些,并且还有一些额外的名字等等。

在我的程序中,我可以在某个地方使用用户类,比如制作管理员然后我将对象传递到下一个屏幕在我的新屏幕中,我希望能够创建一个拥有所有用户属性的客户,然后可以添加新的字段,如customerid,firstname等。



我有2个表,一个用户表和一个客户表,我想用ID作为外键互相引用。

我想到这个错误,或者我应该在整个项目中使用客户,还是我可以在用户转到特定窗口并添加这些新字段时将其升级为客户。



我尝试了什么:



只要使用基类,只要我可以去

现在我有一个新的窗口,在哪里建立一个客户,我希望客户拥有所有的用户信息,但也能够拥有这些新的属性数据

Hello all,
I have a question about Object Oriented concepts. So I have a program where I have a User class and I have a Customer class which inherits all the user properties and then has some extras for example: User has Id, Username, Password. Then Customer inherits all of those and has a couple extra like Firstname, etc.
In my program can I use the ‘User’ class somewhere like for making a Admin then I pass the object to the next screens and in my new screen I want to be able to make a Customer that has all the User properties and then can have the new fields added like customerid, firstname, etc.

I have 2 tables a User table and a customer table and I wanted to reference each other with the ID as a foreign key.
Am I thinking about this wrong or should I be using Customer throughout my whole project or am I able to upgrade a User to Customer when they go to a specific window and add these new fields.

What I have tried:

just using the base class as long as i can go
now i have a new window where to make a customer and i want the customer to have all the user info but to also be able to do have these new property data

推荐答案

你好,

首先,让我们让你的问题更加清晰,并确保我理解正确。所以你有一个名为 User 的类和一个名为 Customer 的类,它是用户类。



Hi to you,
First, let`s make your question a little bit more clear and make sure that I understood right. So you have a class called User and a class called Customer which is a child class of the User class.

public class User
{
   public string Id {get;set;}
   public string Username {get;set;}
   public SecureString Password {get;set;}
   //Read about SecureString in System.Security
}

public class Customer : User
{
   //This is more-less a builder pattern
   public Customer(User user)
   {
        Id = user.Id;
        Username = user.Username;
        Password = user.Password;
   }
   public string Name {get;set;}
}

var user = new User() { Id = "id", Username = "user" };
var customer = new Customer(user);





好​​的,现在您想要从用户创建客户,您只需要使用客户的构造函数并将您的用户注入客户对象。但是,我更愿意将用户作为客户的财产而非其子类。这将使这两个实体分离,并且从业务角度来看也将带来更多的灵活性和清晰的理解。

您可以将客户放在带有用户表的外键的表中,因为客户可能有多个用户(主要取决于您的业务模式)。

但是,如果由于任何合理的原因你需要使用继承,那么你可以做的是创建一个基类/抽象类,如 Person 并驱动来自这个类。

顺便说一句,您可能还需要查看实体状态图,该图讨论了系统不同级别中同一实体的不同统计数据。



干杯,

AH



Ok, now where ever you want to create a customer from a user you just need to use the customer`s constructor and inject your user inside the customer object. However, I would prefer to have the user as a property of customer rather a child class of it. This will decouple these two entities and also will bring more flexibility and a clear understanding from the business point of view.
You can keep customers in a table with a foreign key to the user's table because a customer may have more than one user (depends strongly on your business model).
But, if for any reasonable reason you needed to use inheritance then what you can do is to create a base/abstract class like Person and drive both from this class.
Btw, you may also need to have a look at the Entity State Diagram which talks about different stats for the same Entity in different levels of the system.

Cheers,
AH


这是错误的:你不应该尝试设置循环引用。

为什么需要?

您有一个用户列表 - 其中一些是客户 - 以及单独的客户列表。

客户表引用通过外键访问Users表,但不是所有用户都是客户,所以你不需要另一个链接。



然后你可以使用JOIN合并来自两个表格的信息。
It's wrong: you shouldn't try to set up circular references.
Why would you need to?
You have a list of Users - of which some are customers - and a separate list of Customers.
The Customer table refers to the Users table via a Foreign Key, but not all Users are Customers, so you don't need a link the other way.

You can then use a JOIN to combine information from the two tables.


这篇关于我有一个关于面向对象概念或SQL /继承的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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