Skip to content

Commit af525fa

Browse files
committed
generics
1 parent 98b23dc commit af525fa

File tree

5 files changed

+227
-1
lines changed

5 files changed

+227
-1
lines changed

Generics/mediatr_parody.linq

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var handler = new RequestHandler();
6+
handler.Handle(new GetAge()).Dump();
7+
handler.Handle(new GetName()).Dump();
8+
}
9+
10+
public interface IRequest<T> { }
11+
12+
public class GetAge : IRequest<int> { }
13+
14+
public class GetName : IRequest<string> { }
15+
16+
public interface IHandler { };
17+
18+
public abstract class Handler<T> : IHandler {
19+
public abstract T Handle(IRequest<T> request);
20+
};
21+
22+
public abstract class Handler<TRequest, TResponse> : Handler<TResponse>
23+
where TRequest : IRequest<TResponse>
24+
{
25+
public override TResponse Handle(IRequest<TResponse> request) =>
26+
Handle((TRequest) request);
27+
28+
protected abstract TResponse Handle(TRequest requst);
29+
}
30+
31+
public class GetAgeHandler : Handler<GetAge, int>
32+
{
33+
protected override int Handle(GetAge request) => 20;
34+
}
35+
36+
public class GetNameHandler : Handler<GetName, string>
37+
{
38+
protected override string Handle(GetName request) => "Foo";
39+
}
40+
41+
42+
public class RequestHandler
43+
{
44+
public Dictionary<Type, IHandler> requestHandlers = new() {
45+
[typeof(GetAge)] = new GetAgeHandler(),
46+
[typeof(GetName)] = new GetNameHandler(),
47+
};
48+
49+
public T Handle<T>(IRequest<T> request)
50+
{
51+
var handler = requestHandlers[request.GetType()];
52+
if (handler is Handler<T> h) {
53+
return h.Handle(request);
54+
}
55+
return default;
56+
}
57+
}
58+
59+

Generics/no_generics.linq

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
Hasher.Hash(new Post()).Dump();
6+
Hasher.Hash(new Comment()).Dump();
7+
}
8+
9+
public class Content
10+
{
11+
public string Id { get; set; } = "id";
12+
public string Author { get; set; } = "author";
13+
public string Text { get; set; } = "text";
14+
}
15+
16+
public class Post : Content { }
17+
18+
public class Comment : Content
19+
{
20+
public string PostId { get; set; } = "postid";
21+
}
22+
23+
public class ContentHashingStrategy
24+
{
25+
public static int Hash(Post post) =>
26+
post.Text.Length
27+
+ post.Author.Length;
28+
}
29+
30+
public class CommentHashingStrategy
31+
{
32+
public static int Hash(Comment comment) =>
33+
comment.Text.Length
34+
+ comment.Author.Length
35+
+ comment.PostId.Length;
36+
}
37+
38+
public class Hasher
39+
{
40+
public static int Hash(Content content)
41+
{
42+
if (content is Post p) return ContentHashingStrategy.Hash(p);
43+
if (content is Comment c) return CommentHashingStrategy.Hash(c);
44+
return 0;
45+
}
46+
}

Generics/with_generics.linq

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
Hasher.Hash(new Post()).Dump();
6+
Hasher.Hash(new Comment()).Dump();
7+
}
8+
9+
public interface IHashingStrategy
10+
{
11+
int Hash(Content content);
12+
}
13+
14+
public class Content
15+
{
16+
public string Id { get; set; } = "id";
17+
public string Author { get; set; } = "author";
18+
public string Text { get; set; } = "text";
19+
}
20+
21+
public class Content<T> : Content where T : IHashingStrategy { }
22+
23+
public class Post : Content<ContentHashingStrategy> { }
24+
25+
public class Comment : Content<CommentHashingStrategy>
26+
{
27+
public string PostId { get; set; } = "postid";
28+
}
29+
30+
public class ContentHashingStrategy : IHashingStrategy
31+
{
32+
public int Hash(Content content)
33+
{
34+
return content.Text.Length
35+
+ content.Author.Length;
36+
}
37+
}
38+
39+
public class CommentHashingStrategy : IHashingStrategy
40+
{
41+
public int Hash(Content content)
42+
{
43+
var comment = content as Comment;
44+
return comment.Text.Length
45+
+ comment.Author.Length
46+
+ comment.PostId.Length;
47+
}
48+
}
49+
50+
public class Hasher
51+
{
52+
public static int Hash<T>(Content<T> content)
53+
where T : IHashingStrategy, new()
54+
{
55+
return Activator.CreateInstance<T>().Hash(content);
56+
}
57+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
Hasher.Hash(new Post()).Dump();
6+
Hasher.Hash(new Comment()).Dump();
7+
}
8+
9+
public interface IHashingStrategy {
10+
int Hash(Content content);
11+
}
12+
13+
public abstract class HashingStrategy<T> : IHashingStrategy
14+
where T : Content
15+
{
16+
public int Hash(Content content) => Hash((T) content);
17+
18+
protected abstract int Hash(T content);
19+
}
20+
21+
public class Content
22+
{
23+
public string Id { get; set; } = "id";
24+
public string Author { get; set; } = "author";
25+
public string Text { get; set; } = "text";
26+
}
27+
28+
public class Content<T> : Content where T : IHashingStrategy, new() {}
29+
30+
public class Post : Content<ContentHashingStrategy> { }
31+
32+
public class Comment : Content<CommentHashingStrategy>
33+
{
34+
public string PostId { get; set; } = "postid";
35+
}
36+
37+
public class ContentHashingStrategy : HashingStrategy<Post>
38+
{
39+
protected override int Hash(Post content)
40+
{
41+
return content.Text.Length
42+
+ content.Author.Length;
43+
}
44+
}
45+
46+
public class CommentHashingStrategy : HashingStrategy<Comment>
47+
{
48+
protected override int Hash(Comment comment)
49+
{
50+
return comment.Text.Length
51+
+ comment.Author.Length
52+
+ comment.PostId.Length;
53+
}
54+
}
55+
56+
public class Hasher
57+
{
58+
public static int Hash<H>(Content<H> content)
59+
where H : IHashingStrategy, new()
60+
{
61+
return Activator.CreateInstance<H>().Hash(content);
62+
}
63+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ Full Playlist can be found [here](https://www.youtube.com/playlist?list=PLOeFnOV
1414
- [IEnumerable](https://youtu.be/at6weLnskpU)
1515
- [Reflection](https://youtu.be/cdG2JxuZvNI)
1616
- [Expression Trees](https://youtu.be/dwr40KytyaY)
17-
- [Delegates (+Func +Action +Closure)](https://youtu.be/KaxNwGA9fiY)
17+
- [Delegates (+Func +Action +Closure)](https://youtu.be/KaxNwGA9fiY)
18+
- [Generics](https://youtu.be/Q1Tv7vj3Txo)

0 commit comments

Comments
 (0)