I know that it is a bad practice to use the database in the scene. However, I am passing the user
object and I wonder how I can make it easy to use.
I like the way to work in Ruby on Rail. You just make a @instance_variable
in the before_filter
and say it with controllers and ideas.
You can not do ASP.NET MVC However, I have created a class with all the data needed to pass the view (Data Contains and Users):
Public Sector XData {Public DBDataContex DB {Received; Set; } The public user user {receives; Set; }}
I get all the data in the initial method of the controller:
public XData X; Secure Override Start Zero (RequestContext RequestContacts) {base.Initialize (requestContext); X = new xData (); X.db = ...; X User = ....; }
Works great: I can get database objects from view like this:
<%> Model.X.DB .supers.first () .name% & gt; To see the data, I have to do the following: public action Foo () {return view (new fu model {X = X, HelloMessage = "Hello World!"}); }
I do not like this thing that I have always written the X = X
object, how can I get myself started?
Thank you
I've seen a similar problem where many controllers have only some customizations In this case, make an abstract base model achieving any other model, and a function that gives you the required model with the base X = X
and set it even further Is done.
Public abstract class MyBaseModel {public user user {get; Set; }} Public class FooModel: MyBaseModel {public string FooMessage {get; Set; }} Public Class Barmodel: Mybus Model {public string bar-message {received; Set; }} Public Class MyController: Controller {Public Actionersult Fu () {var model = this.GetModel & lt; FooModel & gt; (); // Set property on FooModel Model FooMessage = "Hello World!" See Return (Model); } Public Transit Bar () {var model = this.GetModel & lt; Barmodel & gt; (); // Set properties on BarModel Model. Burmese = "Hello World 2!" See Return (Model); } Protected T GetModel & lt; T & gt; () Where T: MyBaseModel, New () {T Model = New T (); // Set properties on MyBaseModel Model User = ...; Return model; }}
If you want to share between MyBaseModel
and GetModel
between multiple controllers, then call it ModelProvider
or something that is given to every controller, ideally through dependence injection
Comments
Post a Comment