C # Design Question

I started developing a small application and had some questions related to architecture.

I have some basic objects that I am ready to model - Repository and Indicator .

Repository is basically a facade using the Repository Pattern , which is able to retrieve / store arbitrary objects using some database holder (right now it is NHibernate -driven, but I think this is not very important).

Indicator can be called the logical core of my application. It is used to combine abstract values ​​and the exact time to reach this value (therefore, it is formed and works with Value - Time pairs).

I am ready to make this Indicator as general as possible, but I think my current solution is a big failure :)

See the following code snippets:

 public interface IIndicator<T> { IEnumerable<T> RetrieveValues(DateTime start, DateTime end); } // Should also have something like indicator wrapper / proxy stub here - anything // that represents the 'IIndicator' interface acts through that proxy and // caches the evaluated data using it. 

This is the main attempt to implement the indicator (right now it can be considered as a layout):

 public class Indicator<TValue> : // Self-referencing generic parameter. IIndicator<Indicator<TValue>.TimestampProxy> { // Proxy, which is used to add the timestamp to // every indicated value. public class TimestampProxy { public TValue Value; public DateTime Time; public TimestampProxy(DateTime time, TValue value) { Time = time; Value = value; } } private readonly IRepository repository; public Indicator(IRepository repository) { this.repository = repository; } public IEnumerable<TimestampProxy> RetrieveValues(DateTime start, DateTime end) { // Note the custom time stamp comparation in the lambda // expression. Comparation includes the 'start' and 'end' limits. IQueryable<TimestampProxy> queryable = repository.Retrieve<TimestampProxy>( x => x.Time.CompareTo(start) >= 0 && x.Time.CompareTo(end) <= 0); return queryable.ToList(); } } 

Now - it may look fine, but I'm absolutely sure that the used TimestampProxy really evil.

It also complicates the comprehension (for example, signing the IEnumerable<TimestampProxy> RetrieveValues(...) method will probably result in the phrase β€œwtf ?!” from the person who is studying the code).

Unfortunately, I cannot come up with a better solution / global redesign - could you advise me how to do this, or just tell me some ideas about how this function should be implemented?

Thanks.

+4
source share
1 answer

How about refactoring the RetrieveValues ​​method back to the repository and transitioning with the much simpler Indicator class, which basically replaces your TimestampProxy class.

 public class Indicator<T> { public DateTime Timestamp { get; set; } public T Value { get; set; } } public class Repository { public IEnumerable<Indicator<T>> RetrieveIndicators<T>( DateTime start, DateTime end ) { // determine table to query based on type T // query and convert objects to Indicator<T> // return collection } } 

One thing that bothers me is that when you created it, you lost the connection to the DB table. Perhaps it would be better to simply define an interface that implements all your specific database objects, and use partial implementations to map the actual "value" to the value.

 public interface Indicator<T> { DateTime Timestamp { get; } T Value { get; } } public partial class TemperatureIndicator : Indicator<double> { public double Value { get { return this.Temperature; } } } 

Now you have repository implementation methods that return objects of each type that can be used as (in .NET 4 or in lower versions) interface type objects for common operations.

+1
source

All Articles