I get this json
{
"Name": "Teste",
"ExpirationDate": "/Date(1515703416000-0200)/",
"Program": {
"Id": 1
},
"Resource":{
"Id": 1
},
"InsertDate": "/Date(1515703416000-0200)/",
"InsertUserId": 8,
"LastUpdateDate": null,
"LastUpdateUserId": null
}
And my service looks like this:
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);
}
Do I really need to search my database for Program
and Resource
, before inserting ability
?
The Resource
and the Program
will already exist in the DB at the moment I'm inserting the object ability
<?xml version="1.0" encoding="utf-8" ?>
Ability mapping
<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>
mapping 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>
mapping 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>