The Spring.NET Web Service Exporter Part 3 – The Business Logic
In my previous posts here and here, I explained the advantages of using Spring.NET’s Web Service Exporter and how to initialize Spring and Spring Web Service Exporting in the Web.config file. Now we need a service to export. Below I’ve listed the most important parts of the application – for brevity, I’ll omit imports and namespace declarations.
The service must implement an interface that represents the service contract:
public interface ILookUpService
{
LookUpDTO LookUp(string key);
LookUpDTO[] LookUpAll();
string AccessSession();
}
The service implementation:
public class LookUpService : ILookUpService
{
#region private fields
private ILookUpDal _lookUpDal;
#endregion
#region Properties
public ILookUpDal LookUpDal
{
get { return _lookUpDal; }
set { _lookUpDal = value; }
}
#endregion
#region ILookUpService Members
public LookUpDTO LookUp(string key)
{
return LookUpDal.LookUp(key);
}
public LookUpDTO[] LookUpAll()
{
return LookUpDal.LookUpAll();
}
public string AccessSession()
{
if (HttpContext.Current.Session != null)
{
// This accesses the current user's session
if (HttpContext.Current.Session["First Accessed"] == null) {
HttpContext.Current.Session["First Accessed"] =
string.Format("First accessed on {0:s}", DateTime.Now);
}
return HttpContext.Current.Session["First Accessed"] as string;
}
return "Session Not Found";
}
#endregion
}
The DAL – I’m only showing the implementation (the interface defines both public methods):
public class LookUpDal : ILookUpDal
{
#region private fields
private IDictionary _data;
#endregion
#region Properties
public IDictionary Data
{
get { return _data; }
set { _data = value; }
}
#endregion
#region ILookUpDal Members
public LookUpDTO LookUp(string key)
{
return Data.ContainsKey(key) ? Data[key] : null;
}
public LookUpDTO[] LookUpAll()
{
LookUpDTO[] array = new LookUpDTO[Data.Count];
Data.Values.CopyTo(array, 0);
return array;
}
#endregion
}
And the DTO:
public class LookUpDTO
{
#region Private Fields
private string _key;
private string _title;
private string _description;
#endregion
#region Properties
public string Key
{
get { return _key; }
set { _key = value; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public string Description
{
get { return _description; }
set { _description = value; }
}
#endregion
}
I could have added the DAL code directly into the service for simplicity’s sake and reduced the code to three classes (service interface, service implementation and custom DTO) but it’s good practice to keep actual data access out of the service layer (that’s typically the application layer that combines the fine-grained data access operations into coarse-grained business methods and applies the transactions).
All we’re missing now is the actual Spring configuration file. Stay tuned!
Anke