自动注册所有ViewModel和View文件

时间:2016-02-17 14:18:01

标签: dryioc

我正在考虑将DryIoc与Caliburn.Micro一起使用,我想自动注册所有ViewModel和Views。

在AutoFac中你可以做这样的事情

builder.RegisterAssemblyTypes(AssemblySource.Instance.ToArray())
    .Where(type => type.Name.EndsWith("ViewModel"))
    .Where(type => type.GetInterface(ViewModelBaseType.Name, false) != null)
    .AsSelf()
    .InstancePerDependency();

DryIoc有类似的方法吗?

1 个答案:

答案 0 :(得分:2)

DryIoc中的等效代码:

container.RegisterMany(AssemblySource.Instance,
    serviceTypeCondition: type => 
        type.Name.EndsWith("ViewModel") &&
        type.GetInterface(ViewModelBaseType.Name, false) != null);

默认重用是Transient,与<script type="text/javascript"> $(document).ready(function () { $('#form1').submit(function (event) { event.preventDefault(); event.returnValue = false; var selectValue = $('#selectValue').val(); $.ajax({ url: "/api/Admin/GetDepotDetails/", type: "POST", data: { "selectValue": selectValue }, dataType: "json", success: function (data) { $("#Grid").html($(data).children()); }, error: function (jqXHR, textStatus, errorThrown) { debugger; alert(textStatus, errorThrown, jqXHR); } }); }); }); </script> 相同。

<强>更新

还有一个更简单,更惯用的选择:

@model IEnumerable<SampleApp.DepotDetail>

<table class="table table-condensed" id="Grid">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.DepotID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ColumnName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Country)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CountryCode)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr class="warning">
            <td>
                @Html.DisplayFor(modelItem => item.DepotID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ColumnName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Country)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CountryCode)
            </td>
        </tr>
    }

</table>