I have this query
Code:
ICriteria itemTranslationCriteria = NHibernateSession.CreateCriteria(typeof(Hairless.Entities.Translation.ItemTranslation), "it");
ICriteria itemCriteria = itemTranslationCriteria.CreateAlias("Item", "i");
itemCriteria.CreateAlias("i.Brand", "b");
itemCriteria.CreateAlias("i.Stores", "s");
itemCriteria.CreateAlias("i.SubCategory", "sub");
if (filters != null)
{
foreach (ItemFilter filter in filters)
{
if (filter is DepartmentFilter)
{
itemCriteria.CreateAlias("i.Departments", "d");
}
filter.Execute(itemCriteria);
}
}
itemCriteria.CreateAlias("s.Currency", "cur") // Store filter. Default. Must be handled by developer.
.CreateAlias("i.Images", "img")
.Add(NHibernate.Expression.Expression.Eq("img.SortOrder", 0)) // Only get the first image. Not the best solution
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("i.Id"), "Id")
.Add(Projections.Property("i.Price"), "Price")
.Add(Projections.Property("i.MemberPrice"), "MemberPrice")
.Add(Projections.Property("i.Junior"), "Junior")
.Add(Projections.Property("i.Female"), "Female")
.Add(Projections.Property("i.Male"), "Male")
.Add(Projections.Property("it.Name"), "Name")
.Add(Projections.Property("b.Name"), "BrandName")
.Add(Projections.Property("img.File"), "Image")
.Add(Projections.Property("c.Identifier"), "CollectionName") // We got this from Outlet filter. A default in every search
.Add(Projections.Property("c.Discount"), "CollectionDiscount") // We got this from Outlet filter. A default in every search
.Add(Projections.Property("i.Discount"), "ItemDiscount")
.Add(Projections.Property("cur.Suffix"), "CurrencySuffix")
.Add(Projections.Property("cur.Prefix"), "CurrencyPrefix"))
.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(Hairless.Communication.ItemBase)));
// Execute Sort Filters if there is any
// See ( Hairless.Entities.Sort namespace )
if (sorts != null)
{
foreach (ItemSort sort in sorts)
{
sort.Execute(itemCriteria);
}
}
return itemCriteria;
In my foreach loop i iterate over 1-15 filter that limits the hits. My problem is that I need in my Filter classes to know what alias I have used.
Example. I have set an alias for Item to i. Now I need to know that I used alias i in my filter. Not very loosly copuled. And when I create an alias in my filter I must know that no alias has been made for the collection class.
Code:
public class OutletFilter : ItemFilter
{
private Boolean outlet;
public OutletFilter(Boolean outlet)
{
this.outlet = outlet;
}
public void Execute(NHibernate.ICriteria criteria)
{
ICriteria collectionCriteria = criteria.CreateAlias("i.Collection", "c");
if (outlet)
{
collectionCriteria.Add(NHibernate.Expression.Expression.Lt("c.StartDate", DateTime.Now));
collectionCriteria.Add(NHibernate.Expression.Expression.Lt("c.EndDate", DateTime.Now));
}
else
{
collectionCriteria.Add(NHibernate.Expression.Expression.Le("c.StartDate", DateTime.Now));
collectionCriteria.Add(NHibernate.Expression.Expression.Ge("c.EndDate", DateTime.Now));
}
}
If I have created an alias for the collection I'll get an hibernate exception.
Can this be done without using alias or can I check if an alias is made?