【问题标题】:c# Generic methods for phonebook projectc#电话簿项目的通用方法
【发布时间】:2020-01-29 05:13:48
【问题描述】:

我正在尝试制作电话簿项目,我从 bin 文件中写入/读取数据,我在域类库中有两个类,用户和联系人,现在我想在 FileManager 类中创建私有通用函数,添加/编辑/delete 和 Get 将为联系人和用户找到/工作,

我如何知道private T Get<T>(int id) where T : class 函数中给出了哪种类型?使其适用于这两种品种

如何正确完成这些功能?

namespace Phonebook.Data
{
  public class FileManager
  {
    protected string DataFilePath => @"D:\Data.bin";

    protected IList<User> Users { get; set; }
    protected IList<Contact> Contacts { get; set; }

    public IEnumerable<Contact> SearchContacts(Func<Contact, bool> predicate)
    {
      foreach ( var contact in Contacts )
        if ( predicate(contact) )
          yield return contact;
    }

    public IEnumerable<Contact> AllContacts()
    {
      return SearchContacts(x => true);
    }

    public Contact GetContact(int contactID)
    {
      return Get<Contact>(contactID);
    }

    private T Get<T>(int id) where T : class
    {
      return null;
    }

    private int GenerateContactID()
    {
      int id = 0;
      foreach ( var contact in AllContacts() )
        if ( contact.ContactID > id )
          id = contact.ContactID;
      return id + 1;
    }

    public void AddContact(Contact contact)
    {
      contact.ContactID = GenerateContactID();
      Contacts.Add(contact);
    }

    public void EditContact(Contact contact)
    {
      Edit<Contact>(contact);
    }

    public void DeleteContact(int contactID)
    {
      Delete<Contact>(contactID);
    }

    //add edit da delete generic for both, user / contacts

    private void Add<T>(T entry)
    {
    }

    private void Edit<T>(T entry)
    {
    }

    private void Delete<T>(int id)
    {
    }

    #region Data Load/Save Methods

    public IEnumerable<Contact> LoadData()
    {
      using ( FileStream fileStream = new FileStream(DataFilePath, FileMode.Open) )
      using ( BinaryReader reader = new BinaryReader(fileStream) )
      {
        List<Contact> contacts = new List<Contact>();
        reader.BaseStream.Position = 0;
        while ( reader.PeekChar() != -1 )
        {
          Contact contact = new Contact();
          contact.ContactID = reader.ReadInt32();
          contact.FirstName = reader.ReadString();
          contact.LastName = reader.ReadString();
          contact.Phone = reader.ReadString();
          contact.EMail = reader.ReadString();
          contact.UserID = reader.ReadInt32();
          contacts.Add(contact);
        }
        return contacts;
      }
    }

    public void SaveData(IEnumerable<Contact> contact)
    {
      using ( FileStream fileStream = new FileStream(DataFilePath, FileMode.OpenOrCreate) )
      using ( BinaryWriter writer = new BinaryWriter(fileStream) )
        foreach ( var item in contact )
        {
          writer.Write(item.ContactID);
          writer.Write(item.FirstName);
          writer.Write(item.LastName);
          writer.Write(item.Phone);
          writer.Write(item.EMail);
          writer.Write(item.UserID);
        }
    }

    #endregion
  }
}

【问题讨论】:

标签: c# generics methods phonebook


【解决方案1】:

我认为您应该分别为 User 和 Contact 类做一个通用接口及其实现。如果出现一个新类,例如 Employee - 您将执行此接口的新实现,而不会对 User 和 Contact 类进行任何更改。如果源不是二进制文件,而是数据库 - 那么这个接口的单独实现。

如下:

interface IManager<TEntity> where TEntity : class
    {
        IList<TEntity> GetAll();
        TEntity GetById(int id);
        void Add(TEntity entity);
        void Update(TEntity entity);
        void Remove(int id);
        int GenerateContactId();
        IList<TEntity> Search(Func<TEntity, bool> p);
    }

    class BinaryContactManager : IManager<Contact>
    {
        public void Add(Contact entity)
        {
            throw new NotImplementedException();
        }

        public int GenerateContactId()
        {
            throw new NotImplementedException();
        }

        public IList<Contact> GetAll()
        {
            throw new NotImplementedException();
        }

        public Contact GetById(int id)
        {
            throw new NotImplementedException();
        }

        public void Remove(int id)
        {
            throw new NotImplementedException();
        }

        public IList<Contact> Search(Func<Contact, bool> p)
        {
            throw new NotImplementedException();
        }

        public void Update(Contact entity)
        {
            throw new NotImplementedException();
        }
    }

    class BinaryUserManager : IManager<User>
    {
        public void Add(User entity)
        {
            throw new NotImplementedException();
        }

        public int GenerateContactId()
        {
            throw new NotImplementedException();
        }

        public IList<User> GetAll()
        {
            throw new NotImplementedException();
        }

        public User GetById(int id)
        {
            throw new NotImplementedException();
        }

        public void Remove(int id)
        {
            throw new NotImplementedException();
        }

        public IList<User> Search(Func<User, bool> p)
        {
            throw new NotImplementedException();
        }

        public void Update(User entity)
        {
            throw new NotImplementedException();
        }
    }

【讨论】:

  • 谢谢,我用界面做的。
  • @lukagogrichiani 如果此答案为您解决了问题,请考虑通过单击旁边的复选标记来接受它。还可以考虑投票。这两种机制都是网站的运作方式。
猜你喜欢
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多