HSS Core DataAccessService

The HSS Data Library also includes a DataAccessService.

The DataAccessService wraps the DataAccess class and abstracts out basic CRUD functions.

Please see the developer reference for more details hsscore-help.chm
  • Get
  • GetList
  • GetCollection
  • GetHashSet<>
  • Add
  • Update
  • Delete


Example of DataAccessService<IDataEntity>.GetCollection()
public IEnumerable<UserIdentity> SearchUsers(string companyName, string userGroupName, string userName)
{
    return DataAccessService<UserIdentity>.GetCollection("User_Search"
	, new DataParameter("ApplicationName", DataAccessContext.DefaultConnection.ApplicationName)
	, new DataParameter("UserName", userName)
	, new DataParameter("UserGroupName", userGroupName)
	, new DataParameter("CompanyName", companyName));
}

The requirements for using the DataAccessService, at minumum, is your Model's must implement the IDataEntity Interface. This
is where the DOMGen (Data Object Model Generator) comes in handy. For use with the GetX methods you must implement
the IDataEntity Interface. For Add, Update and Delete, there are several IAdd, IUpdate and IDelete Interfaces you can implement
to support the additional features of the DataAccessService. The Interfaces with 'Trx' will run with a SqlTransaction and expose
the internal SqlCommand (cmd) instance, otherwise will be run normal and expose the internal DataAccess (da) instance.

An expample of a UserIdentity Model with Transaction Support...

public sealed partial class UserIdentity : IDataEntity, IDataEntityAddTrx<Guid>, IDataEntityBatchAddTrx, IDataEntityUpdateTrx, IDataEntityDeleteTrx

public Guid AddNew(SqlCommand cmd, object[] state, ref int result)

public int Update(SqlCommand cmd, object[] state)

public int Delete(SqlCommand cmd)


And then calling the DataAccessService to add, update and delete a user...

DataAccessService<UserIdentity>.AddItemTrx<UserIdentity, Guid>(user, new object[] { userGroups, addToEveryoneGroup, newUserPassword }, ref result)

int retVal = DataAccessService<UserIdentity>.UpdateItemTrx(user, new object[] { addGroups, removeGroups });

int retVal = DataAccessService<UserIdentity>.DeleteItemTrx(user);


A complete example of a Model, generated from HSS DOMGen and consist of the Model Entity class and the Model Context class.

Model Entity File: PolicySettingDetail.cs
#region Using Directives
using System;
using Solarium.ComponentModel;
#endregion

#region PolicySettingDetail
/// <summary>
/// The PolicySettingDetail Object Model. Generate by HSS DOMGen v4.0
/// </summary>
public partial class PolicySettingDetail : Model
{
	#region Constructors
	/// <summary>
	/// Initializes a new instance of the PolicySettingDetail class.
	/// </summary>
	public PolicySettingDetail()
	{
	}
	#endregion

	#region Properties
	/// <summary>
	/// Gets the PolicyId value.
	/// </summary>
	public virtual Int32 PolicyId
	{
		get { return _policyId; }
		set
		{
			if (_policyId != value)
			{
				_policyId = value;
				this.OnPropertyChanged("PolicyId");
			}
		}
	} Int32 _policyId;

	// Trimmed....

	/// <summary>
	/// Gets the CompanyId value.
	/// </summary>
	public virtual Guid? CompanyId
	{
		get { return _companyId; }
		set
		{
			if (_companyId != value)
			{
				_companyId = value;
				this.OnPropertyChanged("CompanyId");
			}
		}
	} Guid? _companyId;
	/// <summary>
	/// Gets the CompanyName value.
	/// </summary>
	public virtual String CompanyName
	{
		get { return _companyName; }
		set
		{
			if (_companyName != value)
			{
				_companyName = value;
				this.OnPropertyChanged("CompanyName");
			}
		}
	} String _companyName;
	#endregion

	#region Overrides
	/// <summary>
	/// Returns the Primary Key of the object.
	/// </summary>
	/// <returns>String</returns>
	public override String ToString()
	{
		return "[PolicySettingDetail] " + this.SettingId.ToString();
	}
	#endregion
}
#endregion

Model Context File: PolicySettingDetail.Context.cs
#region Using Directives
using System.Data;
using HSS.Data;
#endregion

#region PolicySettingDetail
/// <summary>
/// The PolicySettingDetail Object Model IDataEntity implementation.
/// </summary>
public partial class PolicySettingDetail : IDataEntity, IDataEntityUpdate, IDataEntityAdd<int>
{
	#region Constructors
	/// <summary>
	/// Initializes a new instance of the PolicySettingDetail class.
	/// </summary>
	/// <param name="dr">A <see cref="System.Data.IDataRecord"/> containing the data for this Entity</param>
	public PolicySettingDetail(IDataRecord dr)
	{
		this.Load(dr);
	}
	#endregion

