继承,基类,派生类

时间:2014-11-10 05:45:35

标签: c# database inheritance

我再次从头开始..我想知道以下代码是否可以编写和查询数据库。而不是获取和设置城市,州等人的个人文件。我希望容器类能够做到这一点..当我查询一个人时,获取并将该contactinfo对象与其他人数据一起发送到主程序。

namespace ConsoleApplication1
{
    using System;
    using System.Collections.Generic;

    public partial class PERSON
    {
        public PERSON()
        {
            this.MANAGERs = new HashSet<MANAGER>();
            this.TRANSACTIONLISTs = new HashSet<TRANSACTIONLIST>();
        }
        public PERSON(string fName, string lName, ContactInfo contactInfo)
        {
            F_NAME = fName;
            L_NAME = lName;
            ContactInfo = contactInfo;
            this.MANAGERs = new HashSet<MANAGER>();
            this.TRANSACTIONLISTs = new HashSet<TRANSACTIONLIST>();
        }

        public ContactInfo ContactInfo { 

            get
            {
                ContactInfo.Phone = PHONE;
                ContactInfo.Email = EMAIL;
                ContactInfo.Address = new Address() { Street = ADDRESS, City = CITY, State = STATE, Zip = ZIP };
                return ContactInfo;

            }
            set
            {
                PHONE = ContactInfo.Phone;
                EMAIL = ContactInfo.Email;
                ADDRESS = ContactInfo.Address.Street;
                CITY =ContactInfo.Address.City;
                STATE = ContactInfo.Address.State;
                ZIP = ContactInfo.Address.State;
            }
        }

        public string F_NAME { get; set; }
        public string L_NAME { get; set; }
        public string ADDRESS { get; set; }
        public string CITY { get; set; }
        public string STATE { get; set; }
        public string ZIP { get; set; }
        public string PHONE { get; set; }
        public string EMAIL { get; set; }
        public decimal ID { get; set; }
        public string CURRENTMEMBER { get; set; }
        public Nullable<decimal> TOTALDUE { get; set; }
        public string ISMEMBER { get; set; }
        public string PASSWORD { get; set; }

        public virtual ICollection<MANAGER> MANAGERs { get; set; }
        public virtual ICollection<TRANSACTIONLIST> TRANSACTIONLISTs { get; set; }
    }
}

2 个答案:

答案 0 :(得分:0)

你的设计对我来说有点混乱。您从PersonController继承PERSON,并在PersonController(父类)中直接使用PERSON(子类)。 如果我是你,我会创建一个继承自PERSON(我的实体类)的成员类 一个Controller类(T必须是PERSON或子类),它包含与PERSON相关的方法。

答案 1 :(得分:0)

我不得不说你当前的设计很混乱,即使你设法让它工作,也会让人感到困惑。您应该首先遵循iDesign C#命名约定。

我从Person课程一直开始,重新思考它的属性。哪些属性组成了一个很好的逻辑信息组,使它们成为类。

有人会提出,例如以下。

public class Person
{
    public Person()
    {
    }

    public Person(string fName, string lName, ContactInfo contactInfo)
    {
        FirstName = fName;
        LastName = lName;
        ContactInfo = contactInfo;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public ContactInfo ContactInfo { get; set; }
}

其中ContactInfo来自电话号码,电子邮件和Address课程。

public class ContactInfo
{
    public string Phone { get; set; }
    public string Email { get; set; }
    public Address Address { get; set; }
}

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }
}

现在您的Member将是一个人,但附加了其他属性。所以它看起来像下面的

public class Member : Person
{
    public Member(string fName, string lName, ContactInfo contactInfo)
        : base(fName, lName, contactInfo)
    {
        Id = 0;
        CurrentMember = "Y";
        TotalDue = 0;
        IsMember = "Y";
        Password = "Default";

        Managers = new HashSet<Manager>();
        Providers = new HashSet<Provider>();
        Transactionlists = new HashSet<Transactionlist>();
    }

    public decimal Id { get; set; }
    public string CurrentMember { get; set; }
    public decimal? TotalDue { get; set; }
    public string IsMember { get; set; }
    public string Password { get; set; }

    public ICollection<Manager> Managers { get; set; }
    public ICollection<Provider> Providers { get; set; }
    public ICollection<Transactionlist> Transactionlists { get; set; }
}

如果它继承自通用MemberController,那么来自&#34;控制器&#34;,PersonControllerController<T>的内容会更有意义。抽象基础可能是

public abstract class ControllerBase<T>
{
    public abstract void InsertOrUpdate(T obj);
}

派生MemberController

public class MemberController : ControllerBase<Member>
{
    public override void InsertOrUpdate(Member obj)
    {
        if (obj == null)
            throw new ArgumentException("Member");

        using (var context = new ChocDB())
        {
            var state = obj.Id == 0 ? EntityState.Added : EntityState.Modified;

            if (state == EntityState.Added)
                context.People.Add(obj);
            context.SaveChanges();
        }
    }
}

这有什么意义吗?