• Repository Factory – Week#1

    Published by johnny on August 13th, 2007 9:59 am under Uncategorized

    No Comments

    We've completed the first week working on the Repository Factory project. As I mentioned on a previous post, Chris Tavares and I are working on this amazing project.
    I'd like to share with you the improvements we've done during this week, and remember that your vote decides the future of the project. Enter codeplex and keep voting, we're tackling most voted stuff.

    GAT/GAX July 2007 Support

    We have migrated the guidance package to GAT/GAX July 2007 CTP. More about GAT/GAX 2007 migration can be found here.

    Service Factory independent

    We've removed all the dependencies to Microsoft Patterns & Practices Service Factory, so you can run the package without the necessity of having the Service Factory installed.

    Place generic code into its own DLL

    As you might recall the formerly Data Access Guidance Package generated a folder called generic on your Data Access Project. The intent of that was having base-classes for the repository assets. But that code was always the same, so we moved that code a new assembly called Microsoft.Practices.Repository. This new assembly contains the generic code, making your DAC projects are smaller, and easy to maintain.

    Generate interface for Repository

    Let's explain this point with little bit of code. Suppose that we've a presenter class, that uses the repository, and we want to use IoC pattern. Our code should look like this:

    public class Presenter
    {
         public Presenter(Repository repository)
         {
            //TODO: Add some constructor logic here
         }
    }

    Since we're sticking to the concrete repository implementation, we won't be able to mock the repository implementation, when we want to write a unit test. Ok, you might be thinking that you can easily do Right Click -> Extract Interface, we knew that but better than using the refactoring tools we're doing it for you. Both repository and interface are generated by Repository Factory. e.g.

    //The interface generated by RF
    public interface IMyRepository
    {
         void Add(MyEntity entity);
    }
    
    //The concrete class
    public class Repository : IMyRepository
    {
           public void Add(MyEntity entity)
           {
           }
    }

    Create factory to retrieve repositories

    Based on the previously explained improvement (Generate Interface for Repository), we've created a RepositoryFactory (besides the GP, a class) that you can use on your code to insulate callers from the concrete repository implementation. Based on this you can write code to retrieve the repository by it's interface and then you can change the implementation by modifying the config file.

    Usage

    public void SomeMethod()
    {
          IPersonRepository repository =
                    RepositoryFactory.Create(connectionString);
    }

    On the config file you'll have something like this

    <repositoryFactory>
          <repositories>
                 <add repositoryInterface="IMyRepository, MyAssembly"
                      repositoryType="MyRepository, MyAssembly" />
          </repositories>
    </repositoryFactory>

    Auto-map entity fields to Stored Procedure parameters

    Sometimes you'll have to add an operation to your repository that is more than just a single CRUD operation. Previously when you needed to create this operation you had to map the input/output parameters and return values from to entity fields. Now we've improved this by automating the mapping. It's a little bit smarter than just mapping Name -> sProcName, it performs the mapping ignoring case and removing “pty_” (and other suffixes).

    Summary

    We finished this first week, all the tackled issues were not invented by us, they are community requests. We're listening to you, please feel free to provide feedback in our community site. We work on the improvements starting with highly ranked ones, you have a chance to influence the course of the project.

    We're going to be delivering a CTP version soon! So stay tuned…

    thanks,
    ~johnny

    PS. Acknowledgements

    I want to thank Paulo who gave my blog the new head-wash, he is a really creative person, don't forget to check out his blog…

    Tags: , , ,

  • Leave a comment

    Your email address will not be published.