Coding conventions
ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST.
The Execute method
The convention is:
- the method represents the entry point for a type of service with a variable signature
- inside the method, you do 3 things
- set private fields from the parameters received
- launch the internal logic of the service
- return what needs to be returned
You have to remember to launch the internal logic, otherwise the service will do nothing.
The services which are currently using this convention are:
- Application Tasks
Example:
public class SomeEntityLoadTask: BaseLoadTask<SomeEntity> { private int _id; public SomeEntityLoadTask(IBaseRepo<SomeEntity> repository, INHUnitOfWorkProvider uowProvider) : base(repository, uowProvider) { } public SomeEntity Execute(int id) { _id = id; ExecuteInternal(); return Entity; } protected override void ConfigureFilters() { Fetcher.Where(p => p.Id).Eq(_id); } }
- Queries
Example:
public class SomeEntityListQuery: MultiCriteriaQuery<SomeEntity> { private IList<int> _ids; public IList<CodingConvention> Execute(ISession session, IList<int> ids) { _ids = ids; return Load(session); } protected override void ApplyMainFilters(IListMultiCriteriaFetcher<SomeEntity> criteria) { criteria.Where(p => p.Id).In(_ids); } protected override void FilterOnlyActive(IListMultiCriteriaFetcher<SomeEntity> criteria){} protected override void Sort(IListMultiCriteriaFetcher<SomeEntity> criteria){} }
Why this solution?
Because it maintains the consistency and user friendliness of the public interface that the service offers, which is the most important thing: we cannot use the service without calling Execute, and all the service needs to run correctly is in the Execute signature. The drawback is that when you write the service you have to remember to launch the internal logic after you process the input, but once you get used to the convention this becomes a habit.
If we would use the same signature for all services of this type, we would have to send the custom parameters that each of them need in another way (for example, by using an AddDependencies method); in this case we would lose the advantages of the first method, because each time we use the service we have to remember to call AddDependencies before we call Execute.
If we would still let each class have its own signature, in order to make it launch the internal logic automatically we would need an additional mechanism. This would be the ideal solution, but it’s not yet implemented in FwkLight.
Base classes and extension points
You use extension points of base classes from FwkLight by overriding their methods. When doing this, you never have to call base.Something(). The framework is designed like that to avoid situations in which you would forget to make the call, and lose time tracking the error.
About this entry
You’re currently reading “Coding conventions,” an entry on The FwkLight blog
- Published:
- April 21, 2010 / 2:24 pm
- Category:
- Uncategorized
- Tags:
No comments yet
Jump to comment form | comment rss [?] | trackback uri [?]