Anasayfa Specification Pattern Nedir?
Gönderi
Iptal et

Specification Pattern Nedir?

Herkese merhaba, bugün specification tasarım deseninin ne olduğunu ve hangi durumlarda kullanılacağı, bizi nasıl bir yükten kurtaracağını ve son olarak nasıl implemente edileceğinden bahsedeceğim.

Domain Driven Design tarafındaki makalelere bakarsak hepsinin ortak bir anlattığı konu, karışık iş süreçlerini ortak bir dil (ubiqutious language) aracılığıyla çözüme kavuşturmak. Bu süreçte yazılımcıların daha kolay bir şekilde geliştirme yapmasını sağlayan desendir Specification.

Specification: Belirli bir domain kuralını tek bir birim -specification- olarak soyutlayıp farklı senaryolar için kullanmamıza ve farklı domain kuralları ile birleştirebilmemize olanak sağlayan bir yapı.

Örnek üzerinden gidelim ve neden ihtiyaç duyacağımızı anlayalım.

Bir proje yönetim sistemi üzerinde çalıştığınızda hizmet sağladığınız departman sizden inaktif durumdaki task listesini isteyebilir.

Task modeli aşağıdaki gibi property’lere sahip olsun.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Task
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public bool IsClosed { get; set; }
    public string? ClosedReason { get; set; } = null;
    public int? AssignedUserId { get; set; }
    public DateTime? LastCommentDate { get; set; }
    public DateTime CreationDate { get; set; }

    public bool IsInActive()
    {
        DateTime daysAgo30 = DateTime.Now.Subtract(TimeSpan.FromDays(30));
        return !IsClosed &&
                AssignedUserId == null &&
                CreationDate < daysAgo30 &&
                ( LastCommentDate < daysAgo30 || LastCommentDate == null );
    }
}

Genelde bu gibi bir işlemi, IsInActive metotu ile basitçe çözebiliriz.

Bu noktaya kadar her şey güzel.

Daha sonra farklı bir noktada ise taskların, aktif olup olmadıklarını ve aktif ise son yorum tarihinin 1 haftadan önce olanları kontrol etme ihtiyacımızın olduğunu düşünelim. Bu işlemi de aşağıdaki gibi çözebiliriz.

1
2
3
4
DateTime daysAgoOneWeek = Datetime.Now.Substract(TimeSpan.FromDays(7));
if(!task.IsInActive && task.CreationDate < daysAgoOneWeek) {
   //logic
}

Bu örnekte olduğu gibi birçok kez domain kuralını sürekli farklı yerlerde kullanmak zorunda kalacağız ve DRY prensibini uygulayamamış olacağız. İşte bu noktada Specification Pattern yardımıyla tüm domain kurallarımızı tek bir yerden tekrar kullanabilir hale getirebileceğiz. Ayrıca bu domain kurallarını birleştirip farklı bir domain kuralı da oluşturabileceğiz.

Bu sayede daha merkezi bir yapı oluşturarak daha az kod ile daha çok iş yapabileceğiz. Ayrıca ekibe yeni gelen birisinin de alışması bir o kadar kolay olacaktır.


Kodlama Vakti

Öncelikle ISpecification adlı bir interface üretelim.

1
2
3
public interface ISpecification<T>{
    bool IsSatisfiedBy(T entity);
}

Tüm specification sınıfları için genel bir interface oluşturduk. Şimdi base specification sınıfı oluşturma vakti.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public abstract class CompositeSpecification<T> : ISpecification<T>     
{
    public abstract bool IsSatisfiedBy(T entity);

    public abstract Expression<Func<T, bool>> GetExpression();

    public ISpecification<T> And(ISpecification<T> specification)       
    {
        return new AndSpecification<T>(this, specification);
    }
    public ISpecification<T> Or(ISpecification<T> specification)        
    {
        return new OrSpecification<T>(this, specification);
    }
} 

