Data Access Layer

From Znode Knowledge Base
Jump to: navigation, search

Znode uses a multi-tier DAL (Data Access Layer). This layer exposes strongly typed DataSets that make it easier and more efficient to pass entities between the Business Logic Layer and the DAL. You can either modify the DAL libraries manually or use Code Smith templates to re-generate them based on the database schema.

  • Catalog Data is stored in a Mongo 3.0 database and Customer Data is stored in a SQL Server 2008 database
  • Data is accessed using stored procedures. Znode does not use dynamic SQL.
  • Znode uses XML Serialization extensively to retrieve object-oriented data from the database. This reduces the number of calls back and forth from the database.


Znode DAL

The Znode DAL includes 3 different C# libraries that can be modified independently:

Detail Description
ZNode.Libraries.Data Library defines the strongly typed Entity Model. Ex:The Order Entity maps all the fields to the Znode Order table.
Znode.Engine.Services Library defines methods to retrieve data from the SQL Server database. The service methods return strongly typed Entity objects
ZNode.Libraries.MongoDB.Data Library defines the strongly typed Entity to the Mongo DB.


View of the Znode DAL Libraries

Znodedallibraries.png

Example of a Native Data Access Method Using the Znode DAL

private void ExpandOmsOrderNote(NameValueCollection expands, OrderModel order)
=> order.OrderNotes = !String.IsNullOrEmpty(expands.Get(ExpandKeys.OrderNotes)) ?
           GetOrderNoteDetails(order.OmsOrderId, 0) : new List<OrderNotesModel>();

Example of a Custom Data Access Method in MongoDB

public virtual void Create(T entity)
{
try
{
           	// Save the entity with safe mode (WriteConcern.Acknowledged)
           	var result = this.MongoConnectionHandler.MongoCollection.Save(
               entity,
               new MongoInsertOptions
               {
                   	WriteConcern = WriteConcern.Acknowledged
               });
           	if (result.HasLastErrorMessage)
               throw new Exception(result.LastErrorMessage);
}
catch (Exception ex)
{
           	throw ex;
}
}

Example of a Custom Data Access Method in SQL

public virtual IEnumerable<T> Insert(IEnumerable<T> entities)
{
try
{
               if (Equals(entities, null))
               throw new ArgumentNullException(nameof(entities));
           	_context.Set<T>().AddRange(entities);
           	_context.SaveChanges(HelperMethods.GetLoginUserId());
           	return entities;
}
catch (Exception ex)
{
           	//Remove added entities from objects for all the entities tracked by ontext.
           	Entities.RemoveRange(entities);
           	EntityLogging.LogObject(typeof(IEnumerable<T>), entities, ex);
           	throw ex;
}
}

Related Links