Is it possible to use C # object initializers with factories

I am looking for new object initializers in C # 3.0 and would like to use them. However, I cannot figure out how to use them with something like Microsoft Unity. I probably missed something, but if I want to keep strongly typed property names, then I'm not sure I can. e.g. I can do it (pseudo code)

Dictionary<string,object> parms = new Dictionary<string,object>(); parms.Add("Id", "100"); IThing thing = Factory.Create<IThing>(parms)(); 

and then do something in Create through reflection to initialize the parms ... but if I want it to be strongly typed at the Create level, like the new intitalisers object, then I don’t see how I can.

Is there a better way? Thanks

+3
source share
6 answers

AFAIK, this is impossible to do. I would stay away from thinking in this case if I were you, as the reflection is really very slow and can become an easy bottleneck in your application. I think that using reflection for this is actually misusing reflections.

Do not shoot in the foot because you want to imitate syntactic sugar. Just set the fields / properties of the instance one by one, the classic way. It will be much faster than reflection.

Remember: the design template is not a silver bullet. Factory methods and object initializers are good, but try to use common sense and use them only when they really make sense.

0
source

I am not familiar with Unity, but the idea of ​​IoC / DI is that you do not build the object yourself, so of course you cannot use the object initializer syntax.

I assume that you can use from C # 3.0 in your example, this is an anonymous type instead of a dictionary. I don't know if Unity can use this.

In any case, if you call calls like Factory.Create (), you are probably using IoC incorrectly.

0
source

Consider investing time in a Unity or Castle addiction or any other IOC infrastructure available for .net. IOC will allow you to pass the complexity of initializing the object from your code to the configuration file. In your application, you will use interfaces to access objects initialized by the IOC container. Another service that the IOC provides is the monitoring of the lifetime of objects (one-stop, etc.).

0
source

Graham, the idea of ​​IoC / DI is that your components declare their dependencies as constructor parameters or public properties of certain types (usually interfaces). Then the container creates a dependency graph and satisfies these requirements (regarding the component's lifestyle, if in the case of a mature IoC, for example Castle Windsor).

This way you basically write your components, state dependencies, and then register contracts and implementations in an IoC container and no longer care about creating objects. You just make one call, similar to Application.Start () :)

The bad news is that some structures are difficult to integrate. Like ASP.NET WebForms, where you do not control the creation of IHttpHandler and cannot configure the factories that will use IoC to create them. You can check out Ayende "Rhino Igloo," who is doing everything possible to use IoC in such an environment.

0
source

Maybe this can help ... Have you checked this blog post?

http://www.cookcomputing.com/blog/archives/000578.html

The author presents a method for building an object from a factory and registering an object in an IoC container.

0
source

In this question, it is better to answer: Is it possible to use a C # object initializer using the factory method? .

There were four possibilities:

  • Take lambda as argument
  • Return the builder instead
  • Use the default constructor by passing an initialized object
  • Use anonymous object
0
source

All Articles