zaterdag 19 december 2009

FW: Core Module

As described in my first blog post, I'm going to give you a background about all important modules. This blog post explains what the Core module does.


At the heart of the whole framework is the Core assembly of the Core module. It contains the ObjectTree, which is the main power behind all the magic being done by the whole framework.

ObjectTree sample
So what does the ObjectTree actually let you do, you might wonder.

Think of a simple Wordpad alternative project. The base project might just support no file formats at all, but let AddIns handle that. Each file format would be represented by an implementation of the following interface:

public interface IFileFormat
{
string Extension
{
get;
set;
}

string Description
{
get;
set;
}
void Save(RichTextBox aTextBox, string aFile);
void Load(RichTextBox aTextBox, string aFile);
// further members
}

(Disclaimer: this is a sample of how to use the ObjectTree, not of perfect programming)
When now implementing the Save menu item of the application, you would populate the Filter of the SaveDialog control, by iterating the ObjectTree for all supported FileFormats:

var xFormatsItem = DSManager.RootManager["/FileFormats"];
mSaveDialog.Filter.Clear();
foreach (var xFormatItem in xFormatsItem.ChildItems)
{
var xFormat = xFormatItem.Content as IFileFormat;
if (xFormat != null)
{
mSaveDialog.Filter.Add(xFormat.Extension, xFormat.Description);
}
}
if (mSaveDialog.Execute())
{
// etc
}
Second important thing in the Core assembly of the Core module, is the ServiceFactory. It's incredibly simple, but really nice to have.
Let's assume your application needs to send out SMS messages. You found a company hosting a webservice which can send the messages for you. You now define an interface for the service:


public interface ISMSService
{
void SendSMS(string aDestination, string aMessage);
}
You put that in an assembly, without an implementation. Now you can have your SMS sending code use ServiceFactory.GetService<ISMSService>().SendSMS to send SMS messages. The ServiceFactory uses the ObjectTree for getting a real implementation at runtime.

Now you implement the SMSService for the webservice provider you found. You put the implementation in a different assembly. You make sure the AddIn containing the implementation of the service is getting loaded at startup of your application and it works.

Now at some point in time you find a different company hosting an SMS sending proxy, also accessible using a webservice, which would save you a lot of money. You can now make a second implementation, in a different assembly than the other implementation. You make sure it gets registered correctly in the ObjectTree. Now only thing to do is make sure the old implementation doesnt get loaded, and the new one does. That's it.

Now, you might wonder how to get addins loaded. Well, that's the next important part of the Core module, is the AddInEngine. The AddInEngine lets assemblies put objects in the ObjectTree. That is, the AddInEngine would load the SMSService implementation addin.

Geen opmerkingen:

Een reactie posten