Türetilen sınıf ve interface hakkında:

  • IsSatisfiedBy metotu iş kuralının nesnelere uygulanmasının uygun olup olmadığının kontrolünü yapar.
  • And, Or ve Not metotları birden fazla domain kuralının zincirleme olarak bir arada gerektiği senaryolarda kullanılır.
  • Specification Pattern sadece bu 3 operatörle kısıtlı değildir. Ayrıca AndNot, OrNot, AndOr gibi operatörler de bulunmaktadır.(And ile Or birlikte kullanıldığında AndOr specification oluşturulması gibi…)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class AndSpecification<T> : CompositeSpecification<T>
{
    public ISpecification<T> leftSpec;
    public ISpecification<T> rightSpec;

    public AndSpecification(ISpecification<T> leftSpec, ISpecification<T> rightSpec)
    {
        this.leftSpec = leftSpec;
        this.rightSpec = rightSpec;
    }
 
    public override bool IsSatisfiedBy(T obj)
    {
        return (rightSpec.IsSatisfiedBy(obj) && leftSpec.IsSatisfiedBy(obj));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class OrSpecification<T> : CompositeSpecification<T>
{
    public ISpecification<T> leftSpec;
    public ISpecification<T> rightSpec;

    public OrSpecification(ISpecification<T> leftSpec, ISpecification<T> rightSpec)
    {
        this.leftSpec = leftSpec;
        this.rightSpec = rightSpec;
    }

    public override bool IsSatisfiedBy(T obj)
    {
        return ( rightSpec.IsSatisfiedBy(obj) || leftSpec.IsSatisfiedBy(obj) );
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
public class NotSpecification<T> : CompositeSpecification<T>
{
    public ISpecification<T> spec;
    public NotSpecification(ISpecification<T> spec)
    {
        this.spec = spec;
    }
    public override bool IsSatisfiedBy(T obj)
    {
        return !spec.IsSatisfiedBy(obj);
    }
}

Base sınıfımızı da oluşturduğumuza göre artık bu specification sınıflarından nasıl specification oluşturabileceğinizi göstermek istiyorum.

Öncelikle Task modelimizin aktif olup olmadığını kontrol eden specification sınıfını oluşturalım.

1
2
3
4
5
6
7
8
9
10
public class ActiveTaskSpecification : CompositeSpecification<Models.Task>
{
    public override bool IsSatisfiedBy(Models.Task obj)
    {
        DateTime daysAgo30 = DateTime.Now.Subtract(TimeSpan.FromDays(30));
        return !obj.IsClosed &&
                obj.CreationDate > daysAgo30 &&
                (obj.LastCommentDate > daysAgo30 || obj.LastCommentDate == null );
    }
}

Kullanıcıya ait taskları getirmemizi sağlayan specification da oluşturalım.

1
2
3
4
5
6
7
8
public class GetAssignedUserTaskSpecification : CompositeSpecification<Models.Task>
{
    private int UserId { set; get; }

    public GetAssignedUserTaskSpecification(int userId) => UserId = userId;

    public override bool IsSatisfiedBy(Models.Task obj) => obj.AssignedUserId == UserId;
}

Bir Task nesnesinin aktif olup olmadığını kontrol eden bir spesifikasyon ve bir kullanıcıya assign edilen taskları getiren bir spesifikasyonumuz var. Gelin bu ikisini birleştirerek kullanıcıya assign edilen aktif taskları bulalım.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class GetActiveUserTaskSpecification : CompositeSpecification<Models.Task>
{
    private readonly int userId;
    public GetActiveUserTaskSpecification(int userId)
    {
        this.userId = userId;
    }
    public override bool IsSatisfiedBy(Models.Task obj)
    {
        var activeSpec = new ActiveTaskSpecification();
        var userTaskSpec = new GetAssignedUserTaskSpecification(userId);

        return (activeSpec.And(userTaskSpec)).IsSatisfiedBy(obj);
    }
}

Elimizdeki bu specification sınıfını kullanmadan önce modelimizi yeni haliyle güncelleyelim.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Task
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public bool IsClosed { get; set; }
    public string? ClosedReason { get; set; } = null;
    public int? AssignedUserId { get; set; }
    public DateTime? LastCommentDate { get; set; }
    public DateTime CreationDate { get; set; }

    public bool IsInActive()
    {
        var spec = new InActiveTaskSpecification();
        return spec.IsSatisfiedBy(this);
    }
}

Gördüğünüz gibi specification kullanarak DRY prensibini çiğnemeyerek daha merkezi bir yapı oluşturmuş olduk. Şimdi ise elimizde List olsun ve bunun üzerinde işlem yapalım.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[ApiController]
[Route("task")]
public class TaskController : ControllerBase
{
    public List<Models.Task> Tasks = new List<Models.Task>
    {
        new Models.Task
        {
            Title = "Test Task 1",
            CreationDate = DateTime.Now.Subtract(TimeSpan.FromDays(15)),
            Description = "Test Description 1",
            AssignedUserId = 1,
        },
        new Models.Task
        {
            Title = "Test Task 2",
            CreationDate = DateTime.Now.Subtract(TimeSpan.FromDays(35)),
            Description = "Test Description 2",
            AssignedUserId = 1,

        },
        new Models.Task
        {
            Title = "Test Task 3",
            CreationDate = DateTime.Now.Subtract(TimeSpan.FromDays(25)),
            Description = "Test Description 3",
            AssignedUserId = 2,

        },
        new Models.Task
        {
            Title = "Test Task 4",
            CreationDate = DateTime.Now.Subtract(TimeSpan.FromDays(45)),
            Description = "Test Description 4",
            AssignedUserId = 3,

        },
    };

    [HttpGet]
    public List<Models.Task> Get()
    {
        List<Models.Task> inActiveTasks = new List<Models.Task>();
        var spec = new GetActiveUserTaskSpecification(1);
        foreach (var item in Tasks)
        {
            if (spec.IsSatisfiedBy(item))
                inActiveTasks.Add(item);
        }
        return inActiveTasks;
    }
}

Dummy bir liste oluşturdum ve bunun üzerinden işlem yapacağız. IIS Express üzerinden ayağa kaldırdıktan sonra swagger üzerinden istek yapıyorum ve response modelimiz aşağıdaki şekilde dönüyor.


Bu yazımda specification nedir, tam olarak ne işe yarar ve zincirleme specification implementasyonu gibi konuları anlatmaya çalıştım.

Okuduğunuz için teşekkür ederim. Bir sonraki yazıda görüşmek dileğiyle.

This post is licensed under CC BY 4.0 by the author.