# Domain convention ### Methods
MethodParametersLinq / Entity OperationReturn TypeStateDal Sync Mode
Fetch(id, optional filter params, eagerLoaded = false / eagerLoadParms = null)SingleTStaticDefault
TryFetch(id, optional filter params, eagerLoaded = false / eagerLoadParms = null)SingleOrDefaultT?StaticDefault
FetchAll(optional filter params)WhereIEnumerable<T>, IPagedList<T>StaticDefault
FetchAllBy<Association>(<Association>Id, optional filter params, eagerLoaded = false / eagerLoadParms = null)WhereIEnumerable<T>, IPagedList<T>StaticDefault
TryFetchFirstNoneFirstOrDefaultT?StatucDefault
CreateNoneAddObject(this)VoidNon-StaticSync
UpdateNoneAttach(this) / Modified Entity StateVoidNon-StaticSync
DeleteNoneAttach(this) / Deleted Entity StateVoidNon-StaticSync
Queryable<T>{All}{By<Association>SignifyHRDALIQueryableIQueryable<T>StaticDefault
Is<Condition>, Can<Condition>, Has<Condition>, etc.None / Property not methodBoolean CheckBoolNon-StaticDefault
<Enum>TypeNone / Property not method<T>TryParseEnum<T>Non-StaticDefault
CreateEditCall Create / Update separately from method. Is Wrapper MethodCall Methods SeparatelyVoidNon-StaticSync
### ### Meta Data Meta data is used when rendering MVC Razor views and are defined in the domain if display name differs from specified name in base table, values are required and/or format of value. Table 2 : Meta Data Properties
PropertyReasonExample
RequiredMark value as required and performs validation when submitting from form on view. An error message can be defined to display if validation fails.\[Required(ErrorMessage = "The {0} field is required")\]
Display(Name=<string>)Text to display when using DisplayFor() on views.\[Display(Name = "Start Date")\]
DataType(<DataType>)Formats and validate input according to specified type.\[DataType(DataType.DateTime)\]
### ### Examples #### Template Below is an example of a basic domain. This can be used as a template when creating new domains; copy and paste the code, replace all the required words (exSample, sample, smpl) according to applicable entity name.
**Note:** For domains with a single eager loaded objects, the following can be used:
```C# //- Replace "EagerLoadParameters eagerLoadParms = null" bool useEagerLoading = false ```
```C# 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 Search Parameters public class SearchParameters : BaseSearchParameters { public int? SomeId { get; set; } public string Description { get; set; } public bool IsUsed { get; set; } } #endregion #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 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; } protected static IQueryable FilterSamples(SignifyHRDAL dbContext, SearchParameters searchParms, EagerLoadParameters eagerLoadParms = null) { var result = ValidSamples(dbContext, eagerLoadParms); if (searchParms!= null) { if (!String.IsNullOrWhiteSpace(searchParms.Description)) result = result.Where(item => item.Description.ToLower().Contains(searchParms.Description.ToLower())); if (searchParms.SomeId.HasValue) result = result.Where(item => item.SomeId == searchParms.SomeId.Value); if (searchParms.IsUsed.HasValue) result = result.Where(item => item.IsUsed == searchParms.IsUsed.Value); } return result; } #endregion #region Public Methods public static exSample Fetch(ISessionHandler sessionHandler, int id, EagerLoadParameters eagerLoadParms = null) { using (var dbContext = new SignifyHRDAL(sessionHandler)) { return ValidSamples(dbContext, eagerLoadParms).Single(item => item.Id == id); } } public static exSample TryFetch(ISessionHandler sessionHandler, int id, EagerLoadParameters eagerLoadParms = null) { using (var dbContext = new SignifyHRDAL(sessionHandler)) { return ValidSamples(dbContext, eagerLoadParms).SingleOrDefault(item => item.Id == id); } } public static IEnumerable FetchAll(ISessionHandler sessionHandler, SearchParameters searchParms, EagerLoadParameters eagerLoadParms = null) { using (var dbContext = new SignifyHRDAL(sessionHandler)) { var results = FilterSamples(dbContext, searchParms, eagerLoadParms); return results.OrderByDescending(item => item.Id) .Skip(searchParms.Skip) .Take(searchParms.Take) .ToList(); } } #endregion #region Public CRUD Actions public void Create(ISessionHandler sessionHandler) { using (var dbContext = new SignifyHRDAL(sessionHandler)) { dbContext.exSamples.Add(this); dbContext.SaveChanges(); } } public void Update(ISessionHandler sessionHandler) { using (var dbContext = new SignifyHRDAL(sessionHandler)) { dbContext.exSamples.Attach(this); dbContext.Entry(this).State = EntityState.Modified; dbContext.SaveChanges(); } } public void Delete(ISessionHandler sessionHandler) { using (var dbContext = new SignifyHRDAL(sessionHandler)) { dbContext.exSamples.Attach(this); dbContext.exSamples.Remove(this); dbContext.SaveChanges(); } } #endregion } } ``` ### ### Methods #### FetchAll - PagedList.IPagedList
*OrderBy* or *OrderByDescending* **must** be applied prior to *ToPagedList*.
Use *PagedList.IPagedList* for Bootstrap 3 Pager ```C# using PagedList; public static IPagedList FetchAll(ISessionHandler sessionHandler, SearchParameters searchParms, EagerLoadParameters eagerLoadParms = null) { using (var dbContext = new SignifyHRDAL(sessionHandler)) { var results = FilterSamples(dbContext, searchParms, eagerLoadParms); return results.OrderByDescending(item => item.Id) .ToPagedList(searchParms.CurrentPage.Value, searchParms.PageSize.Value); } } ``` #### FetchAll - X.PagedList.IPagedList
*OrderBy* or *OrderByDescending* **must** be applied prior to *ToPagedList*.
Use *X.PagedList.IPagedList* for Bootstrap 3 Pager when returning POCO class data. ```C# using X.PagedList; public static IPagedList FetchAllPreview(ISessionHandler sessionHandler, SearchParameters searchParms, EagerLoadParameters eagerLoadParms = null) { using (var dbContext = new SignifyHRDAL(sessionHandler)) { var samples = FilterSamples(dbContext, searchParms, eagerLoadParms); var results = samples.Select(item => new SamplePreview { XSPreviewId = item.Id, XSDescription = item.Description, XSPath = Path.GetFileNameWithoutExtension(item.Title) }) return results.OrderByDescending(item => item.Id) .ToPagedList(searchParms.CurrentPage.Value, searchParms.PageSize.Value, results.Count()); } } ``` #### Create & Update (Non-*IAuditable* Table) Use the following where table definitions does not contain all columns as required by *IAuditable*. ```C# public void Create(exSample sample) { using (var dbContext = new SignifyHRDAL(true)) { sample.SomeValue = 123; dbContext.exSample.AddObject(sample); dbContext.SaveChanges(); } } public void Update(exSample sample) { using (var dbContext = new SignifyHRDAL(true)) { sample.SomeValue = 123; dbContext.exSample.Attach(sample); dbContext.ObjectStateManager.ChangeObjectState(this, EntityState.Modified); dbContext.SaveChanges(); } } ``` #### Delete Multiple Use the following for multiple items. ```C# public void DeleteMany(IEnumerable sampleList) { using (var dbContext = new SignifyHRDAL(true)) { foreach (var sample in sampleList) { sample.Delete(); } } } ``` #### Meta Data Remember to specify *MetadataType* for partial class created. ```C# [MetadataType(typeof(exSampleMeta))] public partial class exSample : IAuditable { ... } public class exSampleMeta : DefaultColumnsMeta { [Required(ErrorMessage = "The {0} field is required")] [Display(Name = "Sample Name")] public string Description { get; set; } [Required(ErrorMessage = "The {0} field is required")] [Display(Name = "Start Date")] [DataType(DataType.DateTime)] public DateTime StartDate { get; set; } [Required(ErrorMessage = "The {0} field is required")] [Display(Name = "End Date")] [DataType(DataType.DateTime)] public DateTime EndDate { get; set; } [Display(Name = "Enabled")] public bool Enabled { get; set; } } ``` TODO : AddRange, UpdateRange, FetchPaged