patterncsharpMinor
Make a testable DAL using service and repository pattern
Viewed 0 times
daltestablemakeserviceusingrepositoryandpattern
Problem
I'm implementing a Web API that communicates with old WCF services and want to make it future proof.
The Web API consists of:
I've implemented a service and a repository layer. A repository factory is injected into each service, because services can communicate with more than one repository.
The API controllers use service providers, which use a repository which can be tied to a WCF service, database or whatever. For now it's only tied to a WCF service for data, but because these WCF services are old I would like to be able to replace them later on with minimal effort or go straight to the database (using ORM).
Current WCF services deliver polluted complicated objects, so I'm also using the repository layer to filter these objects to a well structured POCOs.
I want to be able to:
A simple flow diagram would be:
Edit 29/08/14: Managed to eliminate the dynamic return type. Changed the return type to
AuthenticationController
```
public class AuthenticationController : ApiController
{
AuthServiceProvider authProvider;
public AuthenticationController()
{
RepositoryFactory repoFactory = new RepositoryFactory();
repoFactory.AddRepository(new AuthServiceRepository());
repoFactory.AddRepository(new SystemServiceRepository());
authProvider = new AuthServiceProvider(repoFactory);
}
[Route("login")]
[HttpPost]
public Authenticate Login(Login login)
The Web API consists of:
- API controllers
- Service Providers
- Repositories
I've implemented a service and a repository layer. A repository factory is injected into each service, because services can communicate with more than one repository.
The API controllers use service providers, which use a repository which can be tied to a WCF service, database or whatever. For now it's only tied to a WCF service for data, but because these WCF services are old I would like to be able to replace them later on with minimal effort or go straight to the database (using ORM).
Current WCF services deliver polluted complicated objects, so I'm also using the repository layer to filter these objects to a well structured POCOs.
I want to be able to:
- Easily replace a data store (WCF service, database or whatever)
- Easily test business logic in isolation (with mock data)
A simple flow diagram would be:
Edit 29/08/14: Managed to eliminate the dynamic return type. Changed the return type to
T and casted the returned repository to type T:public T GetRepository()
{
// If the repository exists, return it
if (this.repositories.ContainsKey(typeof(T)))
{
return (T)this.repositories[typeof(T)];
}
// Repository doesn't exist, throw an exception
throw new RepositoryNotFoundException("No repository found for " + typeof(T).Name);
}AuthenticationController
```
public class AuthenticationController : ApiController
{
AuthServiceProvider authProvider;
public AuthenticationController()
{
RepositoryFactory repoFactory = new RepositoryFactory();
repoFactory.AddRepository(new AuthServiceRepository());
repoFactory.AddRepository(new SystemServiceRepository());
authProvider = new AuthServiceProvider(repoFactory);
}
[Route("login")]
[HttpPost]
public Authenticate Login(Login login)
Solution
For the GetRepository question, if you're using an ORM, I tend to add an interface to my modeling classes that I can call generically on an abstract repository class.
Something like
You can return the interface instead on your GetRepository method
This would probably require some significant restructuring of your base repo classes though.
Something like
public abstract class GenericRepository : IGenericRepository where T : IModel
{
//Implementation
}You can return the interface instead on your GetRepository method
public IGenericRepository GetRepository() where T : IModel
{
//WILL NEED TO RE-IMPLEMENT.
}This would probably require some significant restructuring of your base repo classes though.
Code Snippets
public abstract class GenericRepository<T> : IGenericRepository<T> where T : IModel
{
//Implementation
}public IGenericRepository<T> GetRepository<T>() where T : IModel
{
//WILL NEED TO RE-IMPLEMENT.
}Context
StackExchange Code Review Q#61257, answer score: 2
Revisions (0)
No revisions yet.