views:

121

answers:

1

When I do a save of an entity with assigned id I get a

SharpArch.Core.PreconditionException: For better clarity and reliability, Entities with an assigned Id must call Save or Update

My class is

public class CompanyUserRepository :
RepositoryWithTypedId<CompanyUser, string>, ICompanyUserRepository
{
   public override CompanyUser SaveOrUpdate(CompanyUser entity)
   {
     var user = base.SaveOrUpdate(entity);
     //Do some stuff
     return user;
   }
}

How do I go about saving this entity?

RepositoryWithTypedId does not expose a Save method

Related question. This tells you the reason, but I haven't found out the Sharp Architecture way to do a Save.

+1  A: 

Digging through the NorthWind example in the S#arp repo, I found out the answer.

The Repository class should derive from NHibernateRepositoryWithTypedId

e.g.

public class CustomerRepository : NHibernateRepositoryWithTypedId<Customer, string>, ICustomerRepository
{
    public List<Customer> FindByCountry(string countryName) {
        ICriteria criteria = Session.CreateCriteria(typeof(Customer))
            .Add(Expression.Eq("Country", countryName));


        return criteria.List<Customer>() as List<Customer>;
    }
}
Quintin Par