现在,我的主计长的计算方法都包含一个确定资源的直截面参数,即URLs是/i Controller}/{action}/{id}。 当然,every 方法中的第一个事项是: var=零件。 现在,一个Thing是一个由多个用户和届会共享的活体、read子、径流物体。
因此,我想要做的是,DI框架利用id来从习俗范围(“生活方式?”)中挑出来,并将其注入永久控制器。 如果没有新东西,它就应当创造新的口号。
现在,我的主计长的计算方法都包含一个确定资源的直截面参数,即URLs是/i Controller}/{action}/{id}。 当然,every 方法中的第一个事项是: var=零件。 现在,一个Thing是一个由多个用户和届会共享的活体、read子、径流物体。
因此,我想要做的是,DI框架利用id来从习俗范围(“生活方式?”)中挑出来,并将其注入永久控制器。 如果没有新东西,它就应当创造新的口号。
如果你试图将Thing注入主计长,以便删除所有方法中的“一切=事实”线,那么你可以使用伙伴关系。 NET MVC 海关模型Binder,在您的方法代码开始之前进行检索。
如果您的路线是:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
具有约束力的示范文书将允许你签署这样的方法:
public ActionResult SomeDetails(int id, Thing thing)
{
模型磨灭和验证火在之前执行方法代码。 Default ModelBinder将只从表格中或从其他非永久来源中填满。
推荐的“i m”是,为执行你的观望而设的“Thing”级提供一种定制模型约束器(IModelBinder)。 从Default ModelBinder手中,注入了你的三维理论,推翻了宾德·莫德尔()来研究你的方法。
public class ThingModelBinder : DefaultModelBinder
{
IThingFactory ThingFactory;
public ThingModelBinder(IThingFactory thingFactory)
{
this.IThingFactory = thingFactory;
}
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
Thing thing = new Thing();
string id_string = controllerContext.RouteData.Values["id"].ToString();
int id = 0;
Int32.TryParse(id_string, out id);
var thing = thingFactory.Get(id);
return thing;
}
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException("bindingContext");
}
// custom section
object model = this.CreateModel(controllerContext, bindingContext, typeof(Thing));
// set back to ModelBindingContext
bindingContext.ModelMetadata.Model = model;
// call base class version
// this will use the overridden version of CreateModel() which calls the Repository
// object model = BindComplexModel(controllerContext, bindingContext);
return base.BindModel(controllerContext, bindingContext);
}
然后,你会把该模型捆绑在全球。
// Custom Model Binders
System.Web.Mvc.ModelBinders.Binders.Add(
typeof(MyCompany.Thing)
, new MyMvcApplication.ModelBinders.ThingModelBinder(
WindsorContainer.Resolve<IThingFactory>()
)
);
这将使你的习俗模式受到约束。
你们以正常方式解决温莎人的问题。
依据是:。
如果我正确理解你的话,你有一定数量的<代码>Things,这些编号在集装箱内登记,而且你希望有一个工厂将某一笔记号退回,如果你说<代码>Get(1),你将获得同样的<代码>。 每次打上<<>条/代码>,如果你说<>条码>,你会收到不同东西。
// This is the thing factory - it will create the thing if it has not already
// been created with the given ID - if it is already created it will return
// that instance
public interface IThingFactory
{
Thing Get(int id);
}
// This is the thing - it has an ID and a method that you
// can call that keeps track of how many times it has been
// called (so you can be sure it is the same instance)
public class Thing
{
private int _count;
public Thing(int id)
{
Id = id;
}
public int Id { get; private set; }
public int HowManyCalls()
{
return Interlocked.Increment(ref _count);
}
}
// This is a typed factory selector to manage selecting the component from
// the container by using the name ("Thing" followed by the ID)
public class GetThingComponentSelector : ITypedFactoryComponentSelector
{
public TypedFactoryComponent SelectComponent(MethodInfo method,
Type type,
object[] arguments)
{
return new TypedFactoryComponent("Thing" + arguments[0],
typeof(Thing),
new Arguments(arguments));
}
}
// .... In the installer ....
// Register each thing with a different name that matches the ID
// and register a custom component selector and the thing factory
container
.AddFacility<TypedFactoryFacility>()
.Register(
Component
.For<Thing>()
.Named("Thing1"),
Component
.For<Thing>()
.Named("Thing2"),
Component
.For<GetThingComponentSelector>(),
Component
.For<IThingFactory>()
.AsFactory(c => c.SelectedWith<GetThingComponentSelector>()));
// ... Some demo code (you do not need to resolve the factory directly)
// Now resolve the same thing twice and then a different thing and make sure
// Windsor has handled the lifestyle
var thing = container.Resolve<IThingFactory>().Get(1);
Console.WriteLine("ID should be 1 and is " + thing.Id
+ ". Calls should be 1 and is " + thing.HowManyCalls());
thing = container.Resolve<IThingFactory>().Get(1);
Console.WriteLine("ID should be 1 and is " + thing.Id
+ ". Calls should be 2 and is " + thing.HowManyCalls());
thing = container.Resolve<IThingFactory>().Get(2);
Console.WriteLine("ID should be 2 and is " + thing.Id
+ ". Calls should be 1 and is " + thing.HowManyCalls());
你们在那里看到,我把该身份证作为集装箱中的“名称”,然后使用海关选择人查找该名称。 也许还有其他办法可以做到这一点,但我认为,根据你的问题,至少应该让你开始这样做。
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...