score:2
Accepted answer
I think this is a good example of the dynamic factory pattern. I do this all the time. I understand your concern about exception handling but I think there is no need for this, simply because your unit tests will prevent this factory from ever throwing during production and a good unit test can explain as clearly the problem as an exception message.
But if you really want to do error checking, your LINQ query will never throw an exception. It is the ToDictionary
that will throw when there is a double key. What you can do is validate the results of the LINQ query and communicate the double keys:
static GraphFactory()
{
var items = (
from type in _assembly.GetTypes()
where type.GetInterface(typeof(IGraph).FullName) != null
from attribute in type.GetCustomAttributes(true)
.OfType<GraphTypeAttribute>
select new { attribute, type }).ToArray();
ValidateTypes(items);
_item = items.ToDictionary(
k => k.attribute.CustomType, e => e.type);
}
private static void ValidateTypes<T>(T[] items)
{
var firstDoubleCustomType = (
from item in items
group item by item.attribute.CustomType into g
where g.Count() > 1
select g.Key).FirstOrDefault();
if (firstDoubleCustomType != null)
{
throw new InvalidProgramException(
"Doube: " + firstDoubleCustomType.ToString());
}
}
Source: stackoverflow.com
Related Articles
- Wrapping a complex linq query with a Try-Catch block and catching the correct Exceptions
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Linq Query With Multiple Joins Not Giving Correct Results
- Complex LINQ to SQL Query with dates
- Linq sub query when using a repository pattern with EF code first
- How to convert this complex SQL Query with Subqueries into LINQ
- Complex Linq query to select list items with another list property containing a value
- Avoiding repeating code with Linq query + optional params
- Complex LINQ query with complex where clause
- how to write a Linq query with a EF code first Many to Many relationship
- Recreating a large complex query with left outer joins in LINQ
- Run LINQ query with date range check in C# with correct date format
- Linq query with a Join, not returning the correct results
- Simplifying complex linq query with multiple conditions
- Converting complex query to linq with no EXISTS
- Converting SQL query into Linq which used complex sum with group by
- convert sql query to linq with complex left join
- Where clause in complex linq query giving the following error "A query body must end with a select clause or a group clause linq"
- Proper Linq Query for objects with many to many relation ship generated with code first entity framework
- Complex LINQ Query with Groupings (need some help)
- LINQ query code for complex merging of data
- Complex Linq query dealing with articles, categories and versioning
- Entity Framework Code First - The entity or complex type cannot be constructed in a LINQ to Entities query
- Complex linq with Entity Framework query
- VB - LINQ To XML: nested query with anon types from complex Xml file
- How to properly make a LINQ query with clauses based on complex calculated data
- Linq query with nullable sum
- Problem with linq query
- Nested "from" LINQ query expressed with extension methods
- How to use index/position with Where in LINQ query language?
- Empty response when using DbGeography distance and Entity Framework
- Optimize LINQ query
- EF Core 3.1.1 Avoiding the exception "The data types text and varchar are incompatible in the equal to operator" just as LINQPad 6 does
- .First() with an anonymous function to access multiple properties
- LINQ .OrderBy creates nested casing
- LINQ Select in descending order from collection, three different values that go up to the maximum value of the element
- How to search string array's elements in Where Clause ?
- Linq to SQL With Left Outer Join and Group By With Sum - How To
- Linq DateTime expressions wont compare if only one is nullable
- How can I RemoveAll from List.EntitySet
- Flatten a KeyValuePair<string, List<string>> object using linq
- How to find common elements in several List(of)?
- How to update my model code using LINQ
- Using Linq to drill down on strongly typed objects
- Linq Sum dynamic parameter
- In LINQ, how can I do an .OrderBy() on data that came from my .Include()?
- How to Linq query against combobox using c#
- Querying MongoDB with List of IDs using Lambda Expression
- Why does IEnumerable.Any return True for a collection of False-booleans?
- Bind Objects To DataGridview and Reorder Column