Page 1 of 1

Using Repository and Unit of Work pattern

Posted: Fri 29 Jul 2016 06:45
by dipti_drd
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.

Re: Using Repository and Unit of Work pattern

Posted: Fri 29 Jul 2016 14:48
by Shalex
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?

Re: Using Repository and Unit of Work pattern

Posted: Mon 01 Aug 2016 04:50
by dipti_drd
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

Re: Using Repository and Unit of Work pattern

Posted: Mon 01 Aug 2016 11:39
by Shalex
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.

Re: Using Repository and Unit of Work pattern

Posted: Tue 02 Aug 2016 05:50
by dipti_drd
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.