More then one entity?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
I have followed the tutorial on this site. How do I write the Service layer and the interface if I have more then one entity? Lets say I have three entities and I need to connect to the database with all of them.

I have these classes TypeOfVehicle, Vehicle and manufacture. My service layer looks like this now.


public partial interface ITypeOfVehicleService
{
  TypeOfVehicleRecord GetTypeOfVehicleById(int id);

  void DeleteTypeOfVehicle(TypeOfVehicleRecord typeOfVehicle);
  void UpdateTypeOfVehicle(TypeOfVehicleRecord typeOfVehicle);
  void InsertTypeOfVehicle(TypeOfVehicleRecord typeOfVehicle);

  IPagedList<TypeOfVehicleRecord> GetAllTypeOfVehicles(string typeOfVehicleName = "",
    int pageIndex = 0, int pageSize = int.MaxValue);
    }


My service class looks like this.

public partial class TypeOfVehicleService : ITypeOfVehicleService
    {
        private readonly IRepository<TypeOfVehicleRecord> _typeOfVehicleRepository;
        private readonly IEventPublisher _eventPublisher;

        public TypeOfVehicleService(IRepository<TypeOfVehicleRecord> typeOfVehicleRepository, IEventPublisher eventPublisher)
        {
            this._typeOfVehicleRepository = typeOfVehicleRepository;
            this._eventPublisher = eventPublisher;
        }


        public virtual IPagedList<TypeOfVehicleRecord> GetAllTypeOfVehicles(string typeOfVehicleName = "",
            int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var query = _typeOfVehicleRepository.Table;

            query = query.OrderBy(x => x.TypeOfVehicleName);
            var typeOfVehicles = new PagedList<TypeOfVehicleRecord>(query, pageIndex, pageSize);
            return typeOfVehicles;
        }

        public virtual void InsertTypeOfVehicle(TypeOfVehicleRecord typeOfVehicle)
        {
            if (typeOfVehicle == null) { throw new ArgumentNullException("typeOfVehicle"); }

            _typeOfVehicleRepository.Insert(typeOfVehicle);
            _eventPublisher.EntityInserted(typeOfVehicle);
        }

        public virtual void UpdateTypeOfVehicle(TypeOfVehicleRecord typeOfVehicle)
        {
            if (typeOfVehicle == null) { throw new ArgumentNullException("typeOfVehicle"); }

            _typeOfVehicleRepository.Update(typeOfVehicle);
            _eventPublisher.EntityUpdated(typeOfVehicle);
        }

        public virtual void DeleteTypeOfVehicle(TypeOfVehicleRecord typeOfVehicle)
        {
            if (typeOfVehicle == null) { throw new ArgumentNullException("typeOfVehicle"); }

            _typeOfVehicleRepository.Delete(typeOfVehicle);
            _eventPublisher.EntityDeleted(typeOfVehicle);
        }

        public virtual TypeOfVehicleRecord GetTypeOfVehicleById(int id)
        {
            if (id == 0)
                return null;

            return _typeOfVehicleRepository.GetById(id);


        }
    }


Do I need to write service and Iservice for all my entities or can I use the same? If I can use the same, how will I do that?
7 years ago
You can do it all in one. But different will better.
7 years ago
Thanks for your answer. I will do it in different files.

Thanks again
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.