- Joined
- Jun 23, 2010
- Messages
- 2,324
- Reaction score
- 2,195
4. How do you mean? You mean replacing all "new ..." instances? I know with the "handler" pipeline it can't be done for some weird reason (trust me, ask DotNetty...) as I tried doing it with DI before.
I'm sure you can do it with DI. Try something like this:
Code:
public class ActionChannelInitializerFactory {
private readonly IServiceProvider _serviceProvider;
public ActionChannelInitializerFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public ActionChannelInitializer<IChannel> Create()
{
return new ActionChannelInitializer<IChannel>(channel => {
channel.Pipeline.AddLast("decoder", _serviceProvider.GetRequiredService<Decoder>();
channel.Pipeline.AddLast("encoder", _serviceProvider.GetRequiredService<Encoder>());
channel.Pipeline.AddLast("handler", _serviceProvider.GetRequiredService<GameNetworkHandler>());
});
}
}
...
public class GameNetworkListener
{
private readonly ActionChannelInitializerFactory _actionChannelInitializerFactory;
...
public GameNetworkListener(ActionChannelInitializerFactory actionChannelInitializerFactory, ...)
{
_actionChannelInitializerFactory = actionChannelInitializerFactory;
...
}
...
bootstrap
.ChildHandler(_actionChannelInitializerFactory.Create())
...
}
6. I always thought LINQ would slow things down (not sure how much it matters nowadays though), of course that can be benchmarked but that's the reason I try to limit my LINQ usage.
LINQ itself is fine. They're just a bunch of extension methods. The .Where(predicate) can be compared to something like this:
Code:
public static IEnumerable<T> Where<T>(this IEnumerable<T> items, Func<T, bool> predicate)
{
foreach (var item in items)
{
if (predicate(item))
{
yield return item;
}
}
}
You should however becareful on how you use it. An IEnumerable computes it's results when it's needed. For example if you do something like:
Code:
var filteredItems = items.Where(x => x.type == "foe");
And you would have used filteredItems, like the following:
Code:
if (filteredItems.Any())
{
SomeOtherMethod(filteredItems);
}
The .Any() method, and everything inside the SomeOtherMethod that accesses the IEnumerable would cause a loop and executes the predicate given in the Where. You can avoid this by using .ToList(), .ToArray() or any method that returns a concreet result.
The same applies to IQuerable. You should never return that outside your DAO/Repository/w.e. you use to access your database.
That's just one pitfall you should be aware off. There are a few more, like using First/Single for fetching a single record by an id in a for loop.