Is Unit Of Work and Repository Patterns very useful for big projects?

Now, the first question should be, why do I need a repository or unit of work pattern at all? Couldn’t I just use the EF context from the controller, having the full power of directly writing the query I need and returning the data? Answer: You could, but the real intent behind is testability and … Read more

Examples of Repository Pattern with consuming an external REST web service via HttpClient?

A simple example: // You need interface to keep your repository usage abstracted // from concrete implementation as this is the whole point of // repository pattern. public interface IUserRepository { Task<User> GetUserAsync(int userId); } public class UserRepository : IUserRepository { private static string baseUrl = “https://example.com/api/” public async Task<User> GetUserAsync(int userId) { var userJson … Read more

Is it ok for entities to access repositories?

it’s not a horrible violation of DDD it’s a horrible violation of… well… it’s just plain horrible (i say this tongue in cheek) :). First off, your entity becomes dependent on having a repository… that’s not ideal. Ideally you’d want to have your repository create the Person and then assign it everything it needs to … Read more

How is the Data Mapper pattern different from the Repository Pattern?

[the Repository is] another layer of abstraction over the mapping layer where query construction code is concentrated. The DataMapper ensures the DB side of the fence doesn’t need to know about the specifics of your business logic and how the data is kept in memory by your business objects and your business side of the … Read more

ASP.NET Identity with Repository and Unit of Work

I have found working with ASP.Net Identity 2.0 and EF6 a bit challenging. The biggest drawback is the lack of documentation or conflicting documentation. I am using WebApi 2.0, EF6 and ASP.Net Identity 2.0. At first it was tough to get going but once it’s working, it’s been good. I created my own Identity classes. … Read more

Error while validating the service descriptor ‘ServiceType: INewsRepository Lifetime: Singleton ImplementationType: NewsRepository’:

Firstly,you need to change: services.AddSingleton<INewsRepository, NewsRepository>(); To: services.AddTransient<INewsRepository, NewsRepository>(); Secondly,you need to inject IMemoryCache instead of MemoryCache in NewsRepository. Here is a simple demo like below: 1.Startup.cs: public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddSession(); services.AddTransient<INewsRepository, NewsRepository>(); services.AddDbContext<BmuContext>(options => options.UseSqlServer(Configuration.GetConnectionString(“Connectionstring”))); services.AddMemoryCache(); } 2.appsettings.json: “ConnectionStrings”: { “Connectionstring”: “Server=(localdb)\\mssqllocaldb;Database=Bmu;Trusted_Connection=True;MultipleActiveResultSets=true” } 3.NewsRepository: public class NewsRepository : INewsRepository { private … Read more

How can I use the repository pattern correctly?

One thing that I did wrong when playing around with repository pattern—just like you, I thought that table relates to repository 1:1. When we apply some rules from domain-driven design, the grouping repositories problem often disappears. The repository should be per the Aggregate root and not table. It means, if entity shouldn’t live alone (i.e., … Read more

How can I implement the Unit Of Work pattern with Dapper?

This Git project is very helpful. I started from the same and did some changes as per my need. public sealed class DalSession : IDisposable { public DalSession() { _connection = new OleDbConnection(DalCommon.ConnectionString); _connection.Open(); _unitOfWork = new UnitOfWork(_connection); } IDbConnection _connection = null; UnitOfWork _unitOfWork = null; public UnitOfWork UnitOfWork { get { return _unitOfWork; … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)