This will be quite small blog post but the time is running out and I have to go to bed. But I wanted to let you know what I’m working on at the moment in the “Aspen” project.
I have done several changes to the code. No methods calls to the SubmitChanges of the ObjectContext it done from the Repositories. In a previous post I made a call to the SaveChanges in the Add and Delete methods of a Repository, it’s now removed. The reason why it’s removed is because I want to take the benefit of using the Entity Frameworks ObjectContext through the whole DomainService operation. By doing so I can reuse the ObjectContext’s Unit of Work.
I have also make sure that the SubmitChanges now will be called when the Complete method of the TransactionScope class is called, like nHibernate does.
I will write a new blog soon about all my solutions to solve the things above, but I will in this blog post at least show you some code. The following is a DomainService where the ObjectContext will be reused during a whole operation (so in the GetAll method, both the Member- and GetheringRepository will share the same ObjectContext, you can also see that I use a Transaction when I add members, when the Complete method is called, I will make sure the ObjectContext’s SaveChanges method will be called:
[EnableClientAccess()]
public class MemberDomainService : DomainService
{
readonly IMemberRepository _memberRepository = null;
readonly IGatheringRepository _gatheringRepository = null;
public MemberDomainService(IMemberRepository membershipRepository, IGatheringRepository gatheringRepository)
{
if (membershipRepository == null)
throw new ArgumentNullException("membershipRepository");
_memberRepository = membershipRepository;
_gatheringRepository = gatheringRepository;
}
[Invoke]
public void Save()
{
using (var trans = new TransactionScope())
{
_memberRepository.Add(new Member() { FirstName = "Ove", LastName = "Bengt" });
_gatheringRepository.Add(new Member() { FirstName = "OveG", LastName = "BengtG" });
trans.Complete();
}
}
public IEnumerable<MemberDto> GetMembers()
{
var test = _gatheringRepository.GetAll();
var members = _memberRepository.GetAll();
return members.Select(m => Mapper.Map<Member, MemberDto>(m));
}
}
And one more thing, the ObjectContext reused during the operation will of course be Disposed when the operation is completed. As you may see I still inherits from the DomainService base class so how can I make sure the OjbectContext’s Dispose method will be called after an operation is done? That is something you will see in my next blog post when I will show you the detail of the implementations I have done.
If you want to know when I published a new blog post, you can follow me on twitter: http://www.twitter.com/fredrikn