Executing and using queries
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.
You should Execute queries only in Application Tasks, and only in these 2 places:
- in the LoadAdditionalInitialData method of an Application Task
Example:
public class CalculatedPrimitiveTask2 : BaseApplicationTask<decimal> { private readonly DiscountLevel2ListTask _discountLevel2ListTask; private readonly MaxDiscountPercentage _maxDiscountPercentage; private Client2 _client; private IList<DiscountLevel2> _discountLevel2List; public CalculatedPrimitiveTask2(INHUnitOfWorkProvider uowProvider, DiscountLevel2ListTask discountLevel2ListTask, MaxDiscountPercentage maxDiscountPercentage) : base(uowProvider) { _discountLevel2ListTask = discountLevel2ListTask; _maxDiscountPercentage = maxDiscountPercentage; } public decimal Execute(Client2 client) { _client = client; ExecuteInternal(); return Entity; } protected override void LoadAdditionalInitialData() { _discountLevel2List = _discountLevel2ListTask.Execute(); } protected override void ExecuteBusinessLogic() { Entity = _client.CalculateThe(_maxDiscountPercentage, _discountLevel2List); } }
DiscountLevel2ListTask is an AllItemsTask, which is a wrapper for an AllItemsQuery (which loads all the entities of a certain type):
public class DiscountLevel2ListTask: AllItemsTask<DiscountLevel2> { public DiscountLevel2ListTask(INHUnitOfWorkProvider uowProvider, AllItemsQuery<DiscountLevel2> allItemsQuery) : base(uowProvider, allItemsQuery) { } }
- in a Query Task
Example:
The AggregateListQuery1 is executed in the ExecuteQuery method of the QueryTask:
public class AggregateListTask1 : BaseListQueryTask<CustomerOrder1> { private readonly AggregateListQuery1 _aggregateListQuery1; private decimal _minPrice; public AggregateListTask1(INHUnitOfWorkProvider uowProvider, AggregateListQuery1 aggregateListQuery1) : base(uowProvider) { _aggregateListQuery1 = aggregateListQuery1; } public IList<CustomerOrder1> Execute(decimal minPrice) { _minPrice = minPrice; ExecuteInternal(); return Entity; } protected override IList<CustomerOrder1> ExecuteQuery() { return _aggregateListQuery1.Execute(UOW.Session, _minPrice); } }
public class AggregateListQuery1: MultiCriteriaQuery<CustomerOrder1> { private decimal _minPrice; public IList<CustomerOrder1> Execute(ISession session, decimal minPrice) { _minPrice = minPrice; return Load(session); } protected override void ApplyMainFilters(IListMultiCriteriaFetcher<CustomerOrder1> criteria) { criteria.Where(p => p.OrderPrice).Gt(_minPrice); } protected override void FilterOnlyActive(IListMultiCriteriaFetcher<CustomerOrder1> criteria){} protected override void Sort(IListMultiCriteriaFetcher<CustomerOrder1> criteria) { criteria.OrderAscBy(p => p.OrderPrice); } }
IT IS YOUR RESPONSABILITY TO NOT EXECUTE QUERIES ANYWHERE ELSE WHEN WORKING WITH FWKLIGHT.
The Query uses an Execute convention, and one of the parameters of the Execute method should be the current NHibernate Session (the Session property of the current UnitOfWork, which is offered by the Application Task through the UOW property – example above).
About this entry
You’re currently reading “Executing and using queries,” an entry on The FwkLight blog
- Published:
- April 27, 2010 / 1:19 pm
- Category:
- Uncategorized
- Tags:
No comments yet
Jump to comment form | comment rss [?] | trackback uri [?]