Hello,
I'm updating a program and adding a new table to the database. The program uses Castle's ActiveRecord with repositories. I've set the classes and repository up and can get the test case out of the database fine. However, when I try to add a new record to the database, nothing happens. No error and no new record. If I get the test case out of the database and change something then call Save(), the record in the database changes.
This is the code I'm using to test:
ICountryRepository _countryRepository = Country.GetRepository(site.Id);
//get test case and update
Country c = _countryRepository.FindById(1).Value;
c.Name = "c";
_countryRepository.Save(c);
//create new Country and save
Country d = new Country();
d.Id = 2;
d.Name = "d";
_countryRepository.Save(d);
Now as it's a maintenence project with no real time to stop and study how the Castle framework is doing everything, I'm learning as I go along. I've picked up how to do things by studying the rest of the code and there are occasions in the code where the above code has been used to create new records, so I'm sure Save() is the right function to call.
I've tried dropping the create code in with another part of the code that inserts an object into a different table, just to make sure there weren't different levels of permission in play. There are no differences in the database between the table I added and the table I'm basing my code off.
As I've said, my experiences with ActiveRecord and Castle's framework here are few, it could/will be something quite simple. So hopefully someone can point it out to me.
Thanks.
Edit:
Country Class:
[ActiveRecord("Country")]
[SearchEntity]
public class Country : AbstractEntity<Country, ICountryRepository>
{
int _countryId;
string _name;
int _action;
[PrimaryKey("CountryId")]
public int Id
{
get { return _countryId; }
set { _countryId = value; }
}
[SearchProperty]
[Property(Length = 100)]
public string Name
{
get { return _name; }
set { _name = value; }
}
[Property]
public int PostRegisterActionId
{
get { return _action; }
set { _action = value; }
}
}
AbstractRepository since CountryRepository does nothing at the moment:
[Transient]
public abstract class AbstractRepository<T> : IRepository<T> where T : AbstractEntity, new()
{
#region Private Vars
protected FutureQueryRunner _futureQueryRunner;
protected IDynamicSearchService _dynamicSearchService;
protected bool _caching;
protected int _cachedPages;
protected CachingFutureQueryOfList<T> _cachedQuery;
protected IServiceBus _serviceBus;
private string _entityTypeName = string.Empty;
#endregion
#region Constructors
public AbstractRepository(IDynamicSearchService dynamicSearchService, IServiceBus serviceBus)
{
_dynamicSearchService = dynamicSearchService;
_serviceBus = serviceBus;
}
#endregion
#region Public Methods
public virtual void Save(T instance)
{
ActiveRecordMediator<T>.Save(instance);
}
public virtual void Create(T instance)
{
ActiveRecordMediator<T>.Create(instance);
}
public void Delete(T instance)
{
ActiveRecordMediator<T>.Delete(instance);
}
public virtual IFutureQueryOf<T> New()
{
return new NullFutureQuery<T>();
}
public virtual IFutureQueryOf<T> FindById(int id/*, params string[] eagerAssociations*/) // eager associations buggy
{
DetachedCriteria criteria = GetDefaultCriteria()
.Add(Expression.IdEq(id));
/*foreach (string eager in eagerAssociations)
criteria.SetFetchMode(eager, NHibernate.FetchMode.Eager);*/
return new FutureQueryOf<T>(_futureQueryRunner)
.SetCriteria(criteria);
}
public virtual IFutureQueryOfList<T> FindAll(string sortBy, bool sortAsc)
{
DetachedCriteria criteria = GetDefaultCriteria();
if (!string.IsNullOrEmpty(sortBy))
criteria.AddOrder(sortAsc ? NHibernate.Criterion.Order.Asc(sortBy) : NHibernate.Criterion.Order.Desc(sortBy));
return new FutureQueryOfList<T>(_futureQueryRunner)
.SetCriteria(criteria);
}
public virtual IFutureQueryOfList<T> FindAll(int page, int resultsPerPage, string sortBy, bool sortAsc)
{
return FindAll(new DefaultCriteriaProvider<T>(DetachedCriteria.For<T>()),
page,
resultsPerPage,
sortBy,
sortAsc,
"FindAll");
}
public virtual IFutureQueryOfList<T> FindAll(string searchString, int page, int resultsPerPage, string sortBy, bool sortAsc)
{
return FindAll(new SearchStringCriteriaProvider<T>(_dynamicSearchService, searchString),
page,
resultsPerPage,
sortBy,
sortAsc,
"FindAllSearchString_" + searchString);
}
public virtual IFutureQueryOfList<T> FindAll(IListFilter filter, int page, int resultsPerPage, string sortBy, bool sortAsc)
{
return FindAll(new FilterCriteriaProvider<T>(_dynamicSearchService, filter),
page,
resultsPerPage,
sortBy,
sortAsc,
"FindAllListFilter"); // TODO - the cache key needs to represent individual filters
}
public virtual IFutureQueryOf<int> GetCount(string searchString)
{
return new FutureQueryOf<int>(_futureQueryRunner)
.SetCriteria(AddDefaultCriteria(new SearchStringCriteriaProvider<T>(_dynamicSearchService, searchString).GetDetachedCriteria())
.SetProjection(Projections.RowCount()));
}
public virtual IFutureQueryOf<int> GetCount()
{
return new FutureQueryOf<int>(_futureQueryRunner)
.SetCriteria(GetDefaultCriteria()
.SetProjection(Projections.RowCount()));
}
public virtual string EntityType
{
get
{
if (string.IsNullOrEmpty(_entityTypeName))
_entityTypeName = typeof(T).Name;
return _entityTypeName;
}
}
public IRepository<T> EnableCaching(bool caching)
{
_caching = caching;
return this;
}
public IRepository<T> WithPagesToCache(int cachedPages)
{
_cachedPages = cachedPages;
return this;
}
public virtual IRepository<T> ForSite(int siteId)
{
return this;
}
public IRepository<T> RunFutureQueriesWith(FutureQueryRunner futureQueryRunner)
{
_futureQueryRunner = futureQueryRunner;
return this;
}
#endregion
#region Protected Methods
protected virtual DetachedCriteria AddDefaultCriteria(DetachedCriteria criteria)
{
return criteria;
}
protected DetachedCriteria GetDefaultCriteria()
{
return AddDefaultCriteria(DetachedCriteria.For<T>());
}
protected IFutureQueryOf<U> NewQueryOf<U>(DetachedCriteria criteria)
{
return new FutureQueryOf<U>(_futureQueryRunner).SetCriteria(criteria);
}
protected IFutureQueryOfList<U> NewQueryOfList<U>(DetachedCriteria criteria)
{
return new FutureQueryOfList<U>(_futureQueryRunner).SetCriteria(criteria);
}
#endregion
#region Private Methods
private IFutureQueryOfList<T> FindAll(ICriteriaProvider<T> criteriaProvider, int page, int resultsPerPage, string sortBy, bool sortAsc, string cacheKey)
{
CachingFutureQueryOfList<T> rtnVal = null;
bool cached = false;
if (_cachedQuery != null && _caching)
{
rtnVal = _cachedQuery;
cached = rtnVal.SetPage(page, sortBy, sortAsc, cacheKey);
}
if (!cached)
{
rtnVal = new CachingFutureQueryOfList<T>(_futureQueryRunner, page, _cachedPages, resultsPerPage, cacheKey)
.SetCriteria(AddDefaultCriteria(criteriaProvider.GetDetachedCriteria()), sortBy, sortAsc);
if (_caching)
_cachedQuery = rtnVal;
}
return rtnVal;
}
#endregion
#region Criteria Providers
private interface ICriteriaProvider<U>
{
DetachedCriteria GetDetachedCriteria();
}
private class DefaultCriteriaProvider<U> : ICriteriaProvider<U>
{
private DetachedCriteria _criteria;
public DefaultCriteriaProvider(DetachedCriteria criteria)
{
_criteria = criteria;
}
public DetachedCriteria GetDetachedCriteria()
{
return _criteria;
}
}
private class SearchStringCriteriaProvider<U> : ICriteriaProvider<U>
{
private IDynamicSearchService _searchService;
private string _searchString;
public SearchStringCriteriaProvider(IDynamicSearchService searchService, string searchString)
{
_searchService = searchService;
_searchString = searchString;
}
public DetachedCriteria GetDetachedCriteria()
{
return _searchService.GetDetachedCriteria<U>(_searchString);
}
}
private class FilterCriteriaProvider<U> : ICriteriaProvider<U>
{
private IDynamicSearchService _searchService;
private IListFilter _filter;
public FilterCriteriaProvider(IDynamicSearchService searchService, IListFilter filter)
{
_searchService = searchService;
_filter = filter;
}
public DetachedCriteria GetDetachedCriteria()
{
return _searchService.GetDetachedCriteria<U>(_filter);
}
}
#endregion
}