DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • How To Validate Names Using Java
  • How to Connect to Splunk Through Anypoint Studio in 10 Steps
  • Strategies for Improving the Performance of Applications Using EF Core
  • Optimizing Data Repositories Usage in Java Multi-Threaded Applications

Trending

  • Spring Authentication With MetaMask
  • Agile Estimation: Techniques and Tips for Success
  • Spring WebFlux Retries
  • Log Analysis Using grep
  1. DZone
  2. Data Engineering
  3. Data
  4. The wages of sin: Proper and improper usage of abstracting an OR/M

The wages of sin: Proper and improper usage of abstracting an OR/M

Oren Eini user avatar by
Oren Eini
·
Mar. 22, 11 · News
Like (0)
Save
Tweet
Share
3.53K Views

Join the DZone community and get the full member experience.

Join For Free

This time, this is a review of the Sharp Commerce application. Again, I have stumbled upon the application by pure chance, and I have very little notion about who wrote it.

In this case, I want to focus on the ProductRepository:



In particular, those methods also participate in Useless Abstraction For The Sake Of Abstraction Anti Pattern. Here is how they are implemented:

public AttributeItem GetAttributeItem(int attributeItemId)
{
return Session.Get<AttributeItem>(attributeItemId);
}

public Attribute GetAttribute(int attrbuteId)
{
return Session.Get<Attribute>(attrbuteId);
}

public IEnumerable<Attribute> GetAllAttributes()
{
return Session.QueryOver<Attribute>()
.Future<Attribute>();
}

public void SaveOrUpdate(Attribute attribute)
{
Session.SaveOrUpdate(attribute);
}


And here is how they are called (from ProductService):

public AttributeItem GetAttributeItem(int attributeItemId)
{
return productRepository.GetAttributeItem(attributeItemId);
}

public Attribute GetAttribute(int attrbuteId)
{
return productRepository.GetAttribute(attrbuteId);
}

public void SaveAttribute(Attribute attribute)
{
productRepository.SaveOrUpdate(attribute);
}

public IList<Product> GetProducts()
{
return productRepository.GetAll();
}

public Product GetProduct(int id)
{
return productRepository.Get(id);
}

public void SaveOrUpdate(Product product)
{
productRepository.SaveOrUpdate(product);
}

public void Delete(Product product)
{
productRepository.Delete(product);
}

public IEnumerable<Attribute> GetAllAttributes()
{
return productRepository.GetAllAttributes();
}


Um… why exactly?

But as I mentioned, this post is also about the proper usage of abstracting the OR/M. A repository was originally conceived as a to abstract away messy data access code into nicer to use code. The product repository have one method that actually do something meaningful, the Search method:

public IEnumerable<Product> Search(IProductSearchCriteria searchParameters, out int count)
{
string query = string.Empty;
if (searchParameters.CategoryId.HasValue && searchParameters.CategoryId.Value > 0)
{
var categoryIds = (from c in Session.Query<Category>()
from a in c.Descendants
where c.Id == searchParameters.CategoryId
select a.Id).ToList();

query = "Categories.Id :" + searchParameters.CategoryId;
foreach (int categoryId in categoryIds)
{
query += " OR Categories.Id :" + categoryId;
}
}

if (!string.IsNullOrEmpty(searchParameters.Keywords))
{
if (query.Length > 0)
query += " AND ";

query += string.Format("Name :{0} OR Description :{0}", searchParameters.Keywords);
}

if (query.Length > 0)
{
query += string.Format(" AND IsLive :{0} AND IsDeleted :{1}", true, false);

var countQuery = global::NHibernate.Search.Search.CreateFullTextSession(Session)
.CreateFullTextQuery<Product>(query);

var fullTextQuery = global::NHibernate.Search.Search.CreateFullTextSession(Session)
.CreateFullTextQuery<Product>(query)
.SetFetchSize(searchParameters.MaxResults)
.SetFirstResult(searchParameters.PageIndex * searchParameters.MaxResults);

count = countQuery.ResultSize;

return fullTextQuery.List<Product>();
}
else
{
var results = Session.CreateCriteria<Product>()
.Add(Restrictions.Eq("IsLive", true))
.Add(Restrictions.Eq("IsDeleted", false))
.SetFetchSize(searchParameters.MaxResults)
.SetFirstResult(searchParameters.PageIndex * searchParameters.MaxResults)
.Future<Product>();

count = Session.CreateCriteria<Product>()
.Add(Restrictions.Eq("IsLive", true))
.Add(Restrictions.Eq("IsDeleted", false))
.SetProjection(Projections.Count(Projections.Id()))
.FutureValue<int>().Value;

return results;
}
}

 

I would quibble about whatever this is the best way to actually implement this method, but there is little doubt that something like this is messy. I would want to put this in a very distant corner of my code base, but it does provides a useful abstraction. I wouldn’t put it in a repository, though. I would probably put it in a Search Service instead, but that isn’t that important.

What is important is to understand where there is actually a big distinction between code that merely wrap code for the sake of increasing the abstraction level and code that provide some useful abstraction over an operation.

Abstraction (computer science) Data access application Repository (version control) Notion (music software) Data (computing) POST (HTTP)

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Validate Names Using Java
  • How to Connect to Splunk Through Anypoint Studio in 10 Steps
  • Strategies for Improving the Performance of Applications Using EF Core
  • Optimizing Data Repositories Usage in Java Multi-Threaded Applications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: