Using Repository and Unit of Work pattern

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
dipti_drd
Posts: 5
Joined: Tue 26 Jul 2016 12:06

Using Repository and Unit of Work pattern

Post by dipti_drd » Fri 29 Jul 2016 06:45

Hello,

I am using Repository and Unit of Work template in Visual studio with dot connect.
Also i am using Dependency Injection design pattern. For this i am using Unity 4.0.1.

1. I used Database-first approach to connect to Postgres database.
2. Entities (classes generated are kept in frApp_Dal class library).Here i used DBContext Template.
3. Context object and All Repository classes and Interfaces are in another class library named frApp_Repository.
4. I am using Web- API to connect to the database using above two projects.

In my controller class looks like below

public class TaxReturnController : ApiController
{
/* Dependency injection Code*/
private ITaxReturnRepository taxrep;

public TaxReturnController(ITaxReturnRepository repository)
{
taxrep = repository;
}

[HttpGet]
public IHttpActionResult GetTaxReturns()
{
/* TaxReturn is my entitity class generated by DBContext template*/
IEnumerable<TaxReturn> taxreturns = taxrep.GetTaxReturns();
return Ok(taxreturns.ToList());
}
}

in WebApiConfig.cs i am registering the container for Dependency injection.
var container = new UnityContainer();
/* ITaxReturnRepository is an interface and TaxReturnRepository is repository class implementing ITaxReturnRepository */
container.RegisterType<ITaxReturnRepository, TaxReturnRepository>();
config.DependencyResolver = new UnityResolver(container);

UnityResolver is a class defined as
public class UnityResolver:IDependencyResolver
{
protected IUnityContainer container;
public UnityResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}

public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new UnityResolver(child);
}

public object GetService(Type serviceType)
{
try
{
return container.Resolve(serviceType);
}
catch (ResolutionFailedException)
{
return null;
}
}

public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType);
}
catch (ResolutionFailedException)
{
return new List<object>();
}
}

public void Dispose()
{
container.Dispose();
}

}

But i am getting the error as below
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
An error occurred when trying to create a controller of type 'TaxReturnController'. Make sure that the controller has a parameterless public constructor.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type 'FrApp_Web_API.Controllers.TaxReturnController' does not have a default constructor
</ExceptionMessage>
<ExceptionType>System.ArgumentException</ExceptionType>
<StackTrace>
at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
</StackTrace>
</InnerException>
</Error>

Are there any sample examples of using DBContext and Respository and Unit of work template ?
I would also like to know if dot connect supports dependency injection.
This code works perfectly in Entity framework 6.0 without dot connect.
Without dependency Injection if i call methods from repository class using object of repository class then it works perfectly.

Thanks in Advance.

Shalex
Site Admin
Posts: 8245
Joined: Thu 14 Aug 2008 12:44

Re: Using Repository and Unit of Work pattern

Post by Shalex » Fri 29 Jul 2016 14:48

dipti_drd wrote:<ExceptionMessage>
An error occurred when trying to create a controller of type 'TaxReturnController'. Make sure that the controller has a parameterless public constructor.
</ExceptionMessage>
Please check your code. Does the 'TaxReturnController' type include a parameterless public constructor?

dipti_drd
Posts: 5
Joined: Tue 26 Jul 2016 12:06

Re: Using Repository and Unit of Work pattern

Post by dipti_drd » Mon 01 Aug 2016 04:50

Hi Shalex,

If i include parameterless constructor for TaxReturnController controller, then it does not execute
below code, which means no dependency injection is performed.
public TaxReturnController(ITaxReturnRepository repository)
{
taxrep = repository;
}

Thanks
Dipti

Shalex
Site Admin
Posts: 8245
Joined: Thu 14 Aug 2008 12:44

Re: Using Repository and Unit of Work pattern

Post by Shalex » Mon 01 Aug 2016 11:39

Your stack trace includes no Devart classes. The issue persists only after adding dependency injections code (which is generated NOT by Entity Developer), doesn't it? Please clarify the provider specific problem.

dipti_drd
Posts: 5
Joined: Tue 26 Jul 2016 12:06

Re: Using Repository and Unit of Work pattern

Post by dipti_drd » Tue 02 Aug 2016 05:50

Hi Shalex,

Yes this problem occurs only after adding dependency injection through third party tool "Unity".
I solved the problem by using Unity.WebAPI Nuget Package. I was using Unity.MVC3 nuget package.

Thanks for your support.

Post Reply