General
- All methods accept and use ISessionHandler
using SignifyHR.Core;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
namespace SignifyHR.Data.Domain
{
public partial class exSample : IAuditable
{
protected static IQueryable<exSample> ValidSamples(SignifyHRDAL dbContext, EagerLoadParameters eagerLoadParms = null)
{
var samples = dbContext.exSamples.AsQueryable();
if (eagerLoadParms != null)
{
if (eagerLoadParms.IncludeSampleDocuments)
samples = samples.Include(item => item.exSampleDocuments);
if (eagerLoadParms.IncludeSampleComments)
samples = samples.Include(item => item .exSampleComments);
}
return samples;
}
public static IEnumerable<exSample> FetchAll(ISessionHandler sessionHandler, SearchParameters searchParms, EagerLoadParameters eagerLoadParms = null)
{
using (var dbContext = new SignifyHRDAL(sessionHandler))
{
var results = ValidSamples(dbContext, searchParms, eagerLoadParms);
return results.OrderByDescending(item => item.Id)
.Skip(searchParms.Skip)
.Take(searchParms.Take)
.ToList();
}
}
}
}
- Eager loading used responsibly
using SignifyHR.Core;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
namespace SignifyHR.Data.Domain
{
public partial class exSample : IAuditable
{
#region Eager Load Parameters
public class EagerLoadParameters : BaseSearchParameters
{
public bool IncludeSampleDocuments { get; set; }
public bool IncludeSampleComments { get; set; }
}
#endregion
#region Protected Methods
protected static IQueryable<exSample> ValidSamples(SignifyHRDAL dbContext, EagerLoadParameters eagerLoadParms = null)
{
var samples = dbContext.exSamples.AsQueryable();
if (eagerLoadParms != null)
{
if (eagerLoadParms.IncludeSampleDocuments)
samples = samples.Include(item => item.exSampleDocuments);
if (eagerLoadParms.IncludeSampleComments)
samples = samples.Include(item => item .exSampleComments);
}
return samples;
}
}
}
- IQueryable declared as internal/protected?
protected/internal static IQueryable<exSample> ValidSamples(SignifyHRDAL dbContext, EagerLoadParameters eagerLoadParms = null)
protected/internal static IQueryable<exSample> FilterSamples(SignifyHRDAL dbContext, SearchParameters searchParms, EagerLoadParameters eagerLoadParms = null)
public static exSample Fetch(ISessionHandler sessionHandler, int id, EagerLoadParameters eagerLoadParms = null)
public static exSample TryFetch(ISessionHandler sessionHandler, int id, EagerLoadParameters eagerLoadParms = null)
- POCO created for SP
- Search parameter array used
- No DateTime values are passed to the database (different servers = different time = different results).
- Domain Convention were followed.
- Create a POCO Object when you want to call a stored procedure or view from Entity Framework. See example here
- First, FirstOrDefault, Single, SingleOrDefault used for the correct purpose:
- First - when one or more entities may be returned but only the first one is used (Remember to use OrderBy to return the correct entity)
- FirstOrDefault - when none, one or more entities are returned, but only the first one is used (Remember to use OrderBy to return the correct entity)
- Single - when only one entity will ALWAYS be returned
- SingleOrDefault - when one or no entities are expected