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

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

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