Reputation: 794
I'm new to NHibernate, and FluentNHibernate. I'm trying to figure out the best way to setup mappings between tables in different schemas as well as potentially doing some filtering in the mappings.
In my scenario I have doctors who are associated with hospitals where a doctor can be associated with multiple hospitals. At some hospitals a doctor might be blacklisted from performing certain procedures. There are also verying degrees of blacklisting and I'm only interested in those whose black list type is zero. Because the tables reside in different schemas and are managed by different IT groups the column names for joining don't always match. I'm trying to figure out the best way to map that relationship.
I have part of this working, but I'm not sure how to get the whole relationship setup so that I only get those hospitals where a doctor has a specific black list designation.
Here's a trimmed down look at my entities.
public class Doctor
{
pubilc virtual int DoctorId { get; set; }
public virtual string Name { get; set; }
public virtual IList<BlackListSite> BlackListedSites { get; set; }
}
public class BlackListSite
{
public virtual int DoctorId { get; set; }
public virtual int HospitalId { get; set; }
public virtual int ProcedureId { get; set; }
public virtual int BlackListTypeId { get; set; }
public virtual IList<Facility> Facilities { get; set; } //these are the hospitals
}
public class Facility //this represents a hospital
{
public virtual int FacilityId{ get; set; }
public virtual string Name { get; set; }
}
Here's how I'm mapping the BlackListSite and Hospital, which is working.
public class BlackListSitesMap : ClassMap<BlackListSites>
{
public BlackListSitesMap()
{
Schema("schema1");
Table("RestrictedDoctors");
Id(x => x.BlackListId).GeneratedBy.Identity();
Map(x => x.DoctorId);
Map(x => x.HospitalId);
Map(x => x.Status);
HasMany(x => x.Facilities).Cascade.All().Not.LazyLoad()
.KeyColumns.Add("HospitalID",
mapping => mapping.Name("FacilityID"));
}
}
public class FacilityMap : ClassMap<Facility>
{
public FacilityMap()
{
Schema("schema2");
Table("Facility");
Id(x => x.FacilityId);
Map(x => x.Name);
Map(x => x.Active);
}
}
A query like this gets me a list of blacklisted sites where blacklist type is 0.
public void TestBlackList()
{
using (var session = SessionManager.Session())
{
var blacklist = session.Query<BlackListSites>().
Where(b => b.DoctorId == 1 && b.HospitalId != null
&& b.Status == 0).ToList();
}
}
I'm not sure how to tie the blacklisted sites to the Doctor so that when I request a doctor I get a list of sites where the doctor is blacklisted with a blacklist type of zero. Any ideas?
If I can't do this on the mapping, can I somehow setup the query on Doctor to filter the blacklist where BlackListTypeId = 0? That may be the most ideal anyway. It would set me up in the future to request other Blacklist types.
Upvotes: 1
Views: 1312
Reputation: 30813
i would change the classes to
public class Doctor
{
pubilc virtual int DoctorId { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<BlackListSite> BlackListedSitesTypeZero { get; set; }
}
public class BlackListSite
{
public virtual Doctor Doctor { get; set; }
public virtual Facility Hospital { get; set; }
public virtual int ProcedureId { get; set; }
public virtual int BlackListTypeId { get; set; }
public virtual IList<Facility> Facilities { get; set; } //these are the hospitals
}
public class DoctorMap : ClassMap<Doctor>
{
public DoctorMap()
{
...
HasMany(x => x.BlackListedSitesTypeZero)
.KeyColumn("DoctorId")
.Where("status = 0");
}
}
then in the mappings specify the where condition
public class BlackListSiteMap : ClassMap<BlackListSite>
{
public BlackListSiteMap()
{
Schema("schema1");
Table("RestrictedDoctors");
Id(x => x.BlackListId).GeneratedBy.Identity();
References(x => x.Doctor, "DoctorId");
References(x => x.Hospital, "HospitalId");
Map(x => x.Status);
}
}
Upvotes: 3