N
N
nordwind20132018-07-18 12:45:24
ASP.NET
nordwind2013, 2018-07-18 12:45:24

How to avoid code repetition of repositories?

There are a lot of different entities. Here is the diagram 5b4f0b2446fd0088054942.jpeg
And you need to create repositories for these devils. Here is the native interface

interface IBaseService<T>:IDisposable
    {
        AccountModel Get<T>(int id) where T : class, new();

        AccountModel GetAll<T>() where T : class, new();

        bool Save<T>(AccountModel entity);

        bool Delete(int id);
    }

And here is an example of an AccountModel entity repository
class AccountBaseService : IBaseService <AccountModel>
    {
        List<AccountModel> AllAccountList = new List<AccountModel>();

        private AccountModel a;

        VideoHosting.AccountModel IBaseService<VideoHosting.AccountModel>.Get<AccountModel>(int id)
        {
            foreach (var a in AllAccountList)
            {
                if (a.AccId == id)
                {
                    return a;
                }

                return a;
            }

            return a;
        }

        AccountModel IBaseService<AccountModel>.GetAll<T>()
        {
            foreach (var a in AllAccountList)
            {
                return a;
            }

            return a;
        }

        bool IBaseService<AccountModel>.Save<T>(AccountModel entity)
        {
            entity = new AccountModel();

            AllAccountList.Add(entity);

            return true;
        }

        bool IBaseService<AccountModel>.Delete(int id)
        {
            foreach (var a in AllAccountList)
            {
                if (a.AccId == id)
                {
                    AllAccountList.Remove(a);
                }
            }

            return true;
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }

How can this be done without repeating the code? The whole problem is in the generalized sheet, I don’t know how to define it. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Simonov, 2018-07-18
@nordwind2013

interface IBaseService<T>:IDisposable where T: BaseEntity
    {
        AccountModel Get<T>(int id) where T : class, new();

        AccountModel GetAll<T>() where T : class, new();

        bool Save<T>(AccountModel entity);

        bool Delete(int id);
    }

class BaseEntity {
 public int Id {get;set;}
}

public BaseService: IBaseService<T> {

 protected List<T> Source = new List<T>();
 public bool Delete(int id) {
  foreach (var a in Source )
            {
                if (a.Id == id)
                {
                    Source.Remove(a);
                }
            }

            return true;
 }
}

The essence is approximately the following
If your field is not "Id" in the database, then you need to "map"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question