76 lines
2.5 KiB
C#
76 lines
2.5 KiB
C#
using EnVisage.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace EnVisage.Code.BLL
|
|
{
|
|
public class ExpenditureCategoryManager
|
|
{
|
|
private readonly EnVisageEntities _dbContext;
|
|
private readonly bool _isContexLocal = false;
|
|
|
|
public ExpenditureCategoryManager(EnVisageEntities dbContext)
|
|
{
|
|
if (dbContext == null)
|
|
{
|
|
_dbContext = new EnVisageEntities();
|
|
_isContexLocal = true;
|
|
}
|
|
else
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
}
|
|
public void Dispose()
|
|
{
|
|
if (_isContexLocal)
|
|
_dbContext.Dispose();
|
|
}
|
|
|
|
#region Public Methods
|
|
/// <summary>
|
|
/// Loads a Expenditure Category from the database.
|
|
/// </summary>
|
|
/// <param name="value">Unique identifier of the Expenditure Category.</param>
|
|
/// <param name="isReadOnly">Indicates that object will not be saved later in the code. Use <b>false</b> if you need to save an updated object.</param>
|
|
/// <returns>A <see cref="ExpenditureCategory"/> object retrieved from database.</returns>
|
|
public ExpenditureCategory Load(Guid? value, bool isReadOnly = true)
|
|
{
|
|
if (value == null || value == Guid.Empty)
|
|
return new ExpenditureCategory();
|
|
|
|
return isReadOnly
|
|
? _dbContext.ExpenditureCategory.AsNoTracking().FirstOrDefault(t => t.Id == value)
|
|
: _dbContext.ExpenditureCategory.Find(value);
|
|
}
|
|
|
|
public void Save(ExpenditureCategoryModel model)
|
|
{
|
|
if (model == null)
|
|
throw new ArgumentNullException("model");
|
|
|
|
#region Save ExpenditureCategory data
|
|
ExpenditureCategory dbObj = null;
|
|
if (model.Id != Guid.Empty)
|
|
dbObj = _dbContext.ExpenditureCategory.Find(model.Id);
|
|
if (dbObj == null)
|
|
{
|
|
dbObj = new ExpenditureCategory { Id = Guid.NewGuid() };
|
|
}
|
|
model.CopyTo(dbObj);
|
|
dbObj.CGEFX = "CG";
|
|
if (model.Id == Guid.Empty)
|
|
_dbContext.ExpenditureCategory.Add(dbObj);
|
|
else
|
|
_dbContext.Entry(dbObj).State = EntityState.Modified;
|
|
#endregion
|
|
|
|
if (_isContexLocal)
|
|
_dbContext.SaveChanges();
|
|
}
|
|
#endregion
|
|
}
|
|
} |