c # Общие методы для проекта телефонной книги [новичок] - PullRequest
0 голосов
/ 29 сентября 2019

Я пытаюсь сделать проект телефонной книги, где я пишу / читаю данные из файла bin, у меня есть два класса в библиотеке классов Domain, User и Contacts, теперь я хочу сделать частные обобщенные функции в классе FileManager, добавить / изменить/ delete и Get, которые найдут / будут работать как для Контакта, так и для Пользователя,

Как узнать, какой тип задан в приватном T Get (int id) где T: функция класса? чтобы заставить его работать с обоими разновидностями

Как правильно завершить эти функции?

первый проект для меня, попробуй тяжело. Большое спасибо.

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
  }
}

1 Ответ

0 голосов
/ 30 сентября 2019

Я думаю, вы должны сделать общий интерфейс и его реализации для классов 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();
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...