recebo este json
{
"Name": "Teste",
"ExpirationDate": "/Date(1515703416000-0200)/",
"Program": {
"Id": 1
},
"Resource":{
"Id": 1
},
"InsertDate": "/Date(1515703416000-0200)/",
"InsertUserId": 8,
"LastUpdateDate": null,
"LastUpdateUserId": null
}
e meu servico esta assim:
public string CreateAbility(CallDomain.Ability ability)
{
if (ReferenceEquals(ability, null))
{
throw new WcfException(System.Net.HttpStatusCode.BadRequest, ErrorMessage.Resource.EMPTY);
}
else if(!(ability.Program?.Id > 0))
{
throw new WcfException(System.Net.HttpStatusCode.BadRequest, ErrorMessage.Program.ID_REQUIRED);
}
else if (!(ability.Resource?.Id > 0))
{
throw new WcfException(System.Net.HttpStatusCode.BadRequest, ErrorMessage.Resource.ID_REQUIRED);
}
var repository = new AbilityRepository(_user.Id);
ability.IsActive = true;
var repResource = new ResourceRepository(_user.Id);
var dbResource = repResource.Get(ability.Resource.Id);
var repProgram = new ProgramRepository(_user.Id);
var dbProgam = repProgram.Get(ability.Program.Id);
ability.Resource = dbResource;
ability.Program = dbProgam;
_ability = repository.Create(ability);
return CustomJsonSerializer.Serialize(_ability);
}
Eu preciso realmente buscar no banco de dados os meus Program
e Resource
, antes de inserir o ability
?
O Resource
e o Program
já irão existir no BD no momento em que eu estou inserindo o objeto ability
<?xml version="1.0" encoding="utf-8" ?>
mapeamento Ability
<class name="Ability" table="Ability">
<id name="Id" column="Id">
<generator class="native" />
</id>
<property name="Name" column="Name" />
<property name="IsActive" column="IsActive" />
<property name="ExpirationDate" column="ExpirationDate" />
<property name="InsertDate" column="InsertDate" />
<property name="InsertUserId" column="InsertUserId" />
<property name="LastUpdateDate" column="LastUpdateDate" />
<property name="LastUpdateUserId" column="LastUpdateUserId" />
<many-to-one name="Program" column="ProgramId" fetch="join" />
<many-to-one name="Resource" column="ResourceId" fetch="join" />
</class>
</hibernate-mapping>
mapeamento program
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="BipDomain"
namespace="BipDomain.Models.ProgramDomain">
<class name="Program" table="Program">
<id name="Id" column="Id">
<generator class="native"/>
</id>
<property name="Name" column="Name" />
<property name="IsActive" column="IsActive" />
<property name="InsertDate" column="InsertDate" />
<property name="InsertUserId" column="InsertUserId" />
<property name="LastUpdateDate" column="LastUpdateDate" />
<property name="LastUpdateUserId" column="LastUpdateUserId" />
<bag name="Abilities" cascade="All">
<key column="ProgramId" />
<one-to-many class="BipDomain.Models.CallDomain.Ability" />
</bag>
</class>
</hibernate-mapping>
mapeamento resource
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="BipDomain"
namespace="BipDomain.Models.ProgramDomain">
<class name="Resource" table="Resource">
<id name="Id" column="Id">
<generator class="native"/>
</id>
<property name="Name" column="Name" />
<property name="IsActive" column="IsActive" />
<property name="InsertDate" column="InsertDate" />
<property name="InsertUserId" column="InsertUserId" />
<property name="LastUpdateDate" column="LastUpdateDate" />
<property name="LastUpdateUserId" column="LastUpdateUserId" />
<bag name="Abilities" cascade="All" >
<key column ="ResourceId" />
<one-to-many class="BipDomain.Models.CallDomain.Ability"/>
</bag>
</class>
</hibernate-mapping>