Specification Pattern in Domain Driven Design

I think Specification pattern is not designed for query criteria. Actually, the whole concept of DDD is not, either. Consider CQRS if there are plethora of query requirements. Specification pattern helps develop ubiquitous language, I think it’s like kind of a DSL. It declares what to do rather than how to do it. For example, … Read more

Best way to implement Repository Pattern?

There’s also a good argument for a “none of the above” approach. The problem with generic repositories is that you’re making the assumption that all objects in your system will support all four CRUD operations: Create, Read, Update, Delete. But in complex systems, you’ll likely have objects that support only a few of the operations. … 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

Repository and Unit of Work patterns – How to save changes

Repository can work without Unit Of Work, so it can also have Save method. public interface IRepository<T> { T Get(int id); void Add(T entity); void Update(T entity); void Remove(T entity); void Save(); } Unit Of Work is used when you have multiple repositories (may have different data context). It keeps track of all changes in … Read more

How to get primary key value with Entity Framework Core

I also faced with similar problem and found the following solution // Entity Framework Core public virtual int GetKey<T>(T entity) { var keyName = Context.Model.FindEntityType(typeof (T)).FindPrimaryKey().Properties .Select(x => x.Name).Single(); return (int)entity.GetType().GetProperty(keyName).GetValue(entity, null); }

Repository Pattern: how to Lazy Load? or, Should I split this Aggregate?

Am I misinterpreting the intent of the Repository pattern? I’m going to say “yeah”, but know that me and every person I’ve worked with has asked the same thing for the same reason… “You’re not thinking 4th dimensionally, Marty”. Let’s simplify it a little and stick with constructors instead of Create methods first: Editor e … Read more

Implementing retry logic for deadlock exceptions

How about something like this: public T DeadlockRetryHelper<T>(Func<T> repositoryMethod, int maxRetries) { int retryCount = 0; while (retryCount < maxRetries) { try { return repositoryMethod(); } catch (SqlException e) // This example is for SQL Server, change the exception type/logic if you’re using another DBMS { if (e.Number == 1205) // SQL Server error code … Read more

Generate POCO classes in different project to the project with Entity Framework model

Actually the T4 templates in EF 4.0 were designed with this scenario in mind 🙂 There are 2 templates: One for the Entities themselves (i.e. ModelName.tt) One for the ObjectContext (i.e. ModelName.Context.tt) You should put the ModelName.tt file in you POCO project, and just change the template to point to the EDMX file in the … Read more

Implementation of Repository Pattern in Python?

Out of my head: I define two example domains, User and Animal, an base storage class Store and two specialized Storage classes UserStore and AnimalStore. Use of context manager closes db connection (for simplicity I use sqlite in this example): import sqlite3 def get_connection(): return sqlite3.connect(‘test.sqlite’) class StoreException(Exception): def __init__(self, message, *errors): Exception.__init__(self, message) self.errors … Read more