skip to the main content area of this page
Patterns and Practices

Repository Factory - Use Partial Classes For Custom Properties and Methods in the Data Access Layer


This is old hat to most developers that are familiar with code generation tools and how they take advantage of partial classes to allow you to protect custom methods and properties on generated classes so they don't get overwritten during subsequent code generations, but the question / scenario came up in the Repository Factory Forums which may be useful to developers new to code generation.

The Repository Factory generates repository classes as partial classes. Partial classes allow a developer to define properties and methods of a class across multiple files in your projects and solutions. Partial classes are very popular and useful in situations where classes are generated by a code generator, because they allow a developer to put hand-coded properties and methods in a separate file that are not touched by a code generator. This allows the code generator to overwrite and or delete the files it generates without the developer losing the hand-coded custom properties and methods.

If you use the Repository Factory and want to add custom properties and methods on the repository classes, it is recommended that you add those custom properties and methods on a separate partial class so that they do not get overwritten and accidentally deleted by the guidance automation recipes that generate the repository classes. The same is true for the business entity classes as well.

For example, a CustomerRepository generated by the Repository Factory will contain several CRUD Methods that are generated via the guidance package. These methods will vary based upon your needs:

 

public partial class CustomerRepository : Repository<Customer>, ICustomerRepository

{

    public CustomerRepository(string databaseName)

        : base(databaseName)

    {

    }

 

    public CustomerRepository()

        : base()

    {

    }

 

 

    public List<City> GetAllFromCustomer()

    {

        // ...

    }

 

    public void Add(Customer customer)

    {

        // ...

    }

 

    public void Remove(System.Int32 customerId)

    {

        //...

    }

 

 

    public void Save(Customer customer)

    {

        //...

    }

}

 

You can create another file with a similar partial class name with your own custom methods that will not be overwritten by the Repository Factory during code generation. In this case we add a method that allows us to get customers based on Zip Code:

 

public partial class CustomerRepository

{

    public List<Customer> GetCustomersByZipCode(string zipCode)

    {

        // ...

    }

}

 

Because the custom method is in a separate file using the same partial class, you don't have to be concerned with the method being overwritten by the Repository Factory, but still get the value of the method being a part of the CustomerRepository.

The use of partial classes is used in other code generation tools for the data access layer, like LINQ To SQL, for the very same reason.


Tags: CodeGeneration, DataAccessLayer, PartialClasses


Topics



Popular Tags



Recent Links