How to transfer a shared object to an Aspx page

I am working on an ASP.Net MVC website and I am fixated on getting C # code using generics to play well with my views.

I have a controller that passes the model into a view (so far so good). The model has a type IDescribedEnumerable<T>with some restrictions on T, among which there is a restriction that Tinherits from the interface ( IDescribedModel).

I can easily write a view that accepts IDescribedEnumerable<Country>, and it will work as long as it Tis actually a type Country.

However, I would also like to write a default view that accepts IDescribedEnumerableof <<whatever>>and will display it. This should be completely possible. I do not always need to know the specific type of Model. Often just knowing that is enough IDescribedModel.

As long as I stay in C #, there are no problems. When I do not need a specific type, I simply declare the methods and objects as accepting <T>. When I don't care, I declare them accepted Country.

But:
(1) If I want to display the view, I have to choose the type. I can't just say Inherits="System.Web.Mvc.ViewUserControl<IDescribedEnumerable<T>>"I need to specify an existing type between <>. (even if I inherited from ViewUserControl, I would have to send it to IDescribedEnumerable<<something>>. (2) Ideally, I would say that Model IDescribedEnumerable<IDescribedModel>is in the default view and IDescribedEnumerable<Country>in the specific implementation. However, then my controller should know if it will be displayed in the default view or a specific view. It is not possible to just select an object from IDescribedEnumerable<Country>to IDescribedEnumerable<IDescribedModel>. (IIRC is possible in C # 4, but I use 3.5)

? , , ( , 65 , Type)

+3
3

#, , , IEnumerable :

public IDescribedEnumerable<IDescribedModel> AsIDescribedModels() IDescribedEnumerable GenericDescribedEnumerable<T> : IDescribedEnumerable<IDescribedModel>. DescribedEnumerable<T> GenericDescribedEnumerable . GenericDescribedEnumerable this

:

public interface IDescribedModel<T> : IDescribedModel{
    T original {
        get;
    }
}

public interface IDescribedEnumerable {
    IDescribedEnumerable<IViewModel> AsIViewModels();
}

public interface IDescribedEnumerable<T> : IDescribedEnumerable
    where T : IDescribedModel{
    IEnumerable<T> GetViewModels();
}

public class DescribedEnumerable<T> : IDescribedEnumerable<IDescribedModel<T>>{

    public DescribedEnumerable(IEnumerable<T> enumerable) {}

    public IDescribedEnumerable<IViewModel> AsIViewModels() {
        return new GenericDescribedEnumerable<T>(/*allProperties*/);
    }

    public IEnumerable<T> GetViewModels() {
        foreach ( T obj in _enumerable ) {
            var vm = new DescribedModel<T>( obj);
            yield return vm;
        }
    }
}

public class GenericDescribedEnumerable<T> : IDescribedEnumerable<IViewModel>{
    //pass in the constructor everything you need, or create in the 
            //constructor of DescribedEnumerable<T>
    public GenericDescribedEnumerable(/*allProperties*/) {
    }

    public IEnumerable<IViewModel> GetViewModels() {
        foreach ( T obj in _enumerable ) {
            var vm = new PlatoViewModel<T>( obj );
            yield return vm;
        }
    }

    public IDescribedEnumerable<IViewModel> AsIViewModels() {
        return this;
    }
}
0

, , . , ViewPage, ViewPage ViewPage: ViewPage.

0
Inherits="System.Web.Mvc.ViewUserControl<IDescribedEnumerable<T>>"

...

Model.Cast<IModel>()
0

Source: https://habr.com/ru/post/1725597/


All Articles