views:

14

answers:

1

Iam using .NET 3.5. I have asp.net mvc app. There is base controller:

public abstract class BackendController<TModel> : BaseController where TModel : class
{
 // skipped ...

 public ActionResult BatchDelete(int[] ids)
 {
    var entities = repository.GetList().Where(item => ids.Contains(item.ID));
    repository.delete(entities)

 }

 public ActionResult BatchHide(int[] ids)
 {
   var entities = repository.GetList().Where(item => ids.Contains(item.ID));  
   repository.BatchUpdate(
                        entities.Where(item => item.IsHidden == false),
                        c => new TModel { IsHidden = true }
                    );

 }

}

It is won’t compile, because of item.ID and item.IsHidden - but in runtime this is valid type with certain properties. How to make this compile?

+1  A: 

Well, you could use an interface to describe the common properties, and add a constraint to TModel:

public interface IModel
{
    int ID { get; }
    bool IsHidden { get; set; }
}

...

public abstract class BackendController<TModel> : BaseController
    where TModel : IModel, new()
Jon Skeet