Click here to Skip to main content
15,891,670 members
Home / Discussions / C#
   

C#

 
GeneralRe: Ellipse Pin
WebMaster25-Oct-10 9:27
WebMaster25-Oct-10 9:27 
QuestionOOB design - class fields Pin
Dell.Simmons15-Oct-10 13:41
Dell.Simmons15-Oct-10 13:41 
AnswerRe: OOB design - class fields Pin
ricmil4215-Oct-10 14:29
ricmil4215-Oct-10 14:29 
GeneralRe: OOB design - class fields Pin
Dell.Simmons15-Oct-10 14:34
Dell.Simmons15-Oct-10 14:34 
AnswerRe: OOB design - class fields Pin
Abhinav S15-Oct-10 22:25
Abhinav S15-Oct-10 22:25 
AnswerRe: OOB design - class fields Pin
Keith Barrow16-Oct-10 5:40
professionalKeith Barrow16-Oct-10 5:40 
AnswerRe: OOB design - class fields Pin
Nish Nishant16-Oct-10 6:27
sitebuilderNish Nishant16-Oct-10 6:27 
AnswerRe: OOB design - class fields Pin
Paul Michalik17-Oct-10 0:45
Paul Michalik17-Oct-10 0:45 
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
QuestionRead the 'raw' FileVersion and ProductVersion values from a file's version resource. Pin
Blake Miller15-Oct-10 13:02
Blake Miller15-Oct-10 13:02 
QuestionApplication Settings "Save Location" Pin
I Believe In GOD15-Oct-10 10:30
I Believe In GOD15-Oct-10 10:30 
AnswerRe: Application Settings "Save Location" Pin
Ian Shlasko15-Oct-10 10:34
Ian Shlasko15-Oct-10 10:34 
GeneralRe: Application Settings "Save Location" Pin
I Believe In GOD15-Oct-10 10:46
I Believe In GOD15-Oct-10 10:46 
GeneralRe: Application Settings "Save Location" Pin
AspDotNetDev15-Oct-10 12:29
protectorAspDotNetDev15-Oct-10 12:29 
QuestionHow to create a user control with parameters as tags Pin
kuul1315-Oct-10 10:17
kuul1315-Oct-10 10:17 
AnswerRe: How to create a user control with parameters as tags Pin
Not Active15-Oct-10 11:47
mentorNot Active15-Oct-10 11:47 
QuestionAsp.net Website Pin
MKC00215-Oct-10 2:59
MKC00215-Oct-10 2:59 
AnswerRe: Asp.net Website Pin
OriginalGriff15-Oct-10 3:05
mveOriginalGriff15-Oct-10 3:05 
AnswerRe: Asp.net Website [modified] Pin
Abhinav S15-Oct-10 3:22
Abhinav S15-Oct-10 3:22 
GeneralRe: Asp.net Website Pin
J4amieC15-Oct-10 4:42
J4amieC15-Oct-10 4:42 
AnswerRe: Asp.net Website Pin
Yusuf15-Oct-10 4:47
Yusuf15-Oct-10 4:47 
GeneralRe: Asp.net Website Pin
Abhinav S15-Oct-10 5:12
Abhinav S15-Oct-10 5:12 
GeneralRe: Asp.net Website Pin
MKC00216-Oct-10 3:16
MKC00216-Oct-10 3:16 
GeneralRe: Asp.net Website Pin
Dave Kreskowiak16-Oct-10 7:11
mveDave Kreskowiak16-Oct-10 7:11 
AnswerRe: Asp.net Website Pin
Yusuf15-Oct-10 4:50
Yusuf15-Oct-10 4:50 
GeneralRe: Asp.net Website Pin
MKC00216-Oct-10 3:13
MKC00216-Oct-10 3:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.