Nor of the two is fundamentally wrong. As other posters have pointed out it depends on the required life-time of the DataLayer object. I would however recommend to decouple the dependency between the layers via interfaces:
public interface IDataLayer {
void DoSomething(string firstName, string lastName);
}
public interface IDataLayerFactory {
IDataLayer Generate();
}
Then, your first and second alternatives become:
public class BusinessLayer {
private IDataLayerFactory dataLayerFactory;
protected BusinessLayer() {
dataLayerFactory = null;
}
public BusinesLayer(IDataLayerFactory pDataLayerFactory) {
dataLayerFactory = pDataLayerFactory;
}
public int Method1(string firstName, string lastName) {
return dataLayerFactor.Generate().DoSomething(firstName, lastName);
}
}
public class BusinessLayer {
private IDataLayer dataLayer;
protected BusinessLayer() {
dataLayer = null;
}
public BusinesLayer(IDataLayer pDataLayer) {
dataLayer = pDataLayer;
}
public int Method1(string firstName, string lastName) {
return dataLayer.DoSomething(firstName, lastName);
}
}
Of course there has to be an implementation:
class ASpecificDataLayer : IDataLayer {
public void DoSomething(string firstName, string lastName) {
// implementation goes here
}
};
class ASpecificDataLayerFactory : IDataLayerFactory {
public IDataLayer Generate() {
return new ASpecificDataLayer();
}
};
The fundamental difference is that your buiness layer does not depend on a specific implementation of a data layer, it only depends on the interface which can be implemented in various ways. This idiom is fundamental in OOD and is called dependency inversion. There is a lot of support for this pattern at framework level in form of so called dependency containers or service locators. Please google on the boldified termini for further information. Cheers, Paul