 |
|
 |
Hi,
I tried to explain to my colleagues why the factory pattern can control change ripples but failed. Even if the UI used 'new' to create the clsInvoiceDetail object, I can simply change clsInvoiceDetail's internal coding, recompile, copy the dll to the UI folder and run the UI without recompiling the UI.
However, If I need to change 'string CustomerName' to 'int CustomerId', I still need to recompile everything anyway even if the factory pattern is used.
Can you explain in more depth why the factory pattern can control change ripples while the use of 'new' cannot?
Cheers, Michael Leung
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I have read this article atleast 4 times and it has fondled by thinking....I have been working with plain vanilla behind code. I think after years i am trying to understand three tier...Good walkthrough i will try to creat a sample and implement it as a practice.
By the way congrats for your half century of article....
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
 |
As far as I know, this is not the way a factory works. A factory usually has at least two kind of functionalities, and is ESPECIALLY not dependent on a specific class : - Register a creation method or an instance of an interface - A create function for an interface.
An example :
Factory.Register<IRepositoryA>(new RepositoryA); Factory.Register<IRepositoryB>(delegate {return new RepositoryB});
Then in your program somewhere else you instantiate instances of the interface like this:
IRepositoryA a = Factory.Create<IRepositoryA>(); IRepositoryB b = Factory.Create<IRepositoryB>();
Typically a factory is about a lot more then the things you write about (i.e instance& lifecycle mgmt, configuration etc), and sometimes even some aop-related stuff.
About your ui: one would typically (in an layered model) create an MVP implementation, where the view is your interface, and the presentation layers simply couples through an IView instance through properties and events.
Your examples only create lot more work, and do not have any advantage at all. If this is someone's first article about these patterns, I can imagine he or she would create an aversion of it.
On the positive side, the layout is really nice, and it reads really smooth (big plus about that), you should get you facts right about layered apps, the factory pattern as well as dependency injection BEFORE you publish an article (maybe contact an application architect). Just my .02€
modified on Tuesday, November 11, 2008 9:08 AM
|
| Sign In·View Thread·PermaLink | 3.67/5 (2 votes) |
|
|
|
 |
|
 |
The basic point of the article was to show the importance of factory and trying to stress out the fact that interfaces should be referenced and not the concrete classes. Yes the factory shown above is a very very very basic representation of factory. The point to was to show how factory removes the new keywords from the UI and thus giving more benefit of decoupling. I did not wanted to talk about DI and more compilcated factory and MVP etc. The point was to show the basics....Ok i agree very basics
What you said above is right. When we talk about a typical nice, decoupled architecture your points are valid. Just thought will make the article very complicated.
Footprints on the sand are not made by sitting at the shore.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
you write: "So what will happen if the business objects change?. It will just ripple changes across all the three layers. That means you need compile the whole project."
and what do you do a few lines later?
"IInvoiceDetails objInvoiceDetail = FactoryFinance.GetInvoiceDetails(); objInvoiceDetail.CustomerName = txtCustomerName.Text; objInvoiceDetail.CustomerAddress = txtCustomerAddress.Text;"
in the UI layer.. so if stuff changes in the business object you have ti change the object's class, its properties references in the UI (and DA) and properties in the interface, which is just more code to handle..
passing object's properties rather than the object reference itself between layers is plain stupidity, and coders who're in the business for some time don't do that, so why bother and write interfaces - implementations, factories and stuff.. if you want to avoid "new" keyword in the UI so much you can write a static method returning new object in the referenced class instead of a factory class...
life is study!!!
|
| Sign In·View Thread·PermaLink | 5.00/5 (2 votes) |
|
|
|
 |
|
 |
Seishin# wrote: in the UI layer.. so if stuff changes in the business object you have ti change the object's class, its properties references in the UI (and DA) and properties in the interface, which is just more code to handle..
Your UI does not refer the concrete class. You are only referencing the proxy...You definetly have less work to do as the creation part is handled by the factory.
Seishin# wrote: passing object's properties rather than the object reference itself between layers is plain stupidity, and coders who're in the business for some time don't do that, so why bother and write interfaces - implementations, factories and stuff.. if you want to avoid "new" keyword in the UI so much you can write a static method returning new object in the referenced class instead of a factory class...
Static methods means everything should be static inside. I am sure that will not be a good thought. Yes i agree many people do not want to pass objects but we are talking about interface references and not concrete objects. This brings in a unformity of communicatio both at the UI and DAL end. Both end we have interfaces and if we step one step further and talk about external UI they also have to reference the interface...A common vocabulary of communication for the invoice properties.
Footprints on the sand are not made by sitting at the shore.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Shivprasad koirala wrote: Your UI does not refer the concrete class. You are only referencing the proxy...You definetly have less work to do as the creation part is handled by the factory.
ok, maybe I didn't make myself clear..
your UI refers to properties from the proxy which are implemented in some class.. when you change the class you have to change the interface and the references in the UI - so in case of some changes there's even more refactoring due to the interface..
Shivprasad koirala wrote: Static methods means everything should be static inside.
lol.. this one killed me..
public class SomeClass{ public static GetInstance(){ return new SomeClass(); } }
life is study!!!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Seishin# wrote: your UI refers to properties from the proxy which are implemented in some class.. when you change the class you have to change the interface and the references in the UI - so in case of some changes there's even more refactoring due to the interface..
Again the properties are not in some class its in a interfaces. When some change happens not in the public interface but the logic you do not need to change anything in the UI. I mean if any property is removed or added you can help out there will be refactoring. But if some changes logic the UI is not affected
Seishin# wrote: lol.. this one killed me..
public class SomeClass{public static GetInstance(){return new SomeClass();}}
I did not make myself clear here. If i make a function in the class itself with a static function what is the point....
Footprints on the sand are not made by sitting at the shore.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Well Seishin i think you need to brush your OOPS skill. I think if you are thinking changing in interfaces is only the properties the bigger change is the logic.
My two cents brush up your OOPS concept.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I didn't quite get what you meant by
terragenegeystur wrote: I think if you are thinking changing in interfaces is only the properties the bigger change is the logic.
logic is implemented in classes...
so brush up your english (grammar) and C# skill..
life is study!!!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Ahhhh that makes me believe more stronger....brush up your OOPS skill Logic is in classes and interfaces are proxies. So if you refer proxies logic changes do not matter to the client using it as they refer proxy. I can smell from here that you are a newbie in OOP's. But one thing is sure you have a very good logic. These kind of thinking and questions only emerge when you are thinking logically. If you understand interfaces rest should follow.
I know i also need to improve...any way saw your signature even i am learning.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
terragenegeystur wrote: I can smell from here that you are a newbie in OOP's
yeah.. I must admit, after over 4 years I'm still no compare to old chinese masters with black belts in .net framework 2.0... not to mention you.. give me a break.. (before you reply check the last phrase in a dictionary that contains idioms...)
terragenegeystur wrote: Logic is in classes and interfaces are proxies.
and what have I just written??
terragenegeystur wrote: Ahhhh that makes me believe more stronger....brush up your OOPS skill
believe what you want.. I prefer facts.. and the fact here is you don't understand what I wrote.
p.s. why do I get the feeling your the Shivprasad guy under another login? maybe it's what you and him are writing, maybe it's that you just registered, or maybe it's that you write your posts about the same time he is.. beats me...
life is study!!!
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
Seishin# wrote: p.s. why do I get the feeling your the Shivprasad guy under another login? maybe it's what you and him are writing, maybe it's that you just registered, or maybe it's that you write your posts about the same time he is.. beats me...
I better write more articles than logging in using proxies.....I think codeproject does not allow posting from one IP. If you do , it will mark the user as spam.
I end this discussion here itself.
Footprints on the sand are not made by sitting at the shore.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Shivprasad koirala wrote: Again the properties are not in some class its in a interfaces.
again, the properties in interface are implemented in a class - if some new properties appear or some disappear you still have to recompile the UI to use them - doesn't matter if you use them through interface or directly from class instance.
Shivprasad koirala wrote: When some change happens not in the public interface but the logic you do not need to change anything in the UI.
the same goes for non-interface solution.. if your class is in another layer (project) and some logic changes the call from UI is the same - no need to recompile ui..
Shivprasad koirala wrote: If i make a function in the class itself with a static function what is the point....
the point is that you don't need another class (factory) to create instance of some class witout using "new" in the UI layer. as Tom Janssens wrote, you gave a bad example of factory pattern usage here - there's no need for one if it just initializes one particular implementation of the interface.
life is study!!!
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
 |
Seishin# wrote: again, the properties in interface are implemented in a class - if some new properties appear or some disappear you still have to recompile the UI to use them - doesn't matter if you use them through interface or directly from class instance.
Yes that true and this what exactly i said.
Seishin# wrote: the same goes for non-interface solution.. if your class is in another layer (project) and some logic changes the call from UI is the same - no need to recompile ui..
No....If you have concrete classes you need to recompile the UI by referencing the new concrete classes. But if they are referencing interface they do not want to recompile...The UI references the proxy i.e the interface ...
Seishin# wrote: the point is that you don't need another class (factory) to create instance of some class witout using "new" in the UI layer. as Tom Janssens wrote, you gave a bad example of factory pattern usage here - there's no need for one if it just initializes one particular implementation of the interface.
The whole point of transfering the role to other class is that he takes care of the creation and passes the interface reference to the UI. It will be tangled code if every business oject takes the creational activity. I can make the above article for factory more complicated by putting lot initialization etc etc. Its a factory and i kept it simple for the purpose of article. I have not disagreed to Tom but its important to get basics first and things will follow later.
Footprints on the sand are not made by sitting at the shore.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
Hi,
Good article.
Its probably also worth mentioning that this approach also makes testing a much easier process.
Using this approach and then using unit tests and mock objects make for good testing and code coverage.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
We pass direct concrete classes between layers a interface on the top is good thought. I mean its obvious. I failed to understand the factory pattern thing at the UI side....Will not interface suffice
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Factory pattern avoid the UI to use new keywords. If UI is given the task of creating objects you will always need to reference the concrete object....So says the Article Factory + Interfaces....
I am sure there are better ways of doing decoupling. But i think the basics revolve around Interfaces and Factory.....
Footprints on the sand are not made by sitting at the shore.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
public void InsertInvoiceDetails(IInvoiceDetail iobj) { objDatabase.ExecuteNonQuery("usp_InsertInvoiceDetails",iobj.Id,iobj.InvoiceHeaderId, iobj.ProductId, iobj.Quantity, iobj.Amount,iobj.TaxAmount,iobj.PaidAmount,iobj.UnitCost); }
If we not use the Interfaces+Factory pattern, can we use method like below public void InsertInvoiceDetails(InvoiceDetail iobj) { ... }
Sorry for not understanding your demo.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Yes thats what i had shown in the last code of the article.
Footprints on the sand are not made by sitting at the shore.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I have seen the article of MSDN and is nice. There is nothing new but many developer or architect fail to understand the power of interfaces and pass direct concrete classes between the layers....A spice of Factory pattern makes it look better.
Footprints on the sand are not made by sitting at the shore.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |