I'm reading about application layers, and want to use this design in my next project (c#, .Net). Some questions:
-
Is the separation of layers done through namespaces? Project.BLL.Whatever, Project.DAL.Whatever
-
Is it more appropriate to separate by layers, then components (Project.BLL.Component1), or by components, then layers (Project.Component1.BLL)
-
For my DAL, is this layer further organized using different classes? If all database calls are put into a single class, there is no organization. Would it be better to split these up with different classes or namespaces?
-
Are DAL classes typically static? It seems cumbersome to instantiate a DAL object before calling one of its methods each time.
Any other tips for doing things the correct way with these layers would be appreciated.
Best Answer
No. That makes it much harder to unit test. You should use dependency injection to pass in dependencies to the constructor of each data access class (such as an IDatabaseService). You would then pass the data access objects into the business objects, like this:
BusinessObject businessObject = new BusinessObject(new DataAccessObject(new DatabaseService())); businessObject.PerformOperation();
Each business object may need multiple data access objects. Your GUI code would also use one or more business objects. Some business objects may not need any data access objects but should never use the IDatabaseService directly.