Guidelines for writing a framework - frameworks

I'm faced with writing a framework to simplify working with a large and complex object library (ArcObjects). What guidelines would you suggest for creating a framework of this kind? Are static methods preferred? How do you handle things like logging? How do you future proof your framework code from changes that a vendor might introduce?
I think of all of the various wrappers and helpers I've seen for NHibernate, log4net, and code I've read from projects like NLog and NetTopologySuite and I see so many good approaches, but honestly I'm at a loss where to start.
BTW - I'm working in C# 3.5 but it's more about recommended approach rather than language.

Brad Abrams' Framework Design Guidelines book is all about this. Might be worth a look.

Try to write code to be more flexible. For example, if you have a method that accepts an array as a parameter, would you be able to accept an IEnumerable or IList instead?

I think that you're consistent is more important than what conventions you go with. As far as future-proofing yourself, that's a matter of the code that you're making a framework for. It's a lot easier to build on a brick house than a sand one.

Writing code for framework is absolutely very different from writing application code.
I have always consulted (and have others consult) the Design Guidelines for Class Library Developers when writing framework level code.

Related

WF4 Custom Persistence Examples

I am writing my own custom persistence instance store for WF4, based on the XmlWorkflowInstanceStore found in the .NET 4 WF and WCF samples. This sample is quite simplistic and the xml is produces is quite verbose. I have issues with how some of the objects are serialized.
I have tried using Red Gate Reflector to understand the Sql implementation used, but it is quite complex and difficult to learn from. The MS documentation for this is rather limited - often giving one sentence descriptions for complex methods.
Please could you point me at other examples of WF4 persistence (or proper documentation) around on the web that are not copy and paste versions of XmlWorkflowInstanceStore? Maybe someone else on StackOverflow has written their own?
You are completely correct that the docs are very much lacking here and the sample is of very limited use. I have started work on a custom instance store using the entity framework but, much like you discovered, found it slow going and am nowhere near anything I could use myself, let alone release onto CodePlex.
I am not aware of any blog posts or other information that help solve this.
You've probably seen this already, but I've found the code quite easy to understand: http://xhinker.com/post/WF4Xml-persistence-store.aspx
Ron Jacobs wrote an in memory persistence store for WF unit testing. Check out http://wf.codeplex.com/releases/view/73842

Charateristics and features of every software framework

I was trying to find an answer for my question today using google and StackOverflow search engines... but no luck :)
I was wondering what are the key features of every framework for end user, and how can you characterize every framework from the end-user point of view (I've looked into Framework Design Guidelines by K. Cwalina, but I've found only concepts and guidelines for framework architects), for me:
it should be extensible
should let build extensible and reusable components
and of course : Simple things should be simple, complex things should be possible. (http://en.wikiquote.org/wiki/Alan_Kay)
I think there is much more out there.
Please share your knowledge.
Here are two good quotes from Ralph Johnson and Brian Foote
A framework is a reusable, ``semi-complete'' application that can be specialized to produce custom applications
One important characteristic of a framework is that the methods defined by the user to tailor the framework will often be called from within the framework itself, rather than from the user's application code. The framework often plays the role of the main program in coordinating and sequencing application activity. This inversion of control gives frameworks the power to serve as extensible skeletons. The methods supplied by the user tailor the generic algorithms defined in the framework for a particular application.
There was a special issue of CACM that you might want to take a look at too.
Here's one more link The Hollywood Principal. "Don't call us, we'll call you." About how the framework inverts the typical control so the framework calls your code instead of you calling some library code.
It sounds like you just want to know what is the defining characteristic of a framework...?
From FOLDOC:
In object-oriented systems, a set of classes that embodies an abstract design for solutions to a number of related problems.
So basically, a class library ("set of classes") that's extensible ("embodies an abstract design"),
I'll attempt a definition based on my own understanding: A framework is a body of code that abstracts a subset of tasks common to some classes of application programs. The intent is to provide, once, proven and tested code so that application programming doesn't have to keep re-inventing code for the common tasks handled by the framework.
In real life, frameworks often spring into being when a programmer or team get carried away with generalizing and future-proofing what started out as a single application. There's an honorable intent to start code re-use, but it often turns out that such frameworks aren't designed with intent from the beginning, don't have consistent design reflecting this intent, and are actually lousy code that ends up not being re-used at all. Most architects who feel qualified to create frameworks, aren't.
The difference between libraries and frameworks: You call libraries. Frameworks call you.
Answering your question covering the depth it deserves is beyond the scope of this forum. All you should do is read this book though its focused on .Net frmaework specifically and written by the designers of the .Net framework, I'm sure the wisdom and information that this book provides would be sufficient to answer your question and satisfy your curiosities on the subject.
Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries (2nd Edition)
alt text http://www.lybrary.com/images/0321605012.jpg
A Framework is for me a other name for a Library (like Boost and many others) that is not about only one Topic (there are Librarys about Math, Networking, whatever out there, but these are no Frameworks) and it is of course Extensible and you can combine the Features of it to do your Job.

Framework vs. separate libraries

I'm using C++, but I think that my question goes beyond a single programming language.
What is better - use framework's classes or separate libraries. For example, if I'm using Qt in some project is it better to use QHttp or use cURL (QtXml or TinyXML etc.)?
From my point of view using framework's classes has following features:
Better compatibility with other framework's classes (for example, GUI)
Less dependences
But from other hand separate library could provide better functionality.
What do you think about it?
I get nervous about too much 'framework,' as at some point it can become impossible to extract your code from the 'framework'.
Using different libraries from one 'framework' is fine, but I'd hide them behind my own abstractions rather than routing 'their' types through my core code.
At my company, it depends on the needs of the project. Generally we prefer to use the framework classes. But if it seems that we will have write a lot of extensions or helper classes, then we look for separate libraries.
I tend to go for "what provides the best functionality?". I'll use a framework's methods mostly by default unless it doesn't provide something I need. Then I'll be tempted to use a third party library. If I'm using a lot of third party libraries, I might question the need to use the framework. If I'm using third party libraries I can't live without and they conflict with the framework, the framework goes, unless I can't live without it. It really depends on the situation. Is it the framework I need to accomplish my task, or the third party librar(y|ies) implementing the functionality? Inclusion is then prioritised as appropriate.
It's the job of the framework author to lock you in. It's the job of the application writer not to get locked in.

What are the advantages of using Framework over Library or other way round while developing Software?

What are the advantages of using Framework over Library or other way around while developing Web Applications or different types of Software.
I understand using Framework we can make use of basic functionality and then add upon functionality which we require but I am having hard time understanding the advantages of it as if we develop using Library than also we get some basic functionality and then add upon them the functionality as we need and so where in comes the actual advantages of using Framework.
I think it's important to distinguish between a framework and a library when answering.
A framework follows the Hollywood principle: "Don't call us; we'll call you." You plug your code into the framework according to its API. The framework acts as a constraint that solves the particular problem it was designed to solve (e.g., web application development).
A framework will use a combination of your code and 3rd party libraries to solve a particular problem. It will treat your code as one of those 3rd party libraries.
If you eschew a framework, the roles are reversed. Now you and your code are in charge: "I'll do the calling, thank you." A library is a self-contained piece that plugs into the software that you write.
So why prefer a framework? Use one that is written better than the scaffolding that you would be able to write yourself. A framework is likely to be tested more thoroughly and have a wider user base than code you'll write.
You'd write an application without a framework if you're working on a specialized problem, you have deep knowledge of the domain, and there are no frameworks available that demonstrate deeper insight than you have.
A library is just something that solves a problem but it is your job to integrate it the project. Basically it offers some feature that are context-free.
A framework will provide the features plus an infrastructure for them - you will have to develop according to the framework's rules. So you might have less control and freedom but you save time.

Difficulties when learning Zend Framework 1.8

I am a newbie of Zend Framework.
I downloaded the Zend Framework and then followed the official quickstart tutorial to build a very simple registration form. But after that, I find very hard to learn different elements of Zend Framework.
Many tutorials mention frontController and registerAutoload() in the bootstrap file. However, it seems that I can't see it again in the code in version 1.8 (both in the official quickstart tutorial and Getting Started with Zend Framework 1.8). Many tutorials contain the old version of code and it seems very different in v1.8.
I start with writing registration form with password confirmation. I read the official document and find the custom validator for password confirmation. It just puts the class there and doesn't mention how I can use it. I don't know where I should put this file and I can't find any hints in Google. The "class not found" error always makes me sick (I've tried addPrefixElement, set_include_path but they don't work).
User registration, email activation, login, access control are very common tasks. But I don't even find a piece of sample code in v1.8 that I can run in my machine. I am very frustrated about Zend Framework.
Does anyone give me some advices?
I have also just started learning the zend framework. I also find alot of old tutorials that make learning confusing and hard,
but there are some resources that I find help in learning the zend framework:
http://www.zendcasts.com/ - they have tutorials on ZF1.8 already, like Bootstrapping using Zend_Application, etc.
Some useful blogs that might help:
http://weierophinney.net/matthew/
http://www.thomasweidner.com/flatpress/index.php
http://smartycode.com/zf/
http://codeutopia.net/blog/
Forums to ask your questions:
http://www.nabble.com/Zend-Framework-Community-f16154.html - alot of ZF developers seem to be here to answer your questions :) much more than the official Zend Forums below ...
http://forums.zend.com/viewforum.php?f=69
As to class not found, I think you didn't autoload?
http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html
Parts of Zend Framework have changed slightly in the 1.8 release but everything should be backwards compatible so all code example written using the 1.* release series should still work with little or no modification. The main areas that are different are
Zend Loader (related to register_autoload) which now has several improvements related to performance and some slight differences in its API (with a PHP warning if you use the old way). I wouldn't worry about this too much as a beginner - just follow the tutorial or continue to use include/require statements until you become annoyed with adding them all the time!
Zend Application which basically allows you to create your set-up and initialisation code with less fuss. Again, you don't have to use it and you can happily write a manual bootstrap class to get you used to how the framework is put together.
With regards to getting started I would highly recommend getting your head around the Zend Controller component, particularly the front controller part and this diagram.
For authentication look at Zend Auth, for e-mail see Zend Mail, for access control see Zend Acl and for forms and validation see Zend Form, Zend Validate and Zend Filter. The form and validation components will explain about paths and adding your own custom validators and form elements.
The other area of importance is Zend Db which allows you to persist your data to a database. There is also Zend View which represents the view layer in the MVC stack. In my opinion these are the key components of a basic ZF application. From there you can explore the other components as and when you need them.
Don't forget that the Zend Framework is specifically designed to be loosely coupled and it is very easy to use a different component from another project for a particular task. Sometimes it is necessary to write your own components too. If there is a component you would prefer to use over the Zend component then go ahead and do so.
Honestly, the documentation is very good although it is hard to navigate for beginners. Once you get your head around the core components everything will become a lot clearer.
Also try following this tutorial. It will help explain a lot of the basics.
I recommend these video tutorials pu up by a guy called Alex, he keeps them up to date and releases new ones all the time,they are all relevant to ZF 1.8 and 1.9.
He has even a detailed video tutorial covering ACL, login forms etc. (my first steps with Zend were easier thanks to him!)
http://alex-tech-adventures.com/development/zend-framework.html?start=15
- his site is a bit comfusing but worth the time in figuring it out :)
Cheers
Roman
Some tips:
Here is a nice diagram of the Dispatch process
http://nethands.de/download/zenddispatch_en.pdf
It is much easier to understand ZF if you are experienced in OOP / OOP Design Patterns
When I first stumbled across ZF I had little knowledge of OOP. ZF prompted me to learn OOP which was a very good thing.
Distinguish between the 'magic' of ZF and the actual ZF Architecture
Although I have a good understanding of ZF Classes I still get confused when ZF adds its 'magic' to the mix.
When I say 'magic' I am referring to things like
the default objects loaded and used if none specified, and
url-controllername-filepath-filename inflections / naming conventions.
config settings-to-class-instantiation mappings
ZF is a bit like a crack-dealer in the sense that the 'Getting Started Tutorials' get you hooked straight away. They perform so much magic that it makes you think
.oO(Wow - how simple was that! This framework is cool!)
Then as soon as you start to try to do your own thing - the magic starts to get really confusing.
So - my tactic is to remove as much ZF magic as possible. If you have the option to specify a class to use, or a router to use, then specify it. Don't let Zend do it for you.
Then when something weird is happening you can find the bugs more easily.
Also, if you specify the locations of classes/ paths as (opposed to letting ZF magically inflect paths and find scripts), then you do not have to worry about questions of 'where to put this? , where to put that?' - you just put things where you want them to be and explicitly point ZF to them.
As you get more familiar with ZF you might then want to let it take over and do some magic for you. Only then can you really understand why that magic is convenient.
As Tim Wardle said in another answer - favour require statements over zend loader until you really need it.
If you are a PHP design house that churns out 10 website projects a week, then ZF Magic can be really useful. If you are designing your first ZF app - then 'reducing complexity' is a more important design goal than 're-usability'.
Browse the code
It might seem obvious but one of the best ways to learn about the ZF is to look at the code.
Again the ZF magic can get in the way - I often want to look at a method's arguments to discover what i need to pass in only to discover that it expects an ambiguous 'options array' (not exactly helpful). After a while, though, you start to notice the conventions used in how an 'option key' marries up to a getter/setter method. So, keep looking at the code and familiarising yourself with the 'ZF way'.
I 'ope that 'elps.
The Front Range PHP Users Group website has some presentations on Zend Framework which may be of use.
I have so much sympathy with you Billy. I am a newbie and the confusion between the old way and the new way is almost just too much to handle. Also see here for someone who points out some of the main differences:
http://crossfunctional.wordpress.com/2009/05/
All documentation, particularly Zend's documentation is absolutely horrible. It all assumes that you "know" where to put the snippets they are referring to and how they work together. If I already knew those things, I wouldn't need the documentation. No full example applications with authentication etc. that you can download and modify. I'm seriously regretting putting any time into trying to learning this framework. All examples that I can find are apparently for older versions. I followed one for authentication on youtube that was designed for 1.8 to the letter. It fails to work in 1.9.x. Can't get it to work for the life of me. Completely frustrated with Zend Framework!
Zend Framework has shifted from being a relatively accessible and simple system to a more complex entity. It has gone through a number of rapid developments over the past couple of years which have left a lot of older documentation around.
We have projects that are stuck on Version 1.7.x because the differences between that and the latest version are too great which makes the dev and testing time too expensive.
I like Zend Framework because it has so much depth to it but it is definitely a two edged sword for beginners.
I'd certainly recommend making use of the expertise of other users and search out some simple framework examples. ZF is highly adaptable but you need to try and work from the simplest case possible for your needs.
I think Matthew Weier o'Phinney, (search for 'Phly, boy, phly') is one of the more approachable members of the dev team and has loads of examples and ideas on his website.
Good luck!