Do you ever get the feeling that you're doing things the wrong way, or maybe the right way, and then you get all confused...?
I've developed a number of apps using CSLA and have never felt the need to use interfaces. I'm currently working on a new, fairly large, development so we're at that critical time of starting things off correctly.
My plan was to create an object library, then simply use these objects in the Windows GUI, Web Services and such. This seems like the simplest method to me, and I like to opt for the simplest method.
Other people in the team (including the team leader and other lead developers) are insisting on the use of interfaces which also requires the use of factories. I'm outnumbered, so I'm trying to reason with myself that this is a good method. My instincts are stubborn, so I could really do with someone giving me a good argument why I might be being stupid.
This method will mean our code will need to be something like this (using Customer as an example):
ICustomer customer = ICustomerFactory.GetCustomer(customerId);
or
ICustomerList customerList = ICustomerFactory.GetCustomerList();
instead of simply:
Customer customer = Customer.GetCustomer(customerId);
My opinion is that the interface/factory method means more coding aka more complexity, and for what?
Their argument for this is "How else are we going to use mock objects?". I agree that this does add a level of abstraction, but I can't help believing this is unnecessary. Isn't it a simpler solution to just create a mock object library if need be, and use that instead of the production library?
Additionally, I've run into a slight issue with databinding to interfaces...
We now have a definitive interface heirarchy such as this:
IItem (Id, Name, Description)
ICustomer : IItem (adds Address, PhoneNumber etc)
If I add a Data Source to a Customer object then all's well. If I add a Data Source to ICustomer then all I get are the properties definied by ICustomer. IItem properties are ignored. Any ideas???
Thanks in advance for any advice, even if it involves calling me a muppet.
Andy - thanks for your useful opinion. I have just finished reading your earlier thread on interfaces and data binding which cleared up that issue
I'm not sure I can see the reasoning to prevent data binding to an interface heirarchy (when adding Data Sources). I figured that as an object could have a number of interfaces, why would you try and bind to any number of them when it's far easier just to bind to the concrete object... does that make sense, or am I rambling!?!
For me your comments about YAGNI and 'we may need this later' so 'then we'll add it later' really hit the nail on the head. I'm all for agile development and XP. Unfortunately for us we have legacy software and our developers are constantly saying "We know we'll need this in the future, so lets do it all now". I normally retaliate with "Do we really need it, and if so we'll do it when it's needed". Then I get "Don't be daft, that will mean refactoring a lot of the code in the future". I reply "So what?". Hmm. As far as I can see, these interfaces and factories aren't necessary at the moment.
I also agree with you about mock objects. Why create them until we have a need? They're certainly worth while, especially in unit testing. I quite often think they're a waste of effort, but I can't dismiss that proper unit testing is also very useful to have. Besides, I can't see why mock objects are dependant on interfaces... but it seems that other people believe you can't have a mock object without an interface.
Sorry about the rant. And thanks, I really appreciated your 2 cents.
It's very interesting to see other peoples opinions.
pfeds:Unfortunately for us we have legacy software and our developers are constantly saying "We know we'll need this in the future, so lets do it all now".
Shame on any developers that creates software parts for the future - guess what - the future might never come. And if they do so, then they are creating more issues for the future, and for what, for something they think they will need! I say if you don't have a use case today, you don't code it today. If you get a new use case tomorrow, you code it tomorrow.
More code = more testing = more bug prone = more refactoring = less efficient code reviews = higher cost of change in the future...
Sorry for the rant here but I fully agree with 'pfeds'- and not necessarily with the developers he is working with :)
guyroch:pfeds:Unfortunately for us we have legacy software and our developers are constantly saying "We know we'll need this in the future, so lets do it all now".
Shame on any developers that creates software parts for the future - guess what - the future might never come. And if they do so, then they are creating more issues for the future, and for what, for something they think they will need! I say if you don't have a use case today, you don't code it today. If you get a new use case tomorrow, you code it tomorrow.
More code = more testing = more bug prone = more refactoring = less efficient code reviews = higher cost of change in the future...
Sorry for the rant here but I fully agree with 'pfeds'- and not necessarily with the developers he is working with :)
There are times when you do, actually, know that you will need that functionality.Say you are upgrading a system from Borland Delphi using a Pervasive database to .Net and Micorsoft SQL server. You decide to do it in steps converting modules on a specified schedule.You do, in this case, know that you will need all the functionality of the previous system. You also know what additional functionality you are going to add (because you promised it).In those cases you can safely "design for the future". And using interfaces does give flexiblity to the intermediate steps.But outside of that, I agree with you.
A mock object must have the same interface as the real object because the client should be unaware whether it is using a mock or real object. You can achieve this by using abstract classes or interfaces. Interfaces are used to make sure that the mock objects have same interface as their real counterpart. If you create a mock library without using interfaces you could mess up. This would increase the time spent on debugging the mock library. IMO, using mock object improves your code because it forces you to program to an interface.
The other people on your team are correct. One of the basic principles of OOP is to program to and interface not an implementation. By interface, I mean an abstract class or interface. This will make your code more flexible and easier to maintain. Of course you will need to program to concrete classes some times; however, you should limit this as much as possible.
JHurrell:What is their reasoning for wanting to use mock objects? Is it solely for testing?
I just want to make sure that before we give you any more advice, we clearly understand why they're pushing the interface route.
Copyright (c) Marimer LLC