Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Firstly.... sorry for the length of this. Please keep reading.

I'm looking for some advice on how best to refactor a project that I am building in order to maintain the projects simplicity.

I'm building a blogging engine.... partly as a learning exercise and partly as all the open source solutions I've found are either feature incomplete or have been cobbled together out of code of varying (sometimes pretty poor) quality. The idea is that I can then give something back to the community.

I've started by building my data access layer using the repository pattern whilst utilizing generics to allow me to more easily add features to the engine.

On top of that I've defined a blog provider interface from which I can define custom providers to allow flexibliy of persistance methods (I'm defaulting with XML storage). These would then be called by a static service class which determines which provider to use based on a value in my web.config

The problem I am now having is with the complexity of these providers. Whilst the basic CRUD features are simple and interchangeable I've been
writing type specific methods to instruct my generic persistance classes.

This would be fine if I only had one or two types to persist but now I'm looking at host, blog, blogsettings, post, category, tag with more to follow.

This means that for every type I'm adding that's 4 maybe 5 methods to add to my provider interface, then to my abstract provider base class then to my providers. Then i have to add the methods to call the provider methods to my service class. It's all starting to get a bit silly.

My first thoughts would be to change my methods in my service class to accept a type parameter so rather than:

Blog SelectBlog (Guid id){
    provider.selectBlog(id);
}


and

Post SelectPost (Guid id){
    provider.selectPost(id);
}


I would instead use something like:

IEntity SelectObject (Type type, Guid id){
// IEntity is my basic interface required for all persistable types....
}


With another method in my class that would enumerate through known types in my project and choose the correct persistance method to call.

Whadya reckon?

Thanks for reading!

James
Posted

1 solution

Hello JimBob,

Something I see is the method naming in the providers. Like the blog provider has a SelectBlog and a Post provider has a SelectPost method. If you have control over these providers I would recommend giving them generic names, like a simple method Select. You would then get the same as the CRUD features, a more generic way of methods that can be used.
Example: BlogProvider.select(guid);
Result : The Blog of type IEntity

You could then use a simple class holding these providers and use this, like:

IEntity blogEntity = ProviderCollection.Blog.select(guid);
IEntity postEntity = ProviderCollection.Post.select(guid);

For specific provider methods you can create properties that tell you what capabilities the provider supports. This is like the stream type. You could add properties that indicate if the provider supports specific methods. Like a method AddComment that could be available for a Blog and a Post. All should have a property CanComment that is only true for Blog and Post.

By the way. Providers blog and blogsettings are separated, but aren't the settings kind'a related to the specific blog provider used? Could blogsettings not just be a part of blog? So simply blog.settings? Also adding a property HasSettings to check if settings are supported. The settings itself could be a simple list of definitions with Name, Value and Type. A generic class could build a page to show and save these settings and this way provide generic setting capabilities for all classes that support this.

Well, maybe this text helps give you some ideas... or raises more questions than you had before ;)

Good luck!
 
Share this answer
 
Comments
JimBob SquarePants 16-Jul-10 12:23pm    
Thanks for that...... I'll have a go tonight at trying to impliment some of your ideas.

Blogsetting and blog are separate at the moment as I was using a generic serializer for persisting the blog to xml but reflection to persist the blog setting as the serializer would not natively handle all the properties (like TimeSpan). I was also keeping the blogsettings as a singleton instance.

It would be nice to be able to combine the two though!

I'll let you know how I get on.

Cheers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900