I'm trying to move my business logic out of my service class and my controller, and put it inside my business class.
public interface IFoo{
IBar CreateBar(creationParameters);
}
public class FirstFoo : IFoo{
IBar CreateBar(creationParameters){
return new FirstBar(creationParameter.Id);
}
}
public interface IBar{
void DoSomething();
}
public class FirstBar : IBar{
FirstBar(int id){...}
void DoSomething(){
//well... do something
}
}
public class SecondBar : IBar{
FirstBar(int id){...}
void DoSomething(){
//well... do something else
}
}
Let's imagine I need to create a SecondFoo that'll need access to the database to know of it must create a FirstBar or a SecondBar, how do I do that ? I inject my data source inside the SecondFoo's constructor ? Service locator ? I move the createBar out of the IFoo ?
EDIT: I'm not looking for a definition of the Factory Pattern, the createBar method might be something like "changeBar" or "doBusinessWithBar".
Let's imagine I need to create a SecondFoo that'll need access to the database to know of it must create a FirstBar or a SecondBar
It seems SecondFoo needs some kind of decision logic to decide whether to create a FirstBar or a SecondBar. That logic could be factored out in a strategy interface, of which the database access implementation would be used in production. You could inject the strategy via the constructor.
IBar CreateBar(creationParameters) {
if (strategy.ShouldCreateFirst(creationParameters)) {
return new FirstBar(creationParameter.Id);
}
else {
return new SecondBar(creationParameter.Id);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With