Mendes
Mendes

Reputation: 18561

Common or multiple header files for shared library

I´m building a C++ shared library that will be used to access business objects in a database (essetialy a DAL - Data Access Layer).

So, I have basically multiple objects (business classes), like:

Customer
Address
Product

For each of those, I will have a cpp file providing basic CRUD operations: Create, Read, Update, Delete. Those operations I´ll planning to put in namespaces, like the following:

customer_access.cpp:

namespace customer_access {

      void create(Customer new_customer) { ...code... };
      void read(int id) { ...code... };
      void update(int id, new_customer) { ...code... };
      void delete(int id) { ...code... };
}

address_access.cpp

namespace address_access {

      void create(Address new_address) { ...code... };
      void read(int id) { ...code... };
      void update(int id, new_address) { ...code... };
      void delete(int id) { ...code... };
}

What is the best practice on that case for the shared library header file that will be included on external code:

a) Build one header file for the library and build reference to all namespaces:

dalaccess.hpp

namespace customer_access {

      void create(Customer customer);
      void read(int id);
      void delete(int id);
      void delete(int id);
}
namespace address_access {

      void create(Customer customer);
      void read(int id);
      void delete(int id);
      void delete(int id);
}

b) Build one header file for each object access, like:

customer_access.hpp:

namespace customer_access {

      void create(Customer customer);
      void read(int id);
      void delete(int id);
      void delete(int id);
}

address_access.hpp

namespace customer_access {

  void create(Customer customer);
  void read(int id);
  void delete(int id);
  void delete(int id);

}

Thanks for helping... Please feel free to comment the whole idea if needed....

Upvotes: 0

Views: 1252

Answers (1)

diverscuba23
diverscuba23

Reputation: 2185

A lot of large projects do both. They provide the individual header files for the basic pieces, and then provide the overall header file that just includes each of the individual header files as a convenience.

Upvotes: 2

Related Questions