	#region IDataEntity Members
	/// <summary>
	/// Load this instance from a <see cref="System.Data.IDataRecord"/>.
	/// </summary>
	/// <param name="dr">A <see cref="System.Data.IDataRecord"/> containing the data to load this Entity with.</param>
	public void Load(IDataRecord dr)
	{
		this._policyId = dr.GetInt32(dr.GetOrdinal("PolicyId"));
		this._settingId = dr.GetInt32(dr.GetOrdinal("SettingId"));
		this._settingName = dr.GetString(dr.GetOrdinal("SettingName"));
		this._displayFormat = dr.GetString(dr.GetOrdinal("DisplayFormat"));
		this._settingValue = dr.GetString(dr.GetOrdinal("SettingValue"));
		this._valueTypeId = dr.GetInt32(dr.GetOrdinal("ValueTypeId"));
		this._valueType = dr.GetString(dr.GetOrdinal("ValueType"));
		this._maxValue = dr.GetInt32(dr.GetOrdinal("MaxValue"));
		this._minValue = dr.GetInt32(dr.GetOrdinal("MinValue"));
		this._settingDescription = dr.GetString(dr.GetOrdinal("SettingDescription"));
		if (!dr.IsDBNull(dr.GetOrdinal("CompanyId")))
		    this._companyId = dr.GetGuid(dr.GetOrdinal("CompanyId"));
		if (!dr.IsDBNull(dr.GetOrdinal("CompanyName")))
		    this._companyName = dr.GetString(dr.GetOrdinal("CompanyName"));
	}
	/// <summary>
	/// Add this item to the database.
	/// </summary>
	/// <param name="da">The current DataAccess instance</param>
	/// <param name="state">Optional state containing any additional data.</param>
	/// <param name="result">An ref parameter that will contain the return value from the database call.</param>
	/// <returns>The id of the newly added item</returns>
	public int AddNew(DataAccess da, object[] state, ref int result)
	{
		//@PolicyId		INT,
		//@SettingName		NVARCHAR(50),
		//@SettingValue		NVARCHAR(500)	= NULL,
		//@DisplayFormat		NVARCHAR(128)	= NULL,
		//@ValueType		NVARCHAR(128)	= NULL,
		//@MinValue		INT		= NULL,
		//@MaxValue		INT		= NULL,
		//@SettingDescription	NVARCHAR(256)	= NULL,
		//@CompanyId		UNIQUEIDENTIFIER	= NULL

		da.AddWithValue("PolicyId", this.PolicyId);
		da.AddWithValue("SettingName", this.SettingName);
		da.AddWithValue("SettingValue", this.SettingValue ?? string.Empty);
		da.AddWithValue("DisplayFormat", this.DisplayFormat);
		da.AddWithValue("ValueType", this.ValueType);
		da.AddWithValue("MinValue", this.MinValue);
		da.AddWithValue("MaxValue", this.MaxValue);
		da.AddWithValue("SettingDescription", this.SettingDescription);

		if (this.CompanyId.HasValue)
		    da.AddWithValue("CompanyId", this.CompanyId.Value);
		else
		    da.AddWithValue("CompanyId", null);

		da.ExecNonQuery("PolicySetting_Set", ref result);

		return 0;
	}
	/// <summary>
	/// Update this item in the database.
	/// </summary>
	/// <param name="da">The current DataAccess instance</param>
	/// <param name="item">The item instance containing the updated information</param>
	/// <returns>The return value from the sproc call</returns>
	public int Update(DataAccess da, object[] state)
	{
		int result = -99;

		//@PolicyId		INT,
		//@SettingName		NVARCHAR(50),
		//@SettingValue		NVARCHAR(500)	= NULL,
		//@DisplayFormat		NVARCHAR(128)	= NULL,
		//@ValueType		NVARCHAR(128)	= NULL,
		//@MinValue		INT		= NULL,
		//@MaxValue		INT		= NULL,
		//@SettingDescription	NVARCHAR(256)	= NULL,
		//@CompanyId		UNIQUEIDENTIFIER	= NULL

		da.AddWithValue("PolicyId", this.PolicyId);
		da.AddWithValue("SettingName", this.SettingName);
		da.AddWithValue("SettingValue", (string.IsNullOrEmpty(this.SettingValue) ? null : this.SettingValue));
		da.AddWithValue("DisplayFormat", this.DisplayFormat);
		da.AddWithValue("ValueType", this.ValueType);
		da.AddWithValue("MinValue", this.MinValue);
		da.AddWithValue("MaxValue", this.MaxValue);
		da.AddWithValue("SettingDescription", this.SettingDescription);

		if (this.CompanyId.HasValue)
		    da.AddWithValue("CompanyId", this.CompanyId.Value);
		else
		    da.AddWithValue("CompanyId", null);

		da.ExecNonQuery("PolicySetting_Set", ref result);

		return result;
	}
	#endregion
}
#endregion


Now if you've been paying attention, you'd be asking where does the connection get set. Well for that we expose the DataAccessContext class.
This class is essentially a register of data connections. You can set the default connection which if no other connections are registered is used
by default. If you have a model that is in a database other than your default data connection, then you can register a connection for your model.

Some where in your application initialization code you can register a custom connection

DataAccessContext.DefaultConnection = new DataAccessConnection("your default connection string")

if (!DataAccessContext.IsConnectionRegistered<YourDataModelEntity>())
    DataAccessContext.RegisterConnection<YourDataModelEntity>(new DataAccessConnection("your model specific connection string"));

DataAccessConnection dac = DataAccessContext.GetRegisteredOrDefault<YourDataModelEntity>();

DataAccessConnection dac2;
if (DataAccessContext.TryGetConnection<YourDataModelEntity>(out dac2))
    // Do something with dac2

Last edited Aug 8, 2010 at 3:41 PM by hyspdrt, version 8

Comments

No comments yet.