Below is the factory code that provides IGraph objects that have an implemented GraphTypeAttribute. Inside the GraphFactory static constructor, a list is created using Linq to collect the appropriate classes that will be delivered to Factory. Usually without Linq, I had a loop of loops, and if it could then be easily wrapped by the corresponding try-catch blocks. Since everything is filled with one request, I got a little confused about how to implement the correct data access here .
So my question is / are
- What is the best pattern for handling exceptions in a linq query.
- Should I split it into different requests or not use linq at all?
- Or am I doing something in a query that can eliminate non-existent elements, scan for invalid classes, etc., request duplicate values, etc. (query optimization;).
The query result should be a list of all classes that the factory can provide. For instance. decorated with an attribute and implemented interface.
A "Factory" that creates objects for graphically presenting data:
public sealed class GraphFactory { static readonly GraphFactory _instance = new GraphFactory(); static readonly IDictionary<string, Type> _items; static readonly Assembly _assembly = Assembly.GetExecutingAssembly(); public static GraphFactory Instance { get { return _instance; } } GraphFactory() { } static GraphFactory() { try { _items = (from type in _assembly.GetTypes()
interface (members omitted):
public interface IGraph { }
to decorate the appropriate classes for factory assigment
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false,Inherited=true)] public class GraphTypeAttribute : System.Attribute { public GraphTypeAttribute(string friendlyName) { } }
attribute decorated classes
[GraphTypeAttribute("piechart")] public class PieChart : IGraph{ } [GraphTypeAttribute("map")] public class WorldMap : IGraph { } [GraphTypeAttribute("horizontalbar")] public class Bar : IGraph { } [GraphTypeAttribute("verticalbar")] public class VerticalBar : Bar { }
Sample Usage:
foreach (string friendlyName in GraphFactory.FriendlyNames) { IGraph auth = GraphFactory.CreateGraph(friendlyName); }
Any other comments or class recommendations are greatly appreciated.
source share