Aspen – A sample app using Silverlight 4 and .Net 4.0 – part 5 of X – New Repository implementation

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

Published Friday, January 22, 2010 12:40 AM by Fredrik N

Comments

# Twitter Trackbacks for Aspen ??? A sample app using Silverlight 4 and .Net 4.0 ??? part 5 of X ??? New Repository implementation - Fredrik [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Aspen ??? A sample app using Silverlight 4 and .Net 4.0 ??? part 5 of X ??? New Repository implementation - Fredrik         [asp.net]        on Topsy.com

# Dew Drop &#8211; January 22, 2010 | Alvin Ashcraft&#039;s Morning Dew

Pingback from  Dew Drop &#8211; January 22, 2010 | Alvin Ashcraft&#039;s Morning Dew

# Enlaces Interesantes WPF/Silverlight 22-01-2010

Friday, January 22, 2010 9:59 AM by Blog de Oskar Alvarez

Hola vengo con los enlaces interesantes de hoy pero quisiera resaltar uno y establecer un peque&ntilde;o

# Aspen – part 6 of X – Reusing ObjectContex amon Repositories during a DomainSerivce operation.

Saturday, January 23, 2010 6:52 AM by Fredrik Normén

(I had to shorten the title down a bit ;)) I promised to write about how I have implemented the solution

# re: Aspen – A sample app using Silverlight 4 and .Net 4.0 – part 5 of X – New Repository implementation

Tuesday, March 02, 2010 5:55 PM by Cihan

From the following blog, (if you read the comments)

elegantcode.com/.../comment-page-2

The Func has to be wrapped around Expression

IEnumerable<T> Find(Func<T, bool> where);

Like so

IEnumerable<T> Find(Expression<Func<T, bool>> where);

Supposedly otherwise it predicate doesn't flow down to the DB layer as a where clause.

Appreciate your reference implementation. Thank you!

Keep up the good work

# re: Aspen – A sample app using Silverlight 4 and .Net 4.0 – part 5 of X – New Repository implementation

Wednesday, March 03, 2010 1:48 AM by Fredrik N

@Cihan:

Thank you! That is correct.. will fix it.

Leave a Comment

(required) 
(required) 
(optional)
(